Advertisement
merkelck

Sonoff_REM_061119

Jun 11th, 2019
157
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* This version of sketch is modified for a third LED in the panel that
  2. * responds to the output of the connected sonoff device. One led
  3. * indicates that the remote has sent a message and the second LED
  4. * lights to prove the signal was received and replied to by the connected
  5. * device. Third LED is a simple power indicator. The battery can be recharged
  6. * via the micro usb on the Wemos D1 built in charge circuit.I misspelled one led
  7. * as LDE to help my febble mind keep track of it in the sketch.
  8. */
  9. #include <ESP8266WiFi.h>
  10. #include <PubSubClient.h>
  11.  
  12. const char* ssid = "xxxxx";
  13. const char* password = "xxxxxxxxx";
  14. const char* mqttServer = "192.168.1.180";
  15. const int mqttPort = 1883;
  16.  
  17. #define mqtt_publish "home/office/sonoff01X"
  18. #define mqtt_subscribe "home/office/sonoff01X"
  19. #define mqtt_subscribe2 "home/office/sonoff01X/state"
  20. #define mqtt_sub1 "on"
  21. #define mqtt_sub2 "off"
  22.  
  23. #define LED 5 // (gpio5)
  24. #define BUTTON 4 //(gpio4)
  25. #define LDE 12 // (gpio12)
  26.  
  27. bool lightsOn = false;
  28. bool lightsOn2 = false;
  29.  
  30. WiFiClient espClient;
  31. PubSubClient client(espClient);
  32.  
  33. void setup() {
  34.  
  35. Serial.begin(115200);
  36. pinMode(BUTTON, INPUT_PULLUP); // push button
  37. pinMode(LDE, OUTPUT);//LED 2
  38. pinMode(LED, OUTPUT); // anything you want to control using a switch e.g. a Led
  39.  
  40. delay(500);
  41.  
  42. Serial.print("Connecting to WiFi...");
  43. WiFi.begin(ssid, password);
  44. while (WiFi.status() != WL_CONNECTED)
  45. {
  46. delay(500);
  47. }
  48. Serial.print("Connected to ");
  49. Serial.println(ssid);
  50.  
  51. client.setServer(mqttServer, mqttPort);
  52. client.setCallback(callback);
  53.  
  54. Serial.print("Connecting to MQTT...");
  55. while (!client.connected())
  56. {
  57.  
  58. if (client.connect("ESP8266Client" ))
  59. {
  60. Serial.println("connected");
  61. }
  62. else
  63. {
  64. Serial.print("failed with state ");
  65. Serial.println(client.state());
  66. delay(2000);
  67.  
  68. }
  69. }
  70.  
  71.  
  72. client.subscribe(mqtt_subscribe);
  73. client.subscribe(mqtt_subscribe2);
  74. client.publish(mqtt_publish, "Remote");
  75. }
  76. void loop ()
  77. {
  78. client.loop(); // MQTT houskeeping
  79.  
  80. if(digitalRead(BUTTON) == LOW) // Button pressed (LOW)
  81. {
  82. lightsOn = !lightsOn; // Toggle light state
  83.  
  84. if(lightsOn)
  85. {
  86. client.publish(mqtt_publish, mqtt_sub1);
  87. }
  88. else
  89. {
  90. client.publish(mqtt_publish, mqtt_sub2);
  91. }
  92.  
  93. delay(50); // Switch debounce
  94. while(digitalRead(BUTTON) == LOW) // Wait for button to be released
  95. {
  96. delay(10); // Short delay to yield else WDT reset will happen
  97. }
  98. }
  99. delay(100);
  100. }
  101.  
  102.  
  103. void callback(char* topic, byte * data, unsigned int length)// Callback for subscribed MQTT topics
  104. {
  105. Serial.print(topic);
  106. Serial.print(": ");
  107. for (int i = 0; i < length; i++)
  108. {
  109. Serial.print((char)data[i]);
  110. }
  111. Serial.println();
  112.  
  113.  
  114.  
  115. if (strncmp(topic, mqtt_subscribe, strlen(mqtt_subscribe)) == 0) // Check it's the right topic
  116. {
  117. if (strncmp((char*)data, mqtt_sub1, strlen(mqtt_sub1)) == 0) // Check it's the right data
  118. {
  119. lightsOn = true;
  120. }
  121.  
  122. if (strncmp((char*)data, mqtt_sub2, strlen(mqtt_sub2)) == 0) // Check it's the right data
  123. {
  124. lightsOn = false;
  125. }
  126. }
  127. digitalWrite(LED, lightsOn);
  128.  
  129. if (strncmp(topic, mqtt_subscribe2, strlen(mqtt_subscribe2)) == 0) // Check it's the right topic
  130. {
  131. if (strncmp((char*)data, mqtt_sub1, strlen(mqtt_sub1)) == 0) // Check it's the right data
  132. {
  133. lightsOn2 = true;
  134. }
  135.  
  136. if (strncmp((char*)data, mqtt_sub2, strlen(mqtt_sub2)) == 0) // Check it's the right data
  137. {
  138. lightsOn2 = false;
  139. }
  140. }
  141. digitalWrite(LDE, lightsOn2);
  142.  
  143. }
Advertisement
RAW Paste Data Copied
Advertisement