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>
- //#include <PubSubClient.h>
- /* ---------- DO NOT EDIT ANYTHING ABOVE THIS LINE ---------- */
- //Only edit the settings in this section
- /* WIFI Settings */
- // Name of wifi network
- const char* ssid = "xxxxxxxxxx";
- //const char* ssid = "xxxxxxxxxxxx";
- // Password to wifi network
- const char* password = "xxxxxx";
- //const char* password = "xxxxxxxxxxxxx";
- /* Web Updater Settings */
- // Host Name of Device
- const char* host = "MK-SprinklerSystem1";
- // Path to access firmware update page (Not Neccessary to change)
- const char* update_path = "/firmware";
- // Username to access the web update page
- const char* update_username = "xxxxxxxxxx";
- // Password to access the web update page
- const char* update_password = "xxxxxxxxxxxxxx";
- /* MQTT Settings */
- // Topic which listens for commands
- char* subscribeTopic = "MK-SmartHouse/utilities/MK-SprinklerSystem1";
- //MQTT Server IP Address
- const char* server = "192.168.43.173";
- //#define server "192.168.43.173"
- //Unique device ID
- const char* mqttDeviceID = "MK-SmartHouseDevice4";
- //PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);
- /* ---------- DO NOT EDIT ANYTHING BELOW THIS LINE ---------- */
- //int channel1 = 0;
- //int channel2 = 2;
- #define channel1 0
- #define channel2 2
- //int channel3 = 13;
- //webserver
- ESP8266WebServer httpServer(80);
- ESP8266HTTPUpdateServer httpUpdater;
- //MQTT
- WiFiClient net;
- MQTTClient client;
- unsigned long lastMillis = 0;
- //Connect to WiFI and MQTT
- void connect();
- //Setup pins, wifi, webserver and MQTT
- void setup()
- {
- // set pin modes
- pinMode(channel1, OUTPUT);
- digitalWrite(channel1, LOW);
- pinMode(channel2, OUTPUT);
- digitalWrite(channel2, LOW);
- pinMode(channel3, OUTPUT);
- digitalWrite(channel3, LOW);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- client.begin(server, net);
- connect();
- MDNS.begin(host);
- httpUpdater.setup(&httpServer, update_path, update_username, update_password);
- httpServer.begin();
- MDNS.addService("http", "tcp", 80);
- }
- //Connect to wifi and MQTT
- void connect()
- {
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(1000);
- }
- while (!client.connect(mqttDeviceID))
- {
- delay(1000);
- }
- client.subscribe(subscribeTopic);
- }
- void loop()
- {
- // MQTT Loop
- client.loop();
- delay(10);
- // Make sure device is connected
- if(!client.connected())
- {
- connect();
- }
- httpServer.handleClient();
- }
- // Change the state of a relay based on the MQTT Message
- void messageReceived(String topic, String payload, char * bytes, unsigned int length)
- {
- String msgString = payload;
- if (msgString == "Z1ON")
- {
- digitalWrite(channel1, HIGH);
- delay(250);
- }
- else if (msgString == "Z1OFF")
- {
- digitalWrite(channel1, LOW);
- delay(250);
- }
- else if (msgString == "Z2ON")
- {
- digitalWrite(channel2, HIGH);
- delay(250);
- }
- else if (msgString == "Z2OFF")
- {
- digitalWrite(channel2, LOW);
- delay(250);
- }
- else if (msgString == "Z3ON")
- {
- digitalWrite(channel3, HIGH);
- delay(250);
- }
- else if (msgString == "Z3OFF")
- {
- digitalWrite(channel3, LOW);
- delay(250);
- }
- }
Add Comment
Please, Sign In to add comment