Lab: ESP32 Wi-Fi sensor with MicroPython
In short: You install MicroPython on an ESP32 board, wire a cheap temperature and humidity sensor, join your IoT Wi-Fi and read values every few seconds. The lab also shows how to keep secrets out of code and prepares you for sending data over MQTT.
Aim and what you need
The aim is a working Wi-Fi sensor node: an ESP32 microcontroller running MicroPython that reads temperature and humidity and joins your network. Later labs send these readings to an MQTT broker.
- An ESP32 development board with a USB data cable (some cables carry power only).
- A DHT22 sensor module, jumper wires and a breadboard.
- Python 3 on your computer, plus the
esptoolandmpremotetools. - The current generic ESP32 MicroPython firmware file from the official MicroPython download page.
- A Wi-Fi network for IoT devices, ideally separate from your main one.
Steps
- Wire the sensor as in Figure 2 with the board unplugged.
- Install the tools and find the serial port (
/dev/ttyUSB0or/dev/ttyACM0on Linux,COM3or similar on Windows). - Erase the flash and write the firmware. Newer esptool releases use hyphenated commands; older ones use
esptool.pyand underscores. - Create
secrets.pyandmain.pyas below and copy them to the board. - Reset the board and watch the output in the REPL (the interactive prompt).
python3 -m pip install esptool mpremote
esptool --chip esp32 --port /dev/ttyUSB0 erase-flash
esptool --chip esp32 --port /dev/ttyUSB0 --baud 460800 write-flash -z 0x1000 ESP32_GENERIC-<version>.bin
mpremote connect /dev/ttyUSB0 fs cp secrets.py :secrets.py
mpremote connect /dev/ttyUSB0 fs cp main.py :main.py
mpremote connect /dev/ttyUSB0 reset
mpremote connect /dev/ttyUSB0 repl
# secrets.py (add this file to .gitignore)
WIFI_SSID = "<your-iot-ssid>"
WIFI_PASS = "<your-wifi-password>"
# main.py
import time, network, dht
from machine import Pin
import secrets
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(secrets.WIFI_SSID, secrets.WIFI_PASS)
for _ in range(20):
if wlan.isconnected():
break
time.sleep(1)
print("Wi-Fi:", wlan.isconnected(), wlan.ifconfig())
sensor = dht.DHT22(Pin(4))
while True:
try:
sensor.measure()
print("temp_c", sensor.temperature(), "humidity", sensor.humidity())
except OSError as e:
print("sensor error", e)
time.sleep(5)
How to verify and troubleshoot
The REPL should print Wi-Fi: True with an IP address from your IoT network, then a temperature and humidity line every five seconds. Breathe on the sensor and the humidity should rise. Your router's client list should also show the ESP32.
- Port not found: try another cable, install the USB-to-serial driver (often CP210x or CH340) and check permissions (on Linux, the
dialoutgroup). - Flashing fails to connect: hold the BOOT button while esptool starts, or lower the baud rate.
- Wi-Fi never connects: the ESP32 supports 2.4 GHz only; check the SSID and that the network is not 5 GHz only or WPA3-only.
- Sensor error or odd values: check the data pin number and the pull-up resistor; DHT22 needs about two seconds between reads.
Clean-up and next steps
Unplug the board before changing wiring. If you give the board away or reuse it, run erase-flash so your Wi-Fi password does not leave with it: the ESP32's flash is not encrypted by default, so anyone with the board can read it. Next, run a broker in the Mosquitto lab and publish your readings using MicroPython's umqtt.simple module, then secure them in the MQTT TLS lab.
Practice
Why keep Wi-Fi credentials in a separate secrets.py?
The ESP32 cannot see your network but your phone can. What is a likely reason?
Why erase the flash before giving the board away?
Key takeaways
- MicroPython makes the ESP32 easy to program in Python.
- esptool flashes firmware; mpremote copies files and opens the REPL.
- Keep secrets in a separate, ignored file.
- The ESP32 uses 2.4 GHz Wi-Fi only.
- Erase the flash before reusing or giving away a board.
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.