Free tech learning for everyone, from basics to pro

IoT LabsLabsBeginner

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 esptool and mpremote tools.
  • 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.
Lab topologyDHT22 sensortemp, humidityESP32MicroPythonWi-Fi APIoT networkLaptopserial and MQTT
Figure 1. The sensor connects to the ESP32, which joins your IoT Wi-Fi; your laptop flashes it over USB and receives readings.

Steps

  1. Wire the sensor as in Figure 2 with the board unplugged.
  2. Install the tools and find the serial port (/dev/ttyUSB0 or /dev/ttyACM0 on Linux, COM3 or similar on Windows).
  3. Erase the flash and write the firmware. Newer esptool releases use hyphenated commands; older ones use esptool.py and underscores.
  4. Create secrets.py and main.py as below and copy them to the board.
  5. 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)
Wiring the DHT22VCCto 3V3GNDto GNDDATAto GPIO 4Pull-up10k if bare sensorUSBto laptopPowerdo not use 5V data
Figure 2. Typical wiring for a DHT22 module; breakout boards usually already include the pull-up resistor.

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 dialout group).
  • 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?
So you can exclude that file from Git and share or publish main.py without leaking the password.
The ESP32 cannot see your network but your phone can. What is a likely reason?
The network is 5 GHz only; the ESP32 supports 2.4 GHz Wi-Fi.
Why erase the flash before giving the board away?
Flash is unencrypted by default, so the stored Wi-Fi password and code could be read.

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.
Tracks your learning on this device only. See your progress on the learning paths.

About the author

Mohamed Fayas

Founder of FM TecHub. CCNA certified, with a BTEC HND in Network Engineering, and BEng (Hons) in Computer Networking and Cloud Security. He writes practical, plain-English guides on networking, security and cloud.

More about the author

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.

Stay up to date by email

New notes, tools and security news from FM TecHub, explained simply. Free.

We only use your email to send these updates. See our privacy policy.