Pi-Gateway, das zwischen Heim-WLAN und Handy-Hotspot auf dem Schrebergarten wechselt
Von English mit KI übersetzt ·
Hallo zusammen. Ich habe einen Pi 4, der samstags auf unserem Gemeinschaftsgarten Bodenfeuchte und ein paar DS18B20 ausliest, und die restliche Woche steht er zu Hause auf der Werkbank und liest die Gewächshaussensoren aus. Er veröffentlicht mit paho-mqtt an Hermetry.Problem: Er verbindet sich immer nur mit meinem Heim-WLAN. Im Garten schalte ich den Hotspot an meinem Handy ein, und der Pi verbindet sich einfach nicht, sodass ich an dem Tag ohne Daten in Hermetry nach Hause komme. Ich habe einer Anleitung gefolgt und beide Netze mit priority= in /etc/wpa_supplicant/wpa_supplicant.conf eingetragen, aber es sieht so aus, als würde die Datei ignoriert.
$ cat /etc/os-release | head -1
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
$ nmcli -t -f NAME,TYPE con show
preconfigured:802-11-wireless
lo:loopback
Zweite Frage, dazu passend: Wenn er eine Zeit lang offline ist, kann ich die Messwerte behalten und später senden? Im Moment verwirft das Skript sie einfach, wenn das Veröffentlichen fehlschlägt.
Hi all. I have a Pi 4 that reads soil moisture and a couple of DS18B20s at our community allotment on Saturdays, and the rest of the week it sits at home on the bench reading the greenhouse sensors. It publishes to Hermetry with paho-mqtt.
Problem: it only ever connects to my home WiFi. At the allotment I turn on the hotspot on my phone and the Pi just doesn’t join, so I come home with nothing in Hermetry for that day. I followed a guide and put both networks in /etc/wpa_supplicant/wpa_supplicant.conf with priority=, but it looks like the file is ignored.
$ cat /etc/os-release | head -1
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
$ nmcli -t -f NAME,TYPE con show
preconfigured:802-11-wireless
lo:loopback
Second thing, related: if it’s offline for a bit, can I keep the readings and send them later? Right now the script just drops them when publish fails.
Von English mit KI übersetzt ·
Bookworm verwendet jetzt NetworkManager, daher wird wpa_supplicant.conf nicht mehr gelesen. Der Weg führt über nmcli.
Bookworm uses NetworkManager now, so wpa_supplicant.conf isn’t read anymore. nmcli is the way.
Von English mit KI übersetzt ·
Kevin hat recht, und die „preconfigured“-Verbindung ist diejenige, die der Imager aus deinem Heim-WLAN angelegt hat. Füge den Hotspot als zweite NetworkManager-Verbindung hinzu und vergib jeweils eine Priorität. Ich transportiere einen Pi genau so zwischen zwei Fabriken.
sudo nmcli con add type wifi ifname wlan0 con-name allotment
ssid "Aisha-phone" wifi-sec.key-mgmt wpa-psk wifi-sec.psk "********"
sudo nmcli con modify allotment connection.autoconnect yes connection.autoconnect-priority 10
sudo nmcli con modify preconfigured connection.autoconnect-priority 20
nmcli -f NAME,AUTOCONNECT,AUTOCONNECT-PRIORITY con show
Wenn beide in Reichweite sind, gewinnt die höhere Priorität, also zuerst das Heimnetz. Schalte den Hotspot ein, bevor du den Pi einschaltest; bootet der Pi zuerst, gib ihm eine Minute, NetworkManager sucht dann erneut.Zur Zwischenspeicherung: Halte die Messwerte in einer kleinen SQLite-Tabelle oder sogar in einer JSONL-Datei vor und sende sie, sobald die Verbindung zurück ist, mit ihren eigenen Zeitstempeln in Stapeln:
[{"ts": 1762668000000, "values": {"soil.bed3": 31.5, "air.temp": 12.1}},
{"ts": 1762668300000, "values": {"soil.bed3": 31.2, "air.temp": 12.6}}]
ts ist in Millisekunden UTC. Lösche eine Zeile erst, nachdem die Veröffentlichung bestätigt wurde (QoS 1, in paho auf das PUBACK warten mit wait_for_publish()).Eine Falle beim Pi: Er hat keine batteriegepufferte Uhr. Startet er am Schrebergarten und bekommt längere Zeit kein Netz, ist die Uhr falsch, bis NTP synchronisiert; Messwerte mit dieser Uhr gestempelt landen dann zur falschen Zeit oder werden verworfen (mehr als 10 Minuten in der Zukunft werden abgelehnt). Versieh Messwerte nur dann mit einem Zeitstempel, wenn timedatectl show -p NTPSynchronized yes meldet; andernfalls sende sie ohne ts und lass Hermetry die Ankunftszeit verwenden.
Kevin’s right, and the „preconfigured“ connection is the one the imager created from your home WiFi. Add the hotspot as a second NetworkManager connection and give both a priority. I carry a Pi between two factories with exactly this.
sudo nmcli con add type wifi ifname wlan0 con-name allotment
ssid "Aisha-phone" wifi-sec.key-mgmt wpa-psk wifi-sec.psk "********"
sudo nmcli con modify allotment connection.autoconnect yes connection.autoconnect-priority 10
sudo nmcli con modify preconfigured connection.autoconnect-priority 20
nmcli -f NAME,AUTOCONNECT,AUTOCONNECT-PRIORITY con show
Higher priority wins when both are in range, so home first. Turn the hotspot on before you power the Pi; if it boots first, give it a minute, NetworkManager rescans.
For buffering: keep the readings in a small SQLite table or even a JSONL file, and when the connection comes back send them with their own time stamps, in batches:
[{"ts": 1762668000000, "values": {"soil.bed3": 31.5, "air.temp": 12.1}},
{"ts": 1762668300000, "values": {"soil.bed3": 31.2, "air.temp": 12.6}}]
ts is milliseconds UTC. Delete a row only after the publish is acknowledged (QoS 1, wait for the PUBACK in paho with wait_for_publish()).
One trap with a Pi: there’s no battery-backed clock. If it boots at the allotment and gets no network for a while, the clock is wrong until NTP syncs, and readings stamped with that clock end up at the wrong time or get dropped (more than 10 minutes in the future is refused). Only stamp readings after timedatectl show -p NTPSynchronized says yes, otherwise send them without ts and let Hermetry use the arrival time.
Von English mit KI übersetzt ·
Genau aus diesem Grund habe ich ein günstiges RTC-HAT gekauft, DS3231, für ein paar Pfund. Dann stimmt die Uhr auch offline.
I bought a cheap RTC HAT for this exact reason, DS3231, a couple of quid. Then the clock is right even offline.
Von English mit KI übersetzt ·
Und halte die Batches vernünftig klein: Der Standardwert liegt bei 10 Nachrichten pro Sekunde und Gerät. Ein großer Rückstau auf einmal wird verworfen.
And keep the batches reasonable, the default is 10 messages a second per device. A big backlog in one go gets dropped.
Von English mit KI übersetzt ·
Erledigt. Den Hotspot habe ich mit nmcli hinzugefügt, wie tsmith geschrieben hat, Prioritäten 20/10. Am Samstag hat er sich etwa 40 s nach dem Booten mit dem Handy verbunden. Messwerte wurden in eine JSONL-Datei geschrieben, solange das Handy an den hinteren Beeten kein Signal hatte, und dann in Paketen zu je 50 hochgeladen, sobald es wieder Empfang gab. In der ersten Woche waren die Zeitstempel bei den ersten paar Messwerten um eine Stunde falsch, genau das NTP-Problem, daher habe ich eine DS3231 bestellt, so wie Michael. Bis sie da ist, prüft das Skript vor dem Stempeln NTPSynchronized.
Sorted. Added the hotspot with nmcli as tsmith wrote, priorities 20/10. Saturday it joined the phone within about 40 s of boot. Readings went into a JSONL file while the phone had no signal at the far beds, then went up in batches of 50 when it came back. First week the times were off by an hour on the first few readings, the NTP thing exactly, so I’ve ordered a DS3231 like Michael. Until it comes the script checks NTPSynchronized before stamping.
- Du musst angemeldet sein, um auf dieses Thema antworten zu können.
Mitdiskutieren
Melde dich an, um Fragen zu stellen und zu beantworten, hilfreiche Beiträge mit „Gefällt mir“ zu markieren und die Antwort zu kennzeichnen, die dein Problem gelöst hat. Jeder Beitrag bringt Punkte und Stufen.

