#include <WiFi.h>
#include <PubSubClient.h>
// Change the credentials below, so your ESP8266 connects to your router
const char* ssid = "Wokwi-GUEST";
const char* password = "";
/*
YOU MIGHT NEED TO CHANGE MQTT_client_id, 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.
For instance, you could name one of the ESP devices:
const char* MQTT_client_id = "ESP32_COLOR_LEDS";
Then, you can use another name for another ESP device:
const char* MQTT_client_id = "ESP1_GARAGE";
Then, for the other ESP:
const char* MQTT_client_id = "ESP3_ROOM";
That should solve your MQTT multiple connections problem
*/
const char* MQTT_client_id = "ESP32_COLOR_LEDS";
// MQTT broker credentials (set to NULL if not required)
const char* MQTT_username = "";
const char* MQTT_password = "";
// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
const char* mqtt_server = "mqtt-dashboard.com";
// 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);
const int REDPin = 4;
const int GREENPin = 0;
const int BLUEPin = 2;
const int ORNGPin = 15;
// Timers auxiliar variables
long now = millis();
long lastMeasure = 0;
// 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 function 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 ESP device 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();
// If a message is received on the topic room/lamp, you check if the message is either on or off. Turns the lamp GPIO according to the message
if(topic=="lights/red"){
Serial.print("Changing Red LED to ");
if(messageTemp == "on"){
digitalWrite(REDPin, HIGH);
Serial.print("On");
}
else if(messageTemp == "off"){
digitalWrite(REDPin, LOW);
Serial.print("Off");
}
}
if(topic=="lights/green"){
Serial.print("Changing Green LED to ");
if(messageTemp == "on"){
digitalWrite(GREENPin, HIGH);
Serial.print("On");
}
else if(messageTemp == "off"){
digitalWrite(GREENPin, LOW);
Serial.print("Off");
}
}
if(topic=="lights/blue"){
Serial.print("Changing Blue LED to ");
if(messageTemp == "on"){
digitalWrite(BLUEPin, HIGH);
Serial.print("On");
}
else if(messageTemp == "off"){
digitalWrite(BLUEPin, LOW);
Serial.print("Off");
}
}
if(topic=="lights/orange"){
Serial.print("Changing Orange LED to ");
if(messageTemp == "on"){
digitalWrite(ORNGPin, HIGH);
Serial.print("On");
}
else if(messageTemp == "off"){
digitalWrite(ORNGPin, LOW);
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
if (client.connect(MQTT_client_id, MQTT_username, MQTT_password)) {
Serial.println("connected");
// Subscribe or resubscribe to a topic
// You can subscribe to more topics (to control more LEDs in this example)
client.subscribe("lights/red");
client.subscribe("lights/green");
client.subscribe("lights/blue");
client.subscribe("lights/orange");
} 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(REDPin, OUTPUT);
pinMode(GREENPin, OUTPUT);
pinMode(BLUEPin, OUTPUT);
pinMode(ORNGPin, OUTPUT);
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()) {
Serial.println("Client lost connection - Reconnecting");
reconnect();
}
if(!client.loop())
client.connect(MQTT_client_id, MQTT_username, MQTT_password);
delay(1000);
}