gsm_logger.py

Arduino MKR GSM 1400 · File : gsm_logger.py · Language : Python · Use case : Archiver sur PC les traces série du modem via pyserial

A Python script that logs everything the MKR GSM 1400 sends to its serial port onto a PC: each line is timestamped by the computer and appended to a CSV. Perfect for debugging a measurement campaign or auditing GPRS retries.

Hardware

An Arduino MKR GSM 1400 (SAMD21 microcontroller, 2G/GPRS cellular modem, external antenna, powered by a LiPo battery on the J6 connector or over USB). A PC with Python 3, pyserial (pip install pyserial), and the MKR GSM connected over USB.

Wiring

Just a micro-USB cable between board and PC; the SAMD21 native USB serial port is enough, no adapter.

How it works

pyserial opens the ACM port at 9600 baud; readline() drains the stream line by line, each read gets the computer local timestamp before a CSV write with flush so nothing is lost.

Running it

In the Arduino IDE, install the Arduino SAMD Boards package, select the MKR GSM 1400 board, then upload. Network examples assume an active data SIM is inserted, the antenna is screwed on, and 2G coverage still exists in your area.

Variants

Filter lines containing a keyword, switch to SQLite for large volumes, or auto-reconnect the serial port after unplugs.

gsm_logger.py

#!/usr/bin/env python3
"""Archive les traces serie d'un MKR GSM 1400 dans gsm_log.csv.

Branchez la carte en USB, adaptez PORT, puis : python gsm_logger.py
"""
import csv
import datetime as dt
import serial

PORT = "COM7"  # Linux : /dev/ttyACM0 ; macOS : /dev/cu.usbmodem*
BAUD = 9600


def main() -> int:
    try:
        port = serial.Serial(PORT, BAUD, timeout=1)
    except serial.SerialException as exc:
        print(f"Port {PORT} indisponible : {exc}")
        return 1
    with open("gsm_log.csv", "a", newline="", encoding="utf-8") as fh:
        w = csv.writer(fh)
        try:
            while True:
                ligne = port.readline().decode(errors="replace").strip()
                if ligne:
                    ts = dt.datetime.now().isoformat(timespec="seconds")
                    w.writerow([ts, ligne])
                    fh.flush()
        except KeyboardInterrupt:
            print("Termine.")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

← Back to the board