console_bouton.py

Arduino Nano Every · File : console_bouton.py · Language : Python · Use case : Régler la carte par commandes série depuis Python

Once the Nano Every serial setpoints are in place, this Python client becomes the maintenance tool: it sends s/p commands, checks acknowledgements then keeps listening to traces. The sketch + console duo avoids reflashing for every tweak.

Hardware

An Arduino Nano Every (ATmega4809 at 20 MHz, same footprint as the Nano, micro-B USB, separate UART chip, 5 V pins, drop-in compatibility with the classic Nano). PC with Python 3 + pyserial, Nano Every flashed with nvm-consignes.ino, USB cable.

Wiring

USB cable only; the script waits 2 s after opening to ignore the port reset.

How it works

write + read_all perform the command/ack round trip; readline then continues in live listening. The 1 s timeout prevents any block if the board goes quiet.

Running it

Install the Arduino megaAVR Boards package in the IDE, select Arduino Nano Every, upload. The USB Serial is separate from hardware Serial1 (RX/TX pins): classic serial code works as is.

Variants

Turn the loop into an interactive REPL (input()) for the workshop, or stack setpoint profiles in a JSON applied on choice.

console_bouton.py

#!/usr/bin/env python3
"""Console de reglage pour le Nano Every (croquis nvm-consignes.ino).

Envoie les commandes s<seuil> et p<periode>, lit les reponses.
Usage : python console_bouton.py [port]
"""
import sys
import time

import serial


def envoyer(carte: serial.Serial, commande: str) -> str:
    carte.write((commande + "\n").encode())
    time.sleep(0.2)
    return carte.read_all().decode(errors="replace").strip()


def principal() -> int:
    port = sys.argv[1] if len(sys.argv) > 1 else "/dev/ttyACM0"
    with serial.Serial(port, 9600, timeout=1) as carte:
        time.sleep(2)  # laisser le port se stabiliser
        print(envoyer(carte, ""))  # réveil + etat courant
        for cmd in ("s400", "p30"):
            print(f"> {cmd} -> {envoyer(carte, cmd)}")
        # lecture continue des traces pendant 5 s
        fin = time.time() + 5
        while time.time() < fin:
            ligne = carte.readline().decode(errors="replace").strip()
            if ligne:
                print("|", ligne)
    return 0


if __name__ == "__main__":
    sys.exit(principal())

← Back to the board