Guest User

Untitled

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