Guest User

Untitled

a guest
Jul 18th, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. /*
  2. * Device Title: MK-SprinklerSystem
  3. * Device Description: MQTT Sprinkler System Control
  4. * Device Explanation: The MQTT server sends a message to the device and
  5. * based on the message the device turns on a relay, which
  6. * is attached to a sprinkler valve
  7. * Device information: https://www.MK-SmartHouse.com/door-sensor
  8. *
  9. * Author: Matt Kaczynski
  10. * Website: http://www.MK-SmartHouse.com
  11. *
  12. * Code may only be distrbuted through http://www.MK-SmartHouse.com any other methods
  13. * of obtaining or distributing are prohibited
  14. * Copyright (c) 2016-2017
  15. *
  16. * Note: After flashing the code once you can remotely access your device by going to http://HOSTNAMEOFDEVICE.local/firmware
  17. * obviously replace HOSTNAMEOFDEVICE with whatever you defined below. The user name and password are also defined below.
  18. */
  19.  
  20. #include <ESP8266WiFi.h>
  21. #include <MQTTClient.h>
  22. #include <ESP8266WebServer.h>
  23. #include <ESP8266mDNS.h>
  24. #include <ESP8266HTTPUpdateServer.h>
  25.  
  26. /* ---------- DO NOT EDIT ANYTHING ABOVE THIS LINE ---------- */
  27.  
  28. //Only edit the settings in this section
  29.  
  30. /* WIFI Settings */
  31. // Name of wifi network
  32. const char* ssid = "***********";
  33.  
  34. // Password to wifi network
  35. const char* password = "*************";
  36.  
  37. /* Web Updater Settings */
  38. // Host Name of Device
  39. const char* host = "MK-SprinklerSystem1";
  40.  
  41. // Path to access firmware update page (Not Neccessary to change)
  42. const char* update_path = "/firmware";
  43.  
  44. // Username to access the web update page
  45. const char* update_username = "admin";
  46.  
  47. // Password to access the web update page
  48. const char* update_password = "password";
  49.  
  50. /* MQTT Settings */
  51. // Topic which listens for commands
  52. char* subscribeTopic = "MK-SmartHouse/utilities/MK-SprinklerSystem1";
  53.  
  54. //MQTT Server IP Address
  55. const char* server = "******************";
  56.  
  57. //Unique device ID
  58. const char* mqttDeviceID = "MK-SmartHouseDevice1";
  59.  
  60.  
  61. /* ---------- DO NOT EDIT ANYTHING BELOW THIS LINE ---------- */
  62.  
  63. int channel1 = 14;
  64. int channel2 = 12;
  65. int channel3 = 13;
  66.  
  67. //webserver
  68. ESP8266WebServer httpServer(80);
  69. ESP8266HTTPUpdateServer httpUpdater;
  70.  
  71. //MQTT
  72. WiFiClient net;
  73. MQTTClient client;
  74.  
  75. unsigned long lastMillis = 0;
  76.  
  77. //Connect to WiFI and MQTT
  78. void connect();
  79.  
  80. //Setup pins, wifi, webserver and MQTT
  81. void setup()
  82. {
  83. // set pin modes
  84. pinMode(channel1, OUTPUT);
  85. digitalWrite(channel1, LOW);
  86.  
  87. pinMode(channel2, OUTPUT);
  88. digitalWrite(channel2, LOW);
  89.  
  90. pinMode(channel3, OUTPUT);
  91. digitalWrite(channel3, LOW);
  92.  
  93. WiFi.mode(WIFI_STA);
  94.  
  95. WiFi.begin(ssid, password);
  96. client.begin(server, net);
  97. client.onMessage(messageReceived);
  98.  
  99. connect();
  100.  
  101. MDNS.begin(host);
  102.  
  103. httpUpdater.setup(&httpServer, update_path, update_username, update_password);
  104. httpServer.begin();
  105.  
  106. MDNS.addService("http", "tcp", 80);
  107. }
  108.  
  109. //Connect to wifi and MQTT
  110. void connect()
  111. {
  112. while (WiFi.status() != WL_CONNECTED)
  113. {
  114. delay(1000);
  115. }
  116.  
  117. while (!client.connect(mqttDeviceID))
  118. {
  119. delay(1000);
  120. }
  121.  
  122. client.subscribe(subscribeTopic);
  123. }
  124.  
  125. void loop()
  126. {
  127. // MQTT Loop
  128. client.loop();
  129. delay(10);
  130.  
  131. // Make sure device is connected
  132. if(!client.connected())
  133. {
  134. connect();
  135. }
  136.  
  137. httpServer.handleClient();
  138.  
  139. }
  140.  
  141. // Change the state of a relay based on the MQTT Message
  142. void messageReceived(String &topic, String &payload)
  143. {
  144. String msgString = payload;
  145.  
  146. if (msgString == "Z1ON")
  147. {
  148. digitalWrite(channel1, HIGH);
  149. delay(250);
  150. }
  151. else if (msgString == "Z1OFF")
  152. {
  153. digitalWrite(channel1, LOW);
  154. delay(250);
  155. }
  156. else if (msgString == "Z2ON")
  157. {
  158. digitalWrite(channel2, HIGH);
  159. delay(250);
  160. }
  161. else if (msgString == "Z2OFF")
  162. {
  163. digitalWrite(channel2, LOW);
  164. delay(250);
  165. }
  166. else if (msgString == "Z3ON")
  167. {
  168. digitalWrite(channel3, HIGH);
  169. delay(250);
  170. }
  171. else if (msgString == "Z3OFF")
  172. {
  173. digitalWrite(channel3, LOW);
  174. delay(250);
  175. }
  176. }
Add Comment
Please, Sign In to add comment