nb_monitor.py
During an NB-IoT test campaign, keep an eye on the station: this pyserial script reads the MKR NB 1500 serial port, counts published cycles and prints every error-keyword line. A lightweight monitor for long runs.
Hardware
An Arduino MKR NB 1500 (SAMD21 microcontroller, u-blox LTE-M/NB-IoT modem, external antenna, LiPo power on J6 or USB). PC with Python 3 and pyserial, USB cable to the board, board powered and joined to the network.
Wiring
A micro-USB cable is enough; the SAMD21 native ACM port exposes the sketch Serial.
How it works
readline() drains the stream without blocking (1 s timeout); the MOTS_CLE_ERREUR list centralises detection, and a summary prints every ten events.
Running it
Install the Arduino SAMD Boards package and the MKRNB library, select the board, upload. Plan for an activated IoT SIM (LTE-M or NB1) and good reception: attaching to the network can take several minutes.
Variants
Push counters to a dashboard (InfluxGrafana), add an email alert after three consecutive failures, or log to SQLite for history.
nb_monitor.py
#!/usr/bin/env python3
"""Surveille les logs serie d'un MKR NB 1500 et alerte sur les echecs.
Usage : python nb_monitor.py [port]
"""
import sys
import time
import serial
MOTS_CLE_ERREUR = ("KO", "ECHEC", "perdu", "restart")
def surveiller(port: str) -> None:
with serial.Serial(port, 9600, timeout=1) as carte:
ok = ko = 0
debut = time.time()
print(f"Surveillance de {port} (Ctrl+C pour finir)")
while True:
ligne = carte.readline().decode(errors="replace").strip()
if not ligne:
continue
if any(m in ligne for m in MOTS_CLE_ERREUR):
ko += 1
print(f"[{time.time() - debut:6.0f}s] ERREUR: {ligne}")
elif "publie" in ligne or "OK" in ligne:
ok += 1
if (ok + ko) % 10 == 0:
print(f"bilan: {ok} OK / {ko} KO")
if __name__ == "__main__":
try:
surveiller(sys.argv[1] if len(sys.argv) > 1 else "/dev/ttyACM0")
except KeyboardInterrupt:
print("\nArrete.")