#include #include // WARNING //GPIO2 and GPIO0 can’t be set to LOW at boot time, do not change!!! // GPIO0 relay7 is uncommented in this sketch // PAY ATTENTION // Actual Pin numbers D0-D16 dont match GPIO numbers // NOT all GPIO pins can be used on ESP8266-12E board // Change the credentials below, so your ESP8266 connects to your router const char* ssid = "YOUR-SSID"; const char* password = "YOUR-PASSWORD"; // Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker const char* mqtt_server = "IP OR HOST NAME OF YOUR MQTT BROKER"; // Initializes the espClient. You should change the espClient name if you have multiple ESPs running in your home automation system WiFiClient espClient; PubSubClient client(espClient); // relay1 - GPIO 5 = D1 on ESP-12E NodeMCU board const int relay1 = 5; // relay2 - GPIO 4 = D2 on ESP-12E NodeMCU board const int relay2 = 4; // relay3 - GPIO 14 = D5 on ESP-12E NodeMCU board const int relay3 = 14; // relay4 - GPIO 12 = D6 on6 ESP-12E NodeMCU board const int relay4 = 12; // relay5 - GPIO 13 = D7 on ESP-12E NodeMCU board const int relay5 = 13; // GPIO2 can’t be LOW at boot, do not change!!! // Add pull up resistor in range 2K-10K on pins 3V3 and D4 // Relay6 will activate for 100msec during boot // relay6 - GPIO 2 = D4 on ESP-12E NodeMCU board const int relay6 = 2; // GPIO0 can’t be LOW at boot, do not change!!! // Add pull up resistor in range 2K-10K on pins 3V3 and D3 // WARNING: GPIO0 is needs to be grounded to get into programming mode. // If your sketch is driving it HIGH, grounding it can damage you ESP8266 chip. // The safe way to reprogram the ESP8266 when your code drives the GPIO0 output is to // a) Power down the board // b) short GPIO0 to gnd // c) power up the board which goes into program mode due to the short on GPIO0 // d) remove the short from GPIO0 so you don't short out the output when the program runs // e) reprogram the board // f) power cycle the board if necessary. // Credits to Matthew Ford https://www.forward.com.au/pfod/ESP8266/GPIOpins/index.html // relay7 - GPIO 0 = D3 on ESP-12E NodeMCU board // Uncomment command below at your own risk. // const int relay6 = 0; // Timers auxiliar variables long now = millis(); long lastMeasure = 0; // Don't change the function below. This functions connects your ESP8266 to your router void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(".");} Serial.println(""); Serial.print("WiFi connected - ESP IP address: "); Serial.println(WiFi.localIP()); } // This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to // Change the function below to add logic to your program, so when a device publishes a message to a topic that // your ESP8266 is subscribed you can actually do something void callback(String topic, byte* message, unsigned int length) { Serial.print("Message arrived on topic: "); Serial.print(topic); Serial.print(". Message: "); String messageTemp; for (int i = 0; i < length; i++) { Serial.print((char)message[i]); messageTemp += (char)message[i]; } Serial.println(); // Feel free to add more if statements to control more GPIOs with MQTT // Topic esp01/relay1 // Turns GPIO5 according to the message if(topic=="esp01/relay1"){ Serial.print("Changing esp01 relay1 to "); if(messageTemp == "on"){ digitalWrite(relay1, LOW); Serial.print("On"); } else if(messageTemp == "off"){ digitalWrite(relay1, HIGH); Serial.print("Off"); } } Serial.println(); // Topic esp01/relay2 // Turns the GPIO4 according to the message if(topic=="esp01/relay2"){ Serial.print("Changing esp01 relay2 to "); if(messageTemp == "on"){ digitalWrite(relay2, LOW); Serial.print("On"); } else if(messageTemp == "off"){ digitalWrite(relay2, HIGH); Serial.print("Off"); } } Serial.println(); // Topic esp01/relay3 // Turns the relay3 GPIO14 according to the message if(topic=="esp01/relay3"){ Serial.print("Changing esp01 relay3 to "); if(messageTemp == "on"){ digitalWrite(relay3, LOW); Serial.print("On"); } else if(messageTemp == "off"){ digitalWrite(relay3, HIGH); Serial.print("Off"); } } Serial.println(); // Topic esp01/relay4 // Turns the relay4 GPIO12 according to the message if(topic=="esp01/relay4"){ Serial.print("Changing esp01 relay4 to "); if(messageTemp == "on"){ digitalWrite(relay4, LOW); Serial.print("On"); } else if(messageTemp == "off"){ digitalWrite(relay4, HIGH); Serial.print("Off"); } } Serial.println(); // Topic esp01/relay5 // Turns the relay5 GPIO13 according to the message if(topic=="esp01/relay5"){ Serial.print("Changing esp01 relay5 to "); if(messageTemp == "on"){ digitalWrite(relay5, LOW); Serial.print("On"); } else if(messageTemp == "off"){ digitalWrite(relay5, HIGH); Serial.print("Off"); } } Serial.println(); //GPIO2 can’t be LOW at boot, do not change!!! // Topic esp01/relay6 // Turns the relay6 GPIO2 according to the message if(topic=="esp01/relay6"){ Serial.print("Changing esp01 relay6 to "); if(messageTemp == "on"){ digitalWrite(relay6, LOW); Serial.print("On"); } else if(messageTemp == "off"){ digitalWrite(relay6, HIGH); Serial.print("Off"); } } Serial.println(); //GPIO0 can’t be LOW at boot, do not change!!! // Topic esp01/relay7 // Turns the relay7 GPIO0 according to the message //if(topic=="esp01/relay7"){ // Serial.print("Changing esp01 relay7 to "); // if(messageTemp == "on"){ // digitalWrite(relay7, LOW); // Serial.print("On"); // } // else if(messageTemp == "off"){ // digitalWrite(relay7, HIGH); // Serial.print("Off"); // } // } // Serial.println(); } // This functions reconnects your ESP8266 to your MQTT broker // Change the function below if you want to subscribe to more topics with your ESP8266 void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect /* YOU MIGHT NEED TO CHANGE THIS LINE, IF YOU'RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS To change the ESP device ID, you will have to give a new name to the ESP8266. */ if (client.connect("ESP8266Client")) { Serial.println("connected"); // Subscribe or resubscribe to a topic // You can subscribe to more topics (to control more LEDs in this example) client.subscribe("esp01/relay1"); client.subscribe("esp01/relay2"); client.subscribe("esp01/relay3"); client.subscribe("esp01/relay4"); client.subscribe("esp01/relay5"); client.subscribe("esp01/relay6"); //client.subscribe("esp01/relay7"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } // The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 115200 // Sets your mqtt broker and sets the callback function // The callback function is what receives messages and actually controls the LEDs void setup() { pinMode(relay1, OUTPUT); digitalWrite(relay1, HIGH); pinMode(relay2, OUTPUT); digitalWrite(relay2, HIGH); pinMode(relay3, OUTPUT); digitalWrite(relay3, HIGH); pinMode(relay4, OUTPUT); digitalWrite(relay4, HIGH); pinMode(relay5, OUTPUT); digitalWrite(relay5, HIGH); //GPIO2 can’t be LOW at boot, do not change!!! pinMode(relay6, OUTPUT); digitalWrite(relay6, HIGH); //GPIO0 can’t be LOW at boot, do not change!!! // pinMode(relay7, OUTPUT); // digitalWrite(relay7, HIGH); Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); } // For this project, you don't need to change anything in the loop function. Basically it ensures that you ESP is connected to your broker void loop() { if (!client.connected()) { reconnect(); } if(!client.loop()) client.connect("ESP8266Client"); now = millis(); }