mesures.py
MicroPython runs on all these boards: no toolchain, edit main.py and go. This DHT logger writes line-by-line JSON to a file — the format every downstream tool swallows.
Hardware required
A classic ESP32 board (ESP32-WROOM-32 module, dual-core Xtensa 240 MHz, Wi-Fi + Bluetooth) such as a DevKitC / DOIT V1, powered over micro-USB. 1 DHT11 (or DHT22 by changing the class) on GPIO4.
Wiring
VDD 3.3 V, DATA GPIO4 + 10 kΩ pull-up, GND. The internal flash keeps data.log across power cuts.
How it works
dht.DHT11.measure() drives the 1-wire protocol in pure MicroPython; failures are normal, hence three tries. open("a") appends the JSON line to the internal filesystem. In the Arduino IDE, install the « esp32 » board package, pick « Dev Module », the port, then Upload (hold BOOT if detection fails). GPIO34-39 are input-only; avoid GPIO6-11 and GPIO12.
Going further
mpremote cp data.log . to pull the journal back, or replace the append with a Wi-Fi send (network + socket modules).
mesures.py
# MicroPython : DHT11 sur GPIO4 -> moniteur serie + journal /data.log.
# Copier ce fichier en main.py (Thonny, rshell ou mpremote cp mesures.py main.py).
import machine, dht, time, json
capteur = dht.DHT11(machine.Pin(4))
led = machine.Pin(2, machine.Pin.OUT)
horloge = machine.time_pulse_us # reference pour garder la lib chargee
def lire():
for _ in range(3): # 3 tentatives : le DHT rate des trames
try:
capteur.measure()
return capteur.temperature(), capteur.humidity()
except OSError:
time.sleep_ms(200)
return None, None
print("Mesures toutes les 5 s (Ctrl-C pour sortir)")
while True:
t, h = lire()
if t is not None:
ligne = json.dumps({"t": t, "h": h, "ms": time.ticks_ms()})
print(ligne)
with open("data.log", "a") as f: # append sur le systeme de fichiers
f.write(ligne + "\n")
led.value(not led.value())
time.sleep(5)