Guest User

Untitled

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