MicroPython Guide

Push sensor data from a MicroPython device

MicroPython runs on ESP32, RP2040 (Raspberry Pi Pico W), and other microcontrollers. If you prefer Python's readability over C++ but still want microcontroller constraints, this guide is for you.

Prefer C++ on ESP32?

The ESP32 / Arduino guide uses the HTTPClient library and has a wider range of available sensor libraries.

Step 1 — Create config.py

Put credentials in a separate file. If you use a tool like Thonny or mpremote to manage files, you can control which files are uploaded to the device.

config.py
# config.py  ← add to .gitignore (or manage via WebREPL)
WIFI_SSID    = "your-wifi-network"
WIFI_PASSWORD = "your-wifi-password"
DASHBOARD_ID = "abc123xyz456"
WRITE_TOKEN  = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

Step 2 — Save the EasyBoard helper

Copy this to a file called easyboard.py and upload it to your device. Call update_tile() to push any value.

easyboard.py
# easyboard.py — save this to your device
import urequests
import ujson

_base_url = None
_token = None

def init(dashboard_id: str, write_token: str) -> None:
    """Call this once at startup with your credentials."""
    global _base_url, _token
    _base_url = f"https://easyboard.live/api/d/{dashboard_id}/tiles"
    _token = write_token

def update_tile(tile_id: str, value) -> bool:
    """Push a new value to a tile. Returns True on success."""
    if _base_url is None:
        raise RuntimeError("Call easyboard.init() first")
    try:
        r = urequests.patch(
            f"{_base_url}/{tile_id}",
            headers={
                "Content-Type": "application/json",
                "Authorization": f"Bearer {_token}",
            },
            data=ujson.dumps({"value": str(value)}),
        )
        ok = r.status_code == 200
        r.close()
        return ok
    except Exception as e:
        print(f"EasyBoard update failed: {e}")
        return False

Step 3 — main.py

main.py
# main.py
import time
import network
import easyboard
from config import WIFI_SSID, WIFI_PASSWORD, DASHBOARD_ID, WRITE_TOKEN

# ── Connect to WiFi ───────────────────────────────────────────────────────────
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)

print("Connecting to WiFi", end="")
for _ in range(20):
    if wlan.isconnected():
        break
    time.sleep(0.5)
    print(".", end="")

if not wlan.isconnected():
    raise RuntimeError("WiFi connection failed")
print(" connected")

# ── Set up EasyBoard ──────────────────────────────────────────────────────────
easyboard.init(DASHBOARD_ID, WRITE_TOKEN)

TEMP_TILE_ID = "your-tile-id-here"

# ── Main loop ─────────────────────────────────────────────────────────────────
while True:
    # Replace with your actual sensor read
    # from machine import ADC, Pin
    # temp = read_temperature()
    temp = 23.4

    easyboard.update_tile(TEMP_TILE_ID, round(temp, 1))
    time.sleep(60)

Memory is tight

MicroPython devices have limited RAM. If you see MemoryError, try calling gc.collect() before making HTTP requests, or reduce the size of your JSON payload.