A simple IoT project that controls an ESP8266's onboard LED via MQTT messages. This project demonstrates how to connect an ESP8266 to WiFi, communicate with an MQTT broker, and control hardware based on received messages.
- WiFi connectivity with automatic reconnection
- MQTT client with automatic broker reconnection
- LED control via MQTT messages
- Multiple command support (on, off, toggle, status)
- Device status reporting (IP address, MAC address)
- Serial debug output for monitoring
- ESP8266 board (ESP-12E or compatible)
- USB cable for programming and power
- Built-in LED (GPIO2)
- PlatformIO or Arduino IDE
- PubSubClient library (automatically installed by PlatformIO)
-
Clone this repository:
git clone https://github.com/yourusername/esp8266-mqtt.git cd esp8266-mqtt -
Open the project in PlatformIO (VS Code with PlatformIO extension)
-
Install dependencies:
pio lib install
- Install the Arduino IDE
- Install the ESP8266 board support:
- File → Preferences → Additional Board Manager URLs
- Add:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
- Install PubSubClient library:
- Sketch → Include Library → Manage Libraries
- Search for "PubSubClient" and install
Before uploading the code, you need to configure your WiFi and MQTT settings in src/main.cpp:
// WiFi Configuration
const char *ssid = "YOUR_WIFI_SSID"; // Replace with your WiFi name
const char *password = "YOUR_WIFI_PASSWORD"; // Replace with your WiFi password
// MQTT Configuration
const char *serverHostname = "broker.hivemq.com"; // Replace with your MQTT broker
const unsigned int serverPort = 1883; // MQTT port (default: 1883)
const char *topic = "esp8266/led"; // MQTT topic for LED controlbroker.hivemq.com(public, no authentication)test.mosquitto.org(public, no authentication)broker.emqx.io(public, no authentication)
For production use, consider setting up your own MQTT broker using Mosquitto or HiveMQ.
- Connect your ESP8266 board via USB
- Build and upload:
pio run --target upload
- Monitor serial output:
pio device monitor
- Select your board: Tools → Board → ESP8266 Boards → Generic ESP8266 Module (or your specific board)
- Select the correct port: Tools → Port
- Click Upload button
- Open Serial Monitor (115200 baud)
Once the device is connected to WiFi and the MQTT broker, you can send the following commands to the configured topic:
| Command | Description | Example |
|---|---|---|
on or 1 |
Turn LED on | mosquitto_pub -h broker.hivemq.com -t esp8266/led -m "on" |
off or 0 |
Turn LED off | mosquitto_pub -h broker.hivemq.com -t esp8266/led -m "off" |
toggle |
Toggle LED state | mosquitto_pub -h broker.hivemq.com -t esp8266/led -m "toggle" |
status |
Request device status | mosquitto_pub -h broker.hivemq.com -t esp8266/led -m "status" |
Install mosquitto-clients:
# Ubuntu/Debian
sudo apt-get install mosquitto-clients
# macOS
brew install mosquittoSubscribe to the topic to see responses:
mosquitto_sub -h broker.hivemq.com -t esp8266/ledPublish commands (in another terminal):
# Turn LED on
mosquitto_pub -h broker.hivemq.com -t esp8266/led -m "on"
# Turn LED off
mosquitto_pub -h broker.hivemq.com -t esp8266/led -m "off"
# Toggle LED
mosquitto_pub -h broker.hivemq.com -t esp8266/led -m "toggle"
# Get device status
mosquitto_pub -h broker.hivemq.com -t esp8266/led -m "status"MQTT Explorer is a great GUI tool for testing MQTT communications:
- Download and install MQTT Explorer
- Connect to your broker (e.g., broker.hivemq.com)
- Navigate to your topic (e.g., esp8266/led)
- Publish messages and see responses in real-time
The device provides detailed debug information via serial output at 9600 baud:
========================================
ESP8266 MQTT LED Controller
========================================
Connecting to WiFi: YourWiFiName
.....
WiFi connected! IP address: 192.168.1.100
MAC address: AA:BB:CC:DD:EE:FF
MQTT connecting as client ESP8266-A3F2...
MQTT connected successfully!
Subscribed to topic: esp8266/led
Setup complete!
Message received on topic 'esp8266/led': on
LED turned ON
- Verify SSID and password are correct
- Check that your WiFi network is 2.4GHz (ESP8266 doesn't support 5GHz)
- Ensure the ESP8266 is within range of your router
- Verify the MQTT broker hostname is correct
- Check if the broker requires authentication (update code accordingly)
- Test broker connectivity from your computer first
- Some public brokers may be temporarily unavailable - try another one
- Verify the correct GPIO pin is used (GPIO2 for most ESP8266 boards)
- Check serial monitor to confirm messages are being received
- Note that LED logic is inverted (LOW = ON, HIGH = OFF)
- Ensure baud rate is set to 9600
- Some boards may show boot messages at different baud rates - this is normal
esp8266-mqtt/
├── src/
│ └── main.cpp # Main application code
├── include/ # Header files (if any)
├── lib/ # Custom libraries (if any)
├── test/ # Test files (if any)
├── platformio.ini # PlatformIO configuration
├── Readme.md # This file
└── .gitignore # Git ignore rules
- connectWifi(): Establishes WiFi connection
- connectMQTT(): Connects to MQTT broker and subscribes to topic
- callback(): Handles incoming MQTT messages and controls LED
- setLedState(): Controls the LED state
- setup(): Initializes hardware and connections
- loop(): Maintains MQTT connection and processes messages
The callback function supports multiple commands with case-insensitive matching:
- LED control:
on,off,1,0,toggle - Status reporting:
status - Error handling for unknown commands
- Define new GPIO pins in the configuration section
- Initialize them in
setup() - Add command handling in
callback()
Example - Add a temperature sensor:
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// In callback function:
else if (msg == "temperature")
{
float temp = dht.readTemperature();
String response = "Temperature: " + String(temp) + "C";
client.publish(topic, response.c_str());
}If your MQTT broker requires authentication, modify the connectMQTT() function:
const char* mqtt_user = "your_username";
const char* mqtt_password = "your_password";
// In connectMQTT():
if (client.connect(clientId.c_str(), mqtt_user, mqtt_password))
{
// ... rest of the code
}For better organization, use separate topics for commands and status:
const char *command_topic = "esp8266/led/command";
const char *status_topic = "esp8266/led/status";
// Subscribe to command topic
client.subscribe(command_topic);
// Publish status to status topic
client.publish(status_topic, "LED: ON");- Never commit WiFi passwords to public repositories
- Use environment variables or separate config files for credentials
- Consider using MQTT over TLS (port 8883) for production
- Implement authentication for your MQTT broker
- Use unique, strong passwords
- Consider using certificate-based authentication for production deployments
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is open source and available under the MIT License.
- ESP8266 Arduino Core Documentation
- PubSubClient Library
- MQTT Protocol
- PlatformIO Documentation
- HiveMQ MQTT Essentials
Your Name - Your GitHub Profile
- Nick O'Leary for the PubSubClient library
- ESP8266 Arduino core team
- MQTT community