Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Device Title: MK-SprinklerSystem
- * Device Description: MQTT Sprinkler System Control
- * Device Explanation: The MQTT server sends a message to the device and
- * based on the message the device turns on a relay, which
- * is attached to a sprinkler valve
- * Device information: https://www.MK-SmartHouse.com/door-sensor
- *
- * Author: Matt Kaczynski
- * Website: http://www.MK-SmartHouse.com
- *
- * Code may only be distrbuted through http://www.MK-SmartHouse.com any other methods
- * of obtaining or distributing are prohibited
- * Copyright (c) 2016-2017
- *
- * Note: After flashing the code once you can remotely access your device by going to http://HOSTNAMEOFDEVICE.local/firmware
- * obviously replace HOSTNAMEOFDEVICE with whatever you defined below. The user name and password are also defined below.
- */
- #include <ESP8266WiFi.h>
- #include <MQTTClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266HTTPUpdateServer.h>
- /* ---------- DO NOT EDIT ANYTHING ABOVE THIS LINE ---------- */
- //Only edit the settings in this section
- /* WIFI Settings */
- // Name of wifi network
- const char* ssid = "***********";
- // Password to wifi network
- const char* password = "*************";
- /* 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 = "admin";
- // Password to access the web update page
- const char* update_password = "password";
- /* MQTT Settings */
- // Topic which listens for commands
- char* subscribeTopic = "MK-SmartHouse/utilities/MK-SprinklerSystem1";
- //MQTT Server IP Address
- const char* server = "******************";
- //Unique device ID
- const char* mqttDeviceID = "MK-SmartHouseDevice1";
- /* ---------- DO NOT EDIT ANYTHING BELOW THIS LINE ---------- */
- int channel1 = 14;
- int channel2 = 12;
- 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);
- client.onMessage(messageReceived);
- 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)
- {
- 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