Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Board: NodeMCU 1.0 (ESP-12E Module) */
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- //-----------------------------------------------------------------------------------------------------------------------
- const char* ssid = "********";
- const char* password = "********";
- const char* mqtt_server = "192.168.1.11";
- const byte reed_switch_pin = D2;
- //-----------------------------------------------------------------------------------------------------------------------
- WiFiClient espClient;
- PubSubClient client(mqtt_server, 1883, callback, espClient);
- //-----------------------------------------------------------------------------------------------------------------------
- void go_to_sleep(int seconds)
- {
- send_mqtt_message("/dev/reed-switch_001/go_to_sleep", "1");
- delay(1000);
- ESP.deepSleep(seconds * 1000000);
- }
- //-----------------------------------------------------------------------------------------------------------------------
- boolean reconnect()
- {
- if (client.connect("ESP8266Client")) { return client.connected(); }
- return 0;
- }
- //-----------------------------------------------------------------------------------------------------------------------
- int send_mqtt_message(String Topic, String Message)
- {
- if (!client.connected()) { reconnect(); }
- if (client.publish((char*) Topic.c_str(), (char*) Message.c_str())) return 1;
- else return 0;
- }
- //-----------------------------------------------------------------------------------------------------------------------
- void Interrupt_something() { Serial.print("An interrupt "); }
- //-----------------------------------------------------------------------------------------------------------------------
- void setup()
- {
- Serial.begin(9600);
- Serial.setTimeout(2000);
- while(!Serial) { }
- pinMode(reed_switch_pin, INPUT_PULLUP);
- attachInterrupt(digitalPinToInterrupt(reed_switch_pin), Interrupt_something, CHANGE);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) { delay(500); }
- send_mqtt_message("/dev/reed-switch_001/go_to_sleep", "0");
- send_mqtt_message("/dev/reed-switch_001/connected", "1");
- if ( ESP.getResetReason() == "Deep-Sleep Wake")
- send_mqtt_message("/dev/reed-switch_001/run_type", "0");
- else send_mqtt_message("/dev/reed-switch_001/run_type", "1");
- go_to_sleep(10);
- }
- //-----------------------------------------------------------------------------------------------------------------------
- void loop() { }
- //-----------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement