SHARE
TWEET

Untitled

a guest Apr 19th, 2019 110 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*    Board: NodeMCU 1.0 (ESP-12E Module)    */
  2. #include <ESP8266WiFi.h>
  3. #include <PubSubClient.h>
  4. //-----------------------------------------------------------------------------------------------------------------------
  5.     const char* ssid = "********";
  6.     const char* password = "********";
  7.     const char* mqtt_server = "192.168.1.11";
  8.     const byte reed_switch_pin  = D2;
  9. //-----------------------------------------------------------------------------------------------------------------------
  10.     WiFiClient espClient;
  11.     PubSubClient client(mqtt_server, 1883, callback, espClient);
  12. //-----------------------------------------------------------------------------------------------------------------------
  13. void go_to_sleep(int seconds)
  14. {
  15.     send_mqtt_message("/dev/reed-switch_001/go_to_sleep", "1");
  16.     delay(1000);
  17.     ESP.deepSleep(seconds * 1000000);
  18. }
  19. //-----------------------------------------------------------------------------------------------------------------------
  20. boolean reconnect()
  21. {
  22.     if (client.connect("ESP8266Client"))    {    return client.connected();    }
  23.     return 0;
  24. }
  25. //-----------------------------------------------------------------------------------------------------------------------
  26. int send_mqtt_message(String Topic, String Message)
  27. {
  28.     if (!client.connected())   {    reconnect();    }
  29.     if (client.publish((char*) Topic.c_str(), (char*) Message.c_str()))    return 1;
  30.     else return 0;
  31. }
  32. //-----------------------------------------------------------------------------------------------------------------------
  33. void Interrupt_something() {  Serial.print("An interrupt ");  }
  34. //-----------------------------------------------------------------------------------------------------------------------
  35. void setup()
  36. {
  37.     Serial.begin(9600);
  38.     Serial.setTimeout(2000);
  39.     while(!Serial) { }
  40.  
  41.     pinMode(reed_switch_pin, INPUT_PULLUP);
  42.     attachInterrupt(digitalPinToInterrupt(reed_switch_pin), Interrupt_something, CHANGE);
  43.  
  44.     WiFi.begin(ssid, password);
  45.     while (WiFi.status() != WL_CONNECTED)    {    delay(500);    }
  46.  
  47.     send_mqtt_message("/dev/reed-switch_001/go_to_sleep", "0");
  48.     send_mqtt_message("/dev/reed-switch_001/connected",   "1");
  49.    
  50.     if ( ESP.getResetReason() == "Deep-Sleep Wake")
  51.             send_mqtt_message("/dev/reed-switch_001/run_type", "0");
  52.     else    send_mqtt_message("/dev/reed-switch_001/run_type", "1");
  53.  
  54.     go_to_sleep(10);
  55. }
  56. //-----------------------------------------------------------------------------------------------------------------------
  57. void loop() { }
  58. //-----------------------------------------------------------------------------------------------------------------------
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top