Advertisement
merkelck

sonoff_code

Jun 11th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 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, A1, A2, in operation.
  8. The X designates the device that sends the state message.
  9. *****/
  10.  
  11. // Loading the ESP8266WiFi library and the PubSubClient library
  12. #include <ESP8266WiFi.h>
  13. #include <PubSubClient.h>
  14.  
  15. // Change the credentials below, so your ESP8266 connects to your router
  16. const char* ssid = "xxxxx";
  17. const char* password = "xxxxxxxxx";
  18.  
  19. // Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
  20. const char* mqtt_server = "192.168.1.180";
  21.  
  22. // Initializes the espClient
  23. WiFiClient espClient;
  24. PubSubClient client(espClient);
  25.  
  26. // GPIOs of your ESP8266 on your sonoffxxX
  27. int gpio13Led = 13;
  28. int gpio12Relay = 12;
  29.  
  30. // Don't change the function below. This functions connects your ESP8266 to your router
  31. void setup_wifi() {
  32. delay(10);
  33. // We start by connecting to a WiFi network
  34. Serial.println();
  35. Serial.print("Connecting to ");
  36. Serial.println(ssid);
  37. WiFi.begin(ssid, password);
  38. while (WiFi.status() != WL_CONNECTED) {
  39. delay(500);
  40. Serial.print(".");
  41. }
  42. Serial.println("");
  43. Serial.print("WiFi connected - ESP IP address: ");
  44. Serial.println(WiFi.localIP());
  45. }
  46.  
  47. // This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
  48. // Change the function below to add logic to your program, so when a device publishes a message to a topic that
  49. // your ESP8266 is subscribed you can actually do something
  50. void callback(String topic, byte* message, unsigned int length) {
  51. Serial.print("Message arrived on topic: ");
  52. Serial.print(topic);
  53. Serial.print(". Message: ");
  54. String messageTemp;
  55.  
  56. for (int i = 0; i < length; i++) {
  57. Serial.print((char)message[i]);
  58. messageTemp += (char)message[i];
  59. }
  60. Serial.println();
  61.  
  62. // Feel free to add more if statements to control more GPIOs with MQTT
  63.  
  64. // If a message is received on the topic home/office/sonoffA1, you check if the message is either 1 or 0. Turns the ESP GPIO according to the message
  65. if(topic=="home/office/sonoffxxX"){
  66. Serial.print("Changing sonoffxxX to ");
  67. if(messageTemp == "on"){
  68. digitalWrite(gpio13Led, LOW);
  69. digitalWrite(gpio12Relay, HIGH);
  70. Serial.print("On");
  71. client.publish("home/office/sonoffxxX/state","on");
  72. }
  73. else if(messageTemp == "off"){
  74. digitalWrite(gpio13Led, HIGH);
  75. digitalWrite(gpio12Relay, LOW);
  76. Serial.print("Off");
  77. client.publish("home/office/sonoffxxX/state","off");
  78. }
  79. }
  80. Serial.println();
  81. }
  82.  
  83. // This functions reconnects your ESP8266 to your MQTT broker
  84. // Change the function below if you want to subscribe to more topics with your ESP8266
  85. void reconnect() {
  86. // Loop until we're reconnected
  87. while (!client.connected()) {
  88. Serial.print("Attempting MQTT connection...");
  89. // Attempt to connect
  90. /*
  91. YOU MIGHT NEED TO CHANGE THIS LINE, IF YOU'RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS
  92. To change the ESP device ID, you will have to give a new name to the ESP8266.
  93. Here's how it looks:
  94. if (client.connect("ESP8266Client")) {
  95. You can do it like this:
  96. if (client.connect("ESP1_Office")) {
  97. Then, for the other ESP:
  98. if (client.connect("ESP2_Garage")) {
  99. That should solve your MQTT multiple connections problem
  100. */
  101. if (client.connect("sonoffxxX")) {
  102. Serial.println("connected");
  103. // Subscribe or resubscribe to a topic
  104. // You can subscribe to more topics (to control more sonoffxxXs)
  105. client.subscribe("home/office/sonoffxxX");
  106. } else {
  107. Serial.print("failed, rc=");
  108. Serial.print(client.state());
  109. Serial.println(" try again in 5 seconds");
  110. // Wait 5 seconds before retrying
  111. delay(5000);
  112. }
  113. }
  114. }
  115.  
  116. // The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 115200
  117. // Sets your mqtt broker and sets the callback function
  118. // The callback function is what receives messages and actually controls the LEDs
  119. void setup() {
  120. // preparing GPIOs
  121. pinMode(gpio13Led, OUTPUT);
  122. digitalWrite(gpio13Led, HIGH);
  123.  
  124. pinMode(gpio12Relay, OUTPUT);
  125. digitalWrite(gpio12Relay, HIGH);
  126.  
  127. Serial.begin(115200);
  128. setup_wifi();
  129. client.setServer(mqtt_server, 1883);
  130. client.setCallback(callback);
  131. }
  132.  
  133. // For this project, you don't need to change anything in the loop function.
  134. // Basically it ensures that you ESP is connected to your broker
  135. void loop() {
  136.  
  137. if (!client.connected()) {
  138. reconnect();
  139. }
  140. if(!client.loop())
  141. client.connect("sonoffxxX");
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement