Send DHT11 readings to a live cloud dashboard, and switch an LED or relay back from anywhere.
Project_11_ESP32_MQTT_Ubidots.ino, then open it in the Arduino IDE. Needs the Ubidots ESP32 MQTT, DHT sensor library, and Adafruit Unified Sensor libraries, plus your Wi-Fi and Ubidots token.
In Projects 5 to 9 the ESP32 was a web server: your phone had to be on the same Wi-Fi and had to keep asking for data. MQTT flips that around. Devices publish messages to a broker, and anyone interested subscribes. It is lightweight and works from anywhere with internet.
| Term | Meaning | Here |
|---|---|---|
| Broker | Server that routes all messages | Hosted by Ubidots |
| Publish | Send a value to a topic | ESP32 sends temperature, humidity |
| Subscribe | Ask to receive a topic | ESP32 listens for led |
stem.ubidots.com.BBUS-xxxx).Install from the Library Manager: Ubidots ESP32 MQTT (pulls in PubSubClient), DHT sensor library, Adafruit Unified Sensor.
| Qty | Part | Notes |
|---|---|---|
| 1 | ESP32 · breadboard · jumper wires | as always |
| 1 | DHT11 module | S to GPIO 4, + to 3.3 V, - to GND (same as Project 9) |
| 1 | LED + 220 Ohm, or the relay from Project 7 | to GPIO 26 (the thing the cloud switches) |
Edit the marked block at the top of the sketch: WIFI_SSID, WIFI_PASS, UBIDOTS_TOKEN, and a unique DEVICE_LABEL. Upload, then open Serial Monitor at 115200:
============================================== Project 11: ESP32 + MQTT + Ubidots ============================================== DHT11 data -> GPIO 4 Output -> GPIO 26 [PUB] temperature: 24.6 C, humidity: 41.0 % [CLOUD] led -> ON (raw "1")
temperature and humidity.led.led.| Symptom | Likely cause | Fix |
|---|---|---|
| Serial stops after Wi-Fi | Wrong token | Re-copy the Default token |
| No device in Ubidots | Never published | Check DHT reads numbers, not -- |
| Switch does nothing | Variable not named led | Name it exactly led |
| Reconnect loop | 5 GHz Wi-Fi | ESP32 is 2.4 GHz only |