In this project, you are going to build a simple home appliances control for an LED connected to an ESP32 using a switch on the Node-RED Dashboard. Your ESP32 will subscribe to an MQTT topic, receive “true” or “false” commands from Node-RED, and toggle the LED state in real-time.
This exercise teaches you:
- How to use a GPIO pin on ESP32 to control an LED
- How to configure Node-RED to publish messages using a Dashboard Switch
- How to subscribe to an MQTT topic on ESP32
- How to process received MQTT commands to control a physical output
Checklist Before Start
- How to use a GPIO pin with ESP32
- How to connect ESP32 with WiFi
- How to subscribe to sensor data using MQTT
- How to use the Node-RED Dashboard Switch node
NODE-RED Setup
Download Node-RED Desktop
Firstly before you start, you need to download the Node-RED Desktop first in your PC or laptop. Just click this link (https://vitormiao.com/Node-RED-Installer) and follow the step by step.
- Click “Download”

2. Click “More info”

3. Click “Run Anyway”

4. Click “Next”

5. Click “Next”

6. Click “Next”

7. Click “Install”

8. Click “Finish”

Setup Node-RED
After downloading Node-RED Desktop, you can set up the flow to send commands to the ESP32. Open Node-RED and follow these steps:
Step 1: Install Node-RED Dashboard Setup
Go to the Menu at the top-right corner and click Manage Palette.
- Click Install and search “node-red-dashboard”. Install the palette (if not already installed).
Step 2: Add Switch Node
From the dashboard section in the left palette, drag a Switch node into the flow.

Step 3: Configure Switch Node and Add Dashboard Group
- Double-click the Switch node.
2. Click the “pencil” icon to add a new Dashboard tab.

3. In the Switch properties, under Group, click the “+” icon to add a new Dashboard group.
- Insert the Name as “Control”
- Click “pencil” icon
- Name: Home
- Click Add
- Click Update


4. Configure the Switch:
- Group: Select “[Home] Control”
- Label: LED Switch
- On Payload: true (boolean)
- Off Payload: false (boolean)
- Click Done

Step 4: Add MQTT-out Node

- Drag an MQTT-out node into the flow
- Connect the Switch node with MQTT-out node
- Double click the MQTT-out node
- Click the “+” button to add a new MQTT broker (or select HiveMQ if you already configured it)
- Name : HiveMQ
- Server : broker.hivemq.com
- Click Update

5. Configure the MQTT-out node:
- Server : (choose) HiveMQ
- Topic : your_name/led/control (make sure it same with your code)
- Click Done

Step 5: Deploy and view Dashboard
- Click Deploy

- Click the icon beside the setting and select Dashboard to view your new switch interface.
- Click arrow icon


Circuit Connections

| Components | ESP32 Dev Module |
| Blue LED (any LED) | IO2 |
| LCD 16X2 SDA | SDA |
| LCD 16×2 SCL | SCL |
Logic Flow

Code Lab
Step 1: Install Library
Open the Library Manager at the sidebar and install the following library:
- PubSubClient by Nick O’Leary
- LiquidCrystal_I2C.h by Martin Kubavcok

Step 2: Code
Copy and paste the code in Arduino IDE.
#include <WiFi.h>
#include <PubSubClient.h>
#include <LiquidCrystal_I2C.h>
// --- WiFi Configuration ---
const char* ssid = "YOUR_WIFI_NAME"; // Your WiFi SSID
const char* password = "YOUR_WIFI_PASSWORD"; // Your WiFi Password
// --- MQTT Configuration ---
const char* mqtt_server = "broker.hivemq.com"; // Use your broker IP or hostname
const char* mqtt_topic_sub = "your_name/led/control"; // your_name/led/control
const long ledPin = 2; // The LED pin GPIO2
// --- LCD Configuration ---
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns, 2 rows
WiFiClient espClient;
PubSubClient client(espClient);
// Function to connect to WiFi
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// Display on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi Connecting");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Display WiFi connected on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi Connected");
lcd.setCursor(0, 1);
lcd.print(WiFi.localIP());
delay(2000);
}
// The function called when a message is received on a subscribed topic
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message received [");
Serial.print(topic);
Serial.print("]: ");
// Convert the incoming payload (byte array) to a String
String messageTemp;
for (int i = 0; i < length; i++) {
messageTemp += (char)payload[i];
}
Serial.println(messageTemp);
// Check the subscribed topic
if (String(topic) == mqtt_topic_sub) {
Serial.print("Control Command: ");
// Check the message content to control the LED
if (messageTemp == "true" || messageTemp == "1") {
Serial.println("LED ON");
digitalWrite(ledPin, HIGH);
// Display LED status on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MQTT: Connected");
lcd.setCursor(0, 1);
lcd.print("LED: ON");
} else if (messageTemp == "false" || messageTemp == "0") {
Serial.println("LED OFF");
digitalWrite(ledPin, LOW);
// Display LED status on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MQTT: Connected");
lcd.setCursor(0, 1);
lcd.print("LED: OFF");
}
}
}
// Function to reconnect to the MQTT broker
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Display MQTT connecting on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MQTT Connecting");
// Create a random client ID
String clientId = "ESPClient_your_name-"; // put your name in it
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, SUBSCRIBE to the control topic
client.subscribe(mqtt_topic_sub);
Serial.print("Subscribed to topic: ");
Serial.println(mqtt_topic_sub);
// Display MQTT connected on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MQTT: Connected");
lcd.setCursor(0, 1);
lcd.print("LED: OFF");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" trying again in 5 seconds");
// Display MQTT failed on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MQTT Failed");
lcd.setCursor(0, 1);
lcd.print("Retry in 5s");
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ESP32 Starting");
delay(1000);
setup_wifi();
client.setServer(mqtt_server, 1883); // Standard MQTT port 1883
client.setCallback(callback); // Set the function to call on message reception
}
void loop() {
if (!client.connected()) {
reconnect(); // Keep trying to connect if disconnected
}
client.loop(); // Handle incoming/outgoing MQTT traffic
}
Step 3: Configurations
- Change the SSID and Password to your own wifi and password

- Change to your own topic

Step 4: Upload and Run
- Connect your ESP32 board.
- Upload the code.
- Open Serial Monitor at
115200baud. - Use the switch on your Node-RED Dashboard to toggle the LED
System Check
Working properly if:
- Serial Monitor shows “Subscribed to: your_name/led/control”
- Toggling the switch in the Node-RED Dashboard instantly turns the physical LED ON/OFF.
- Serial Monitor prints “Message arrived [your_name/led/control] ON/OFF” and “LED turned ON/OFF” when the switch is toggled.


Troubleshooting Guide
| Problems | Solutions |
| LED not turning ON/OFF | Check wiring connection. Verify LED pin is IO2 |
| No MQTT message received in Serial Monitor | Verify subTopic in ESP32 code matches the Switch topic in Node-RED (your_name/led/control) |
| ESP32 not connecting to MQTT | Check WiFi name and Password. Ensure broker.hivemq.com is reachable and not blocked by a firewall. |
Buy from:
Myduino AIot Education Kit at myduino.com






