wifi_bridge.py
When onboard WiFi misbehaves, the MKR WiFi 1010 serial port becomes plan B: this Python bridge reads key:value lines and POSTs them as JSON to any collector. A PC over USB keeps collection running during radio diagnosis.
Hardware
An Arduino MKR WiFi 1010 (SAMD21 microcontroller, WINC1500 Wi-Fi module, no Bluetooth, USB or LiPo power on J6). PC with Python 3, pyserial and requests, USB cable to the board printing lines on Serial.
Wiring
A micro-USB cable; the board sketch just prints lines formatted sensor:value.
How it works
The for loop over the serial port iterates line by line; partition splits key and value, then requests.post sends a minimal JSON. Connection errors are logged without stopping the read loop.
Running it
Install Arduino SAMD Boards and the WiFi101 library, select the board, upload. Wi-Fi runs on 2.4 GHz only: check that your access point is not 5 GHz only.
Variants
Add a local queue (JSONL file) to lose nothing during server outages, or swap requests for paho-mqtt to go MQTT.
wifi_bridge.py
#!/usr/bin/env python3
"""Relais serie -> HTTP pour une MKR WiFi 1010 en mode fallback.
Si le WiFi de la carte est capricieux, son port serie devient le
reseau de secours : chaque ligne "cle:valeur" est POSTee en JSON.
Usage : python wifi_bridge.py [port] [url]
"""
import json
import sys
import requests
import serial
PORT = sys.argv[1] if len(sys.argv) > 1 else "/dev/ttyACM0"
URL = sys.argv[2] if len(sys.argv) > 2 else "http://127.0.0.1:5000/mesures"
def main() -> int:
with serial.Serial(PORT, 9600, timeout=1) as carte:
print(f"Relais {PORT} -> {URL}")
for ligne in carte:
l = ligne.decode(errors="replace").strip()
if ":" not in l:
continue
cle, _, valeur = l.partition(":")
try:
requests.post(URL, json={cle: valeur}, timeout=5)
except requests.ConnectionError:
print("serveur injoignable, ligne perdue:", l)
return 0
if __name__ == "__main__":
sys.exit(main())