Guest User

Untitled

a guest
Dec 24th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. #include <PubSubClient.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ArduinoOTA.h>
  4.  
  5. // NOTE: Source code is based on nummerous other projects, it's not my own, neither is it clearly from one other author.
  6. // Considering it GPL would be best I assume.
  7.  
  8. #define D0 16 //WAKE => 16
  9. #define D1 5 //IOS => 5
  10. #define D2 4 // => 4
  11. #define D3 0 // => 0
  12. #define D4 2 // => 2
  13. #define D5 14 //CLK => 14
  14. #define D6 12 //MISO => 12
  15. #define D7 13 //MOSI => 13
  16. #define D8 15 //CS => 15
  17. #define D9 3 //RX => 3
  18. #define RELAY_PIN D6 //D6 == default on SonOFF
  19. #define TOGGLE_PIN D3 //D3 == default on SonOFF
  20. #define LED_PIN D7 //D7 == default on SonOFF
  21.  
  22. #define MQTT_VERSION MQTT_VERSION_3_1_1
  23. #define MQTT_SERVER "192.168.XXXX.XXXX" //you MQTT IP Address
  24. const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
  25. const PROGMEM char* MQTT_CLIENT_ID = "sonoff";
  26. const char* MQTT_LIGHT_STATE_TOPIC = "/house/switchConfirm6/";
  27. const char* MQTT_LIGHT_COMMAND_TOPIC = "/house/switch6/";
  28. boolean m_relay_state = false;
  29. boolean fDebug = true;
  30. long lastMsg = 0;
  31. long unsigned int pause = 5000;
  32. int chk;
  33. int TOGGLE_COUNT = 0;
  34.  
  35. void callback(char* topic, byte* payload, unsigned int length);
  36. void setRelay(char payload);
  37.  
  38. const char* ssid = "bergman_U";
  39. const char* password = "gastvrij";
  40. const char* mqtt_username= "mqtt_username";
  41. const char* mqtt_password = "XXXXSECRETHEREXXXX";
  42.  
  43.  
  44. WiFiClient wifiClient;
  45. PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);
  46.  
  47. void setup() {
  48. //initialize the switch as an output and set to HIGH (on) (default)
  49. pinMode(LED_PIN, OUTPUT); // Led 1
  50. digitalWrite(LED_PIN, HIGH); // Led 1
  51. pinMode(RELAY_PIN, OUTPUT); // Relay Switch 1
  52. digitalWrite(RELAY_PIN, LOW); // Relay Switch 1
  53. pinMode(TOGGLE_PIN, INPUT); // Toggle Button
  54.  
  55. ArduinoOTA.setHostname("My Arduino SONOFF"); // A name given to your ESP8266 module when discovering it as a port in ARDUINO IDE
  56. ArduinoOTA.begin(); // OTA initialization
  57.  
  58. //start the serial line for debugging
  59. Serial.begin(115200);
  60. delay(100);
  61.  
  62. //start wifi subsystem
  63. WiFi.begin(ssid, password);
  64. //attempt to connect to the WIFI network and then connect to the MQTT server
  65. reconnect();
  66.  
  67. //wait a bit before starting the main loop
  68. delay(2000);
  69. }
  70.  
  71. void loop(){
  72. //reconnect if connection is lost
  73. if (!client.connected() && WiFi.status() == 3) {reconnect();}
  74. // insert button actions here;
  75.  
  76. //check for button actions
  77. if (digitalRead(TOGGLE_PIN) == LOW) {
  78. m_relay_state = !m_relay_state; // switch state and setRelay accordingly
  79. if(m_relay_state == true){
  80. setRelay('1');
  81. }else{
  82. setRelay('0');
  83. }
  84. delay(1000);
  85. }
  86.  
  87. //maintain MQTT connection
  88. client.loop();
  89.  
  90. //MUST delay to allow ESP8266 WIFI functions to run
  91. delay(20);
  92. ArduinoOTA.handle();
  93. }
  94.  
  95. void setRelay(char payload){
  96. if(payload == '1'){
  97. Serial.print("Set 1");
  98. digitalWrite(RELAY_PIN, HIGH);
  99. digitalWrite(LED_PIN, LOW);
  100. m_relay_state = true;
  101. client.publish(MQTT_LIGHT_STATE_TOPIC, "1");
  102. }
  103. //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
  104. else if (payload == '0'){
  105. Serial.print("Set 0");
  106. digitalWrite(RELAY_PIN, LOW);
  107. digitalWrite(LED_PIN, HIGH);
  108. m_relay_state = false;
  109. client.publish(MQTT_LIGHT_STATE_TOPIC, "0");
  110. }
  111. }
  112.  
  113. void callback(char* topic, byte* payload, unsigned int length) {
  114. //convert topic to string to make it easier to work with
  115. String topicStr = topic;
  116. //EJ: Note: the "topic" value gets overwritten everytime it receives confirmation (callback) message from MQTT
  117.  
  118. //Print out some debugging info
  119. Serial.println("Callback update.");
  120. Serial.print("Topic: ");
  121. Serial.println(topicStr);
  122. Serial.print("Payload: ");
  123. Serial.println(payload[0]);
  124.  
  125. if (topicStr == MQTT_LIGHT_COMMAND_TOPIC){
  126. Serial.print(MQTT_LIGHT_COMMAND_TOPIC);
  127. //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
  128. setRelay(payload[0]);
  129. }
  130. }
  131.  
  132. void reconnect() {
  133. //attempt to connect to the wifi if connection is lost
  134. if(WiFi.status() != WL_CONNECTED){
  135. //debug printing
  136. Serial.print("Connecting to ");
  137. Serial.println(ssid);
  138.  
  139. //loop while we wait for connection
  140. while (WiFi.status() != WL_CONNECTED) {
  141. delay(500);
  142. Serial.print(".");
  143. }
  144.  
  145. //print out some more debug once connected
  146. Serial.println("");
  147. Serial.println("WiFi connected");
  148. Serial.println("IP address: ");
  149. Serial.println(WiFi.localIP());
  150. }
  151.  
  152. //make sure we are connected to WIFI before attemping to reconnect to MQTT
  153. if(WiFi.status() == WL_CONNECTED){
  154. // Loop until we're reconnected to the MQTT server
  155. while (!client.connected()) {
  156. Serial.print("Attempting MQTT connection...");
  157.  
  158. // Generate client name based on MAC address and last 8 bits of microsecond counter
  159. String clientName;
  160. clientName += "esp8266-";
  161. uint8_t mac[6];
  162. WiFi.macAddress(mac);
  163. clientName += macToStr(mac);
  164.  
  165. //if connected, subscribe to the topic(s) we want to be notified about
  166. //EJ: Delete "mqtt_username", and "mqtt_password" here if you are not using any
  167. if (client.connect((char*) clientName.c_str(),mqtt_username, mqtt_password)) { //EJ: Update accordingly with your MQTT account
  168. Serial.print("\tMQTT Connected");
  169. client.subscribe(MQTT_LIGHT_COMMAND_TOPIC);
  170. //EJ: Do not forget to replicate the above line if you will have more than the above number of relay switches
  171. }
  172.  
  173. //otherwise print failed for debugging
  174. else{Serial.println("\tFailed."); abort();}
  175. }
  176. }
  177. }
  178.  
  179. //generate unique name from MAC addr
  180. String macToStr(const uint8_t* mac){
  181. String result;
  182. for (int i = 0; i < 6; ++i) {
  183. result += String(mac[i], 16);
  184.  
  185. if (i < 5){
  186. result += ':';
  187. }
  188. }
  189. return result;
  190. }
Add Comment
Please, Sign In to add comment