GPS speedometer and clock
A bike or car speedometer that needs no wheel sensor: satellites do the measuring.
Shop by category
Shop all componentsMQTT is the messaging protocol most home dashboards, Node-RED and Home Assistant setups use. This sketch reads a DHT22 and publishes two numbers to a broker with the PubSubClient library. It uses a public test broker so you can see results in minutes; for anything real, run your own broker such as Mosquitto on a Raspberry Pi. Any MQTT phone app can subscribe to the topics and show the readings.
Do not send private data to a public broker.
Tick anything you already have and the total updates.
Follow the colours: red is power, dark grey is ground, blue is a signal.
USB phone charger or a power bank.
mqtt_dht22.inoVerify & upload in Electrolark#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
const char* WIFI_NAME = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";
const char* BROKER = "broker.hivemq.com"; // public test broker: use your own for real data
const char* BASE = "gb-demo-1234/room"; // change this to something unique
DHT dht(4, DHT22);
WiFiClient net;
PubSubClient mqtt(net);
void connectAll() {
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(WIFI_NAME, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) delay(300);
}
while (!mqtt.connected()) {
String id = "esp32-" + String((uint32_t)ESP.getEfuseMac(), HEX);
if (!mqtt.connect(id.c_str())) delay(2000);
}
}
void setup() {
Serial.begin(115200);
dht.begin();
mqtt.setServer(BROKER, 1883);
}
void loop() {
connectAll();
float t = dht.readTemperature(), h = dht.readHumidity();
if (!isnan(t) && !isnan(h)) {
mqtt.publish((String(BASE) + "/temperature").c_str(), String(t, 1).c_str());
mqtt.publish((String(BASE) + "/humidity").c_str(), String(h, 0).c_str());
Serial.printf("published %.1f C %.0f %%\n", t, h);
}
mqtt.loop();
delay(30000);
}
Check the wiring and that the DHT22 data pin is on GPIO 4. Wait two seconds after power-up before the first read.
Subscribe to the exact topic, for example gb-demo-1234/room/#, and check that the app uses port 1883 without TLS.
Another device may be using the same client id. The sketch builds one from the chip id, so make sure each ESP32 has its own.
A small publish and subscribe messaging protocol. A device publishes a value to a topic and anything subscribed to that topic receives it.
No. Anyone can read what you send there. Use it only to learn, then run your own Mosquitto broker.
Yes. Point BROKER at your Home Assistant's MQTT broker and add sensors for the two topics.
4 parts: ESP32-WROOM-32 38-Pin Development Board, DHT22 AM2302 Digital Temperature & Humidity Sensor, Solderless Breadboard and Dupont Jumper Wires (40 pcs). On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 1.5 hours. The build on this page has 6 steps.
No. This page has the complete, tested code (mqtt_dht22.ino).
About ₹585 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
How MQTT topics and publishing work; Reading a DHT22 on an ESP32; Reconnecting to Wi-Fi and the broker automatically.
Ideas that reuse this build: ESP32 web server relay switch. They are listed under Take it further on this page.
A bike or car speedometer that needs no wheel sensor: satellites do the measuring.
Records where you went every five seconds, ready to open on a map.
Two boards, two radios: press the button at the gate and it rings indoors.
Check temperature, humidity and pressure from any phone on your Wi-Fi.
Learning project. Check every circuit and the datasheet of each part before you power it up.