Advertisement
merkelck

Sonoff Basic with button

Jul 15th, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. /*****
  2.  
  3. All the resources for this project:
  4. https://rntlab.com/
  5. This is the code to flash sonoff basic. Search for sonoffxxX
  6. and replace xx with next higher number.
  7. As of 6/11/2019 there are 01X , 02X ,03X, 04X, 05X, 06X, 10X, A1, A2, in operation.
  8. The X designates the device that sends the state message.
  9. The XA designates the device that sends a state message and has manual control
  10. *****/
  11.  
  12. // Loading the ESP8266WiFi library and the PubSubClient library
  13. #include <ESP8266WiFi.h>
  14. #include <PubSubClient.h>
  15.  
  16. // Change the credentials below, so your ESP8266 connects to your router
  17. const char* ssid = "xxxxx";
  18. const char* password = "xxxxxxx";
  19.  
  20. // Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
  21. const char* mqtt_server = "192.168.1.xxx";
  22.  
  23. // Initializes the espClient
  24. WiFiClient espClient;
  25. PubSubClient client(espClient);
  26.  
  27. // GPIOs of your ESP8266 on your sonoffxxX
  28. int gpio13Led = 13;
  29. int gpio12Relay = 12;
  30. int buttonPin = 0;
  31.  
  32. // Variables will change:
  33. int ledState = HIGH; // the current state of the output pin
  34. int buttonState; // the current reading from the input pin
  35. int lastButtonState = LOW; // the previous reading from the input pin
  36.  
  37. // the following variables are unsigned longs because the time, measured in
  38. // milliseconds, will quickly become a bigger number than can be stored in an int.
  39. unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
  40. unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
  41.  
  42.  
  43. // Don't change the function below. This functions connects your ESP8266 to your router
  44. void setup_wifi() {
  45. delay(10);
  46. // We start by connecting to a WiFi network
  47. Serial.println();
  48. Serial.print("Connecting to ");
  49. Serial.println(ssid);
  50. WiFi.begin(ssid, password);
  51. while (WiFi.status() != WL_CONNECTED) {
  52. delay(500);
  53. Serial.print(".");
  54. }
  55. Serial.println("");
  56. Serial.print("WiFi connected - ESP IP address: ");
  57. Serial.println(WiFi.localIP());
  58. }
  59.  
  60. // This functions is executed when some device publishes a message
  61. // to a topic that your ESP8266 is subscribed to
  62. // Change the function below to add logic to your program,
  63. // so when a device publishes a message to a topic that
  64. // your ESP8266 is subscribed you can actually do something
  65. void callback(String topic, byte* message, unsigned int length) {
  66. Serial.print("Message arrived on topic: ");
  67. Serial.print(topic);
  68. Serial.print(". Message: ");
  69. String messageTemp;
  70.  
  71. for (int i = 0; i < length; i++) {
  72. Serial.print((char)message[i]);
  73. messageTemp += (char)message[i];
  74. }
  75. Serial.println();
  76.  
  77. // Feel free to add more if statements to control more GPIOs with MQTT
  78.  
  79. // If a message is received on the topic home/office/sonoffxxX,
  80. // you check if the message is either 1 or 0.
  81. // Turns the ESP GPIO according to the message
  82.  
  83. if(topic=="home/office/sonoff02X"){
  84. Serial.print("Changing sonoff02X to ");
  85. if(messageTemp == "on");
  86. else if (ledState == HIGH); {
  87. digitalWrite(gpio13Led, LOW); //turns LED ON
  88. digitalWrite(gpio12Relay, HIGH); //energizes relay
  89. Serial.print("On");
  90. client.publish("home/office/sonoff02X/state","on"); //sends "ON" message to NR
  91. }
  92. }
  93. else if(messageTemp == "off"){
  94. digitalWrite(gpio13Led, HIGH); //turns LED OFF
  95. digitalWrite(gpio12Relay, LOW); //deenergizes relay
  96. Serial.print("Off");
  97. client.publish("home/office/sonoff02X/state","off");//sends "OFF" message to NR
  98. }
  99.  
  100. Serial.println();
  101. }
  102.  
  103. // This functions reconnects your ESP8266 to your MQTT broker
  104. // Change the function below if you want to subscribe to more topics with your ESP8266
  105. void reconnect() {
  106. // Loop until we're reconnected
  107. while (!client.connected()) {
  108. Serial.print("Attempting MQTT connection...");
  109. // Attempt to connect
  110. /*
  111. YOU MIGHT NEED TO CHANGE THIS LINE, IF YOU'RE HAVING
  112. PROBLEMS WITH MQTT MULTIPLE CONNECTIONS
  113. To change the ESP device ID, you will have
  114. to give a new name to the ESP8266.
  115. Here's how it looks:
  116. if (client.connect("ESP8266Client")) {
  117. You can do it like this:
  118. if (client.connect("ESP1_Office")) {
  119. Then, for the other ESP:
  120. if (client.connect("ESP2_Garage")) {
  121. That should solve your MQTT multiple connections problem
  122. */
  123. if (client.connect("sonoff02X")) {
  124. Serial.println("connected");
  125. // Subscribe or resubscribe to a topic
  126. // You can subscribe to more topics (to control more sonoffxxXs)
  127. client.subscribe("home/office/sonoff02X");
  128. } else {
  129. Serial.print("failed, rc=");
  130. Serial.print(client.state());
  131. Serial.println(" try again in 5 seconds");
  132. // Wait 5 seconds before retrying
  133. delay(5000);
  134. }
  135. }
  136. }
  137.  
  138. // The setup function sets your ESP GPIOs to Outputs,
  139. // starts the serial communication at a baud rate of 115200
  140. // Sets your mqtt broker and sets the callback function
  141. // The callback function is what receives messages
  142. // and actually controls the LEDs
  143. void setup() {
  144. // preparing GPIOs
  145.  
  146. pinMode(buttonPin, INPUT);
  147. pinMode(gpio13Led, OUTPUT);
  148. digitalWrite(gpio13Led, HIGH);
  149.  
  150. pinMode(gpio12Relay, OUTPUT);
  151. digitalWrite(gpio12Relay, HIGH);
  152.  
  153. Serial.begin(115200);
  154. setup_wifi();
  155. client.setServer(mqtt_server, 1883);
  156. client.setCallback(callback);
  157. }
  158.  
  159.  
  160.  
  161. // For this project, you don't need to change anything in the loop function.
  162. // Basically it ensures that you ESP is connected to your broker
  163. void loop() {
  164. // read the state of the switch into a local variable:
  165. int reading = digitalRead(buttonPin);
  166.  
  167. // check to see if you just pressed the button
  168. // (i.e. the input went from LOW to HIGH), and you've waited long enough
  169. // since the last press to ignore any noise:
  170.  
  171. // If the switch changed, due to noise or pressing:
  172. if (reading != lastButtonState) {
  173. // reset the debouncing timer
  174. lastDebounceTime = millis();
  175. }
  176.  
  177. if ((millis() - lastDebounceTime) > debounceDelay) {
  178. // whatever the reading is at, it's been there for longer than the debounce
  179. // delay, so take it as the actual current state:
  180.  
  181. // if the button state has changed:
  182. if (reading != buttonState) {
  183. buttonState = reading;
  184.  
  185. // only toggle the LED if the new button state is HIGH
  186. if (buttonState == HIGH) {
  187. ledState = !ledState;
  188. }
  189. }
  190. }
  191.  
  192. // set the LED:
  193. digitalWrite(gpio13Led, ledState);
  194.  
  195. // save the reading. Next time through the loop, it'll be the lastButtonState:
  196. lastButtonState = reading;
  197.  
  198.  
  199.  
  200.  
  201. if (!client.connected()) {
  202. reconnect();
  203. }
  204. if(!client.loop())
  205. client.connect("sonoff02X");
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement