Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <MQTTClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266HTTPUpdateServer.h>
- const char* ssid = "XXXX";
- const char* password = "XXXX";
- const char* host = "MJ-LAMP";
- const char* update_path = "/firmware";
- const char* update_username = "xxxx";
- const char* update_password = "xxxx";
- char* subscribeTopic = "MJ/Light/MJ-LAMP";
- const char* server = "192.168.X.X";
- const char* mqttDeviceID = "MJ-LAMP";
- #define LAMP D2
- ESP8266WebServer httpServer(80);
- ESP8266HTTPUpdateServer httpUpdater;
- WiFiClient net;
- MQTTClient client;
- unsigned long lastMillis = 0;
- void connect();
- void setup()
- {
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- client.begin(server, net);
- client.onMessage(messageReceived);
- connect();
- Serial.begin(115200);
- MDNS.begin(host);
- httpUpdater.setup(&httpServer, update_path, update_username, update_password);
- httpServer.begin();
- MDNS.addService("http", "tcp", 80);
- pinMode(LAMP,OUTPUT);
- analogWrite(LAMP,1023);
- Serial.println("setup finished");
- }
- void connect()
- {
- while (WiFi.status() != WL_CONNECTED) { delay(1000); }
- while (!client.connect(mqttDeviceID)) { delay(1000); }
- client.subscribe(subscribeTopic);
- }
- void loop()
- {
- client.loop();
- delay(10);
- if(!client.connected()) { connect(); }
- httpServer.handleClient();
- }
- void messageReceived(String &topic, String &payload)
- {
- String stringOne = payload;
- Serial.println(stringOne);
- int val = map(payload.toInt(), 0, 100, 0, 1023);
- analogWrite(LAMP,val);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement