Pasarela Pi que alterna entre el WiFi de casa y el punto de acceso del móvil en el huerto
Traducido del English por IA ·
Hola a todos. Tengo una Pi 4 que lee la humedad del suelo y un par de DS18B20 en nuestro huerto comunitario los sábados, y el resto de la semana está en casa, sobre la mesa, leyendo los sensores del invernadero. Publica en Hermetry con paho-mqtt.Problema: solo se conecta a mi WiFi de casa. En el huerto activo el punto de acceso de mi móvil, pero la Pi no se une, así que vuelvo a casa sin ningún dato en Hermetry ese día. Seguí una guía y añadí ambas redes en /etc/wpa_supplicant/wpa_supplicant.conf con priority=, pero parece que el archivo se ignora.
$ 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
Segunda cuestión, relacionada: si se queda sin conexión un rato, ¿puedo guardar las lecturas y enviarlas después? Ahora mismo el script simplemente las descarta cuando falla la publicación.
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.
Traducido del English por IA ·
Bookworm ahora usa NetworkManager, así que wpa_supplicant.conf ya no se lee. La forma correcta es nmcli.
Bookworm uses NetworkManager now, so wpa_supplicant.conf isn’t read anymore. nmcli is the way.
Traducido del English por IA ·
Kevin tiene razón, y la conexión «preconfigured» es la que creó el imager a partir de tu WiFi de casa. Añade el hotspot como segunda conexión de NetworkManager y asigna una prioridad a ambas. Yo llevo una Pi entre dos fábricas exactamente con esta configuración.
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
Si ambas están al alcance, gana la prioridad más alta, así que primero la de casa. Enciende el hotspot antes de alimentar la Pi; si arranca primero, dale un minuto, NetworkManager vuelve a escanear.Para el almacenamiento temporal: guarda las lecturas en una tabla SQLite pequeña o incluso en un archivo JSONL, y cuando vuelva la conexión envíalas con sus propias marcas de tiempo, en lotes:
[{"ts": 1762668000000, "values": {"soil.bed3": 31.5, "air.temp": 12.1}},
{"ts": 1762668300000, "values": {"soil.bed3": 31.2, "air.temp": 12.6}}]
ts está en milisegundos UTC. Borra una fila solo después de confirmar la publicación (QoS 1, espera el PUBACK en paho con wait_for_publish()).Una trampa con la Pi: no tiene reloj con batería de respaldo. Si arranca en el huerto y no obtiene red durante un rato, el reloj es incorrecto hasta que NTP se sincroniza, y las lecturas marcadas con ese reloj acaban con la hora equivocada o se descartan (más de 10 minutos en el futuro se rechaza). Marca las lecturas con hora solo cuando timedatectl show -p NTPSynchronized diga yes; si no, envíalas sin ts y deja que Hermetry use la hora de llegada.
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.
Traducido del English por IA ·
Compré una RTC HAT barata justo para este motivo, una DS3231, por un par de libras. Así el reloj está bien incluso sin conexión.
I bought a cheap RTC HAT for this exact reason, DS3231, a couple of quid. Then the clock is right even offline.
Traducido del English por IA ·
Y mantén los lotes en un tamaño razonable: el valor por defecto es de 10 mensajes por segundo y por dispositivo. Una acumulación grande enviada de una vez se descarta.
And keep the batches reasonable, the default is 10 messages a second per device. A big backlog in one go gets dropped.
Traducido del English por IA ·
Resuelto. Añadí el punto de acceso con nmcli como escribió tsmith, prioridades 20/10. El sábado se conectó al teléfono unos 40 s después del arranque. Las lecturas se guardaron en un archivo JSONL mientras el teléfono no tenía señal en los bancales del fondo, y luego se enviaron en lotes de 50 cuando volvió la señal. La primera semana, las horas estaban desfasadas una hora en las primeras lecturas, justo el tema del NTP, así que encargué un DS3231 como Michael. Mientras llega, el script comprueba NTPSynchronized antes de poner la marca de tiempo.
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.
- Debes estar registrado para responder a este debate.
Únete a la conversación
Inicia sesión para preguntar y responder, dar me gusta a las publicaciones útiles y marcar la respuesta que resolvió tu problema. Cada aportación suma puntos y niveles.

