PROJECT 11 · CLOUD / MQTT

MQTT + Ubidots: Cloud Dashboard and Control

Send DHT11 readings to a live cloud dashboard, and switch an LED or relay back from anywhere.

⬇ Download the sketch Save 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.

1 · Why MQTT

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.

DHT11 --> ESP32 --publish--> [ Ubidots broker ] --> Dashboard (you watch) LED <-- ESP32 <--subscribe-- [ Ubidots broker ] <-- Switch widget (you control)
TermMeaningHere
BrokerServer that routes all messagesHosted by Ubidots
PublishSend a value to a topicESP32 sends temperature, humidity
SubscribeAsk to receive a topicESP32 listens for led

2 · Get your Ubidots token

  1. Sign up for a free Ubidots STEM account at stem.ubidots.com.
  2. Open your profile, then API Credentials, and copy the Default token (looks like BBUS-xxxx).
  3. No need to create the device by hand: it appears the first time the board publishes.

3 · Libraries and wiring

Install from the Library Manager: Ubidots ESP32 MQTT (pulls in PubSubClient), DHT sensor library, Adafruit Unified Sensor.

QtyPartNotes
1ESP32 · breadboard · jumper wiresas always
1DHT11 moduleS to GPIO 4, + to 3.3 V, - to GND (same as Project 9)
1LED + 220 Ohm, or the relay from Project 7to GPIO 26 (the thing the cloud switches)
This is Project 9's sensor plus Project 7's actuator on one breadboard. Nothing new to wire, only new code.

4 · Edit, upload, watch

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")

5 · Build the dashboard

  1. In Devices, confirm your device appeared with temperature and humidity.
  2. Add a control variable: open the device, Add Variable, type Raw, name it exactly led.
  3. In Data, Dashboards, add a Gauge on temperature, a Line chart on temperature and humidity, and a Switch on led.
  4. Flip the switch. The board's LED or relay follows within a few seconds.

6 · Troubleshooting

SymptomLikely causeFix
Serial stops after Wi-FiWrong tokenRe-copy the Default token
No device in UbidotsNever publishedCheck DHT reads numbers, not --
Switch does nothingVariable not named ledName it exactly led
Reconnect loop5 GHz Wi-FiESP32 is 2.4 GHz only
← Project 10 · OLED Display Project 12 · Telegram Bot →