mesures.py

ESP32-S2 · File : mesures.py · Language : MicroPython · Use case : Journaliser des mesures en MicroPython sans compilation

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

An ESP32-S2 board (single-core Xtensa, Wi-Fi, native USB-OTG on GPIO19/20, 14 capacitive touch pads) such as a DevKitC-1 or QT Py, powered over 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 (esp32 package), select « ESP32S2 Dev Module »; the S2 flashes straight over its native USB (double reset on some boards to enter the bootloader).

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)

← Back to the board