Project: a Wi-Fi temperature and humidity monitor
In short: In this project you wire a temperature and humidity sensor to an ESP32 board, write a short MicroPython program and publish readings every minute to an MQTT broker over TLS. It is a perfect first IoT build and teaches the security basics every device needs.
What you will build and what you need
You will build a small sensor node for a server cupboard, greenhouse or bedroom. It measures temperature and relative humidity and sends a JSON message to an MQTT broker once a minute. A dashboard or alert rule can then warn you when a comms room gets too hot.
| Part | Notes |
|---|---|
| ESP32 development board | Any board with USB and Wi-Fi; flash the current MicroPython firmware |
| DHT22 (AM2302) sensor | Cheap and simple; for better accuracy use an SHT31 or SHT4x on I2C |
| 10 kilohm resistor | Pull-up for the DHT22 data line (many breakout boards include it) |
| Breadboard and jumper wires | For the prototype |
| USB power supply and cable | A good-quality 5 V supply avoids random resets |
| An MQTT broker | Mosquitto on a Raspberry Pi or server in your lab |
Design
Wire the DHT22 pin 1 to 3.3 V, pin 2 (data) to GPIO 4 with the pull-up to 3.3 V, and pin 4 to ground. Mount the sensor away from the ESP32 itself, because the board's own heat will raise the reading by a degree or more. Readings go to the topic home/<room>/<device-id>/env, which keeps permissions simple: each device can publish only under its own ID.
Build steps
- Flash MicroPython to the ESP32 with
esptooland connect with a tool such as Thonny ormpremote. - Install the MQTT client library on the board:
mpremote mip install umqtt.simple. - Create the broker user for this device and copy the broker's CA certificate (in DER format) to the board as
ca.der. - Create
secrets.pyon the board with the Wi-Fi and MQTT credentials. Never commit it to Git. - Upload
main.pybelow, reset the board and watch the topic withmosquitto_sub. - Check the readings against a trusted thermometer and note any offset.
import time, json, ssl, network, machine, dht
from umqtt.simple import MQTTClient
import secrets # SSID, WIFI_PASS, MQTT_HOST, MQTT_USER, MQTT_PASS, DEVICE_ID
sensor = dht.DHT22(machine.Pin(4))
TOPIC = "home/lab/" + secrets.DEVICE_ID + "/env"
def wifi_up():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
wlan.connect(secrets.SSID, secrets.WIFI_PASS)
for _ in range(20):
if wlan.isconnected():
break
time.sleep(1)
return wlan.isconnected()
def mqtt_client():
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.verify_mode = ssl.CERT_REQUIRED
ctx.load_verify_locations(cafile="ca.der")
c = MQTTClient(secrets.DEVICE_ID, secrets.MQTT_HOST, port=8883,
user=secrets.MQTT_USER, password=secrets.MQTT_PASS,
keepalive=120, ssl=ctx)
c.connect()
return c
while True:
try:
if wifi_up():
sensor.measure()
msg = {"t": sensor.temperature(), "h": sensor.humidity()}
c = mqtt_client()
c.publish(TOPIC, json.dumps(msg), qos=1)
c.disconnect()
except Exception as e:
print("error:", e)
time.sleep(60)
The program reconnects each cycle, which is simple and robust. For battery power, replace time.sleep with machine.deepsleep(60000) so the board sleeps between readings.
Security checklist and next steps
- Unique credentials per device, stored in
secrets.py, not in the main code or in Git. - TLS with certificate verification to the broker; no plain port 1883 outside the lab.
- Broker ACL allowing this device to publish only to its own topic.
- Separate network: put sensors on an IoT VLAN or SSID that cannot reach your laptops.
- Disable unused services: if you enabled WebREPL during development, turn it off.
- Plan updates: keep a note of the firmware version and update MicroPython periodically.
Common mistakes are reading the DHT22 more often than every two seconds, which returns errors, and placing the sensor in direct sunlight. Next, feed the data into the MQTT dashboard project and add an alert when the temperature passes a limit you choose.
Practice
Why should the sensor be placed away from the ESP32 board?
Why does each device publish only under its own device ID topic?
What change would make the node suitable for battery power?
Key takeaways
- An ESP32, a DHT22 or SHT31 and MicroPython make a complete sensor node.
- Publish small JSON messages to a per-device MQTT topic.
- Keep secrets in a separate file and out of version control.
- Use TLS with certificate checking and broker ACLs, even at home.
- Calibrate against a trusted thermometer and keep the sensor away from heat.
Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.