Advertisement
MichalCzEJ

Untitled

Oct 7th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 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.  
  7.  
  8. const char* ssid = "XXXX";
  9. const char* password = "XXXX";
  10. const char* host = "MJ-LAMP";
  11. const char* update_path = "/firmware";
  12.  
  13. const char* update_username = "xxxx";
  14. const char* update_password = "xxxx";
  15. char* subscribeTopic = "MJ/Light/MJ-LAMP";
  16. const char* server = "192.168.X.X";
  17. const char* mqttDeviceID = "MJ-LAMP";
  18.  
  19. #define LAMP D2
  20.  
  21.  
  22. ESP8266WebServer httpServer(80);
  23. ESP8266HTTPUpdateServer httpUpdater;
  24.  
  25. WiFiClient net;
  26. MQTTClient client;
  27.  
  28. unsigned long lastMillis = 0;
  29.  
  30. void connect();
  31.  
  32. void setup()
  33. {
  34. WiFi.mode(WIFI_STA);
  35. WiFi.begin(ssid, password);
  36. client.begin(server, net);
  37. client.onMessage(messageReceived);
  38. connect();
  39. Serial.begin(115200);
  40. MDNS.begin(host);
  41.  
  42. httpUpdater.setup(&httpServer, update_path, update_username, update_password);
  43. httpServer.begin();
  44.  
  45. MDNS.addService("http", "tcp", 80);
  46. pinMode(LAMP,OUTPUT);
  47. analogWrite(LAMP,1023);
  48. Serial.println("setup finished");
  49. }
  50.  
  51. void connect()
  52. {
  53. while (WiFi.status() != WL_CONNECTED) { delay(1000); }
  54.  
  55. while (!client.connect(mqttDeviceID)) { delay(1000); }
  56. client.subscribe(subscribeTopic);
  57. }
  58.  
  59. void loop()
  60. {
  61. client.loop();
  62. delay(10);
  63. if(!client.connected()) { connect(); }
  64. httpServer.handleClient();
  65. }
  66.  
  67. void messageReceived(String &topic, String &payload)
  68. {
  69. String stringOne = payload;
  70. Serial.println(stringOne);
  71. int val = map(payload.toInt(), 0, 100, 0, 1023);
  72. analogWrite(LAMP,val);
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement