Raspberry Pi 5 as a Meshtastic Gateway
A Raspberry Pi 5 makes an excellent Meshtastic gateway — a node that bridges the mesh to the internet, logs data from field sensors, and gives you remote visibility into your network. The Pi runs the Meshtastic Python library to talk to an attached LoRa module (or connects via USB to a Pico running Meshtastic), then forwards traffic to MQTT and logs telemetry to a local database. This is the architecture for a base station at the edge of a field network.
Architecture
Choosing a Pi 5 Role
Both 4 GB and 16 GB Pi 5 boards can be useful gateways. The radio interface and a small SQLite logger need very little memory; storage quality, reliable power, and a good antenna matter more.
| Pi 5 role | 4 GB board | 16 GB board |
|---|---|---|
| USB radio + Meshtastic CLI | More than enough | More than enough |
| SQLite logger + SSH or VPN access | Good fit | Good fit |
| Mosquitto + modest dashboard | Usually fine with sensible limits | Extra headroom |
| Several services, Grafana, databases, development work | Possible, but monitor memory | Better choice |
Use a quality USB-C power supply and, for a long-lived logger, an SSD or other storage designed for repeated writes. A fast board with a brownout-prone supply or worn microSD card is not a reliable gateway.
Hardware Options for the Gateway Radio
| Option | Notes |
|---|---|
| USB-connected Pico + SX1262 | Simplest: flash Meshtastic onto the Pico, plug into Pi via USB. Python CLI connects via serial. No HAT needed on the Pi. |
| Waveshare SX1262 HAT for Pi | Plugs onto Pi's 40-pin header. Requires SPI configuration on the Pi. More integrated. |
| RAK2287 / RAK5146 M.2 card | LoRaWAN concentrator for Pi via M.2. Multi-channel (8+ simultaneous). Overkill for Meshtastic but excellent for LoRaWAN. |
Setup: USB Pico as Gateway Radio
# On the Pico (already flashed with Meshtastic): # Configure as a router to participate actively in relaying $ meshtastic --port /dev/ttyACM0 --set device.role ROUTER # On the Pi 5: $ sudo apt update && sudo apt install python3-pip python3-venv -y $ python3 -m venv ~/meshtastic-env $ source ~/meshtastic-env/bin/activate $ pip install meshtastic # Verify the Pico is visible $ meshtastic --port /dev/ttyACM0 --info # Add your user to the dialout group (avoid sudo for serial access) $ sudo usermod -aG dialout $USER $ newgrp dialout
Use a Stable Serial Device Name
/dev/ttyACM0 can change when another USB serial device is connected or after a reboot. On a Pi gateway, prefer the stable symlink created for the physical Pico:
$ ls -l /dev/serial/by-id/ usb-Raspberry_Pi_Pico_... -> ../../ttyACM0 # Use the full /dev/serial/by-id/... path in scripts and systemd units.
Update SERIAL_PORT in the logger to that stable path after confirming it works. This makes an unattended gateway much less likely to attach to the wrong device.
Python Script: Logging Telemetry to SQLite
#!/usr/bin/env python3
"""Log Meshtastic telemetry to SQLite."""
import sqlite3
import time
import meshtastic
import meshtastic.serial_interface
from pubsub import pub
DB_PATH = "/home/pi/meshtastic-data.db"
SERIAL_PORT = "/dev/ttyACM0"
def init_db():
con = sqlite3.connect(DB_PATH)
cur = con.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS telemetry (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts INTEGER NOT NULL,
node_id TEXT NOT NULL,
long_name TEXT,
battery_pct REAL,
voltage REAL,
rssi INTEGER,
snr REAL
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts INTEGER NOT NULL,
from_id TEXT NOT NULL,
to_id TEXT,
channel INTEGER,
text TEXT
)
""")
con.commit()
return con
con = init_db()
def on_receive(packet, interface):
ts = int(time.time())
from_id = hex(packet.get('from', 0))
decoded = packet.get('decoded', {})
portnum = decoded.get('portnum', '')
if portnum == 'TELEMETRY_APP':
tel = decoded.get('telemetry', {}).get('deviceMetrics', {})
rssi = packet.get('rxRssi')
snr = packet.get('rxSnr')
cur = con.cursor()
cur.execute(
"INSERT INTO telemetry (ts, node_id, battery_pct, voltage, rssi, snr) "
"VALUES (?, ?, ?, ?, ?, ?)",
(ts, from_id,
tel.get('batteryLevel'),
tel.get('voltage'),
rssi, snr)
)
con.commit()
print(f"[{ts}] Telemetry from {from_id}: "
f"batt={tel.get('batteryLevel')}% rssi={rssi}")
elif portnum == 'TEXT_MESSAGE_APP':
text = decoded.get('text', '')
cur = con.cursor()
cur.execute(
"INSERT INTO messages (ts, from_id, channel, text) VALUES (?, ?, ?, ?)",
(ts, from_id, packet.get('channel', 0), text)
)
con.commit()
print(f"[{ts}] Message from {from_id}: {text}")
pub.subscribe(on_receive, "meshtastic.receive")
iface = meshtastic.serial_interface.SerialInterface(SERIAL_PORT)
print("Gateway running. Ctrl+C to stop.")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
iface.close()
con.close()
print("Stopped.")
MQTT Forwarding from the Pi
A USB-connected Pico talks to the Pi over serial; it cannot reach the Pi's localhost MQTT broker on its own. Let the Python process running on the Pi publish the packets it receives.
# Install a local broker on the Pi $ sudo apt install mosquitto mosquitto-clients -y $ sudo systemctl enable --now mosquitto # In the same Python virtual environment as gateway.py: $ python -m pip install paho-mqtt
# Add a small publisher to gateway.py (this code runs on the Pi):
import json
import paho.mqtt.publish as publish
def publish_local(topic, payload):
publish.single(
topic,
payload=json.dumps(payload),
hostname="127.0.0.1",
)
# Call it from on_receive after validating and storing a packet:
# publish_local("field/telemetry", decoded)
# publish_local("field/text", decoded)
# Watch the local messages: $ mosquitto_sub -h 127.0.0.1 -t "field/#" -v
Bind a private broker to the loopback interface unless another protected machine truly needs access. If MQTT must leave the Pi, add authentication, TLS, and a clear topic policy. A Pico W can use a reachable network broker when configured for Wi‑Fi, but a plain Pico cannot.
Running the Logger as a systemd Service
# /etc/systemd/system/meshtastic-gateway.service [Unit] Description=Meshtastic Gateway Logger After=network-online.target Wants=network-online.target [Service] Type=simple User=pi WorkingDirectory=/home/pi ExecStart=/home/pi/meshtastic-env/bin/python3 /home/pi/gateway.py Restart=on-failure RestartSec=10 [Install] WantedBy=multi-user.target
Keep the serial device name, database path, and log location in one documented place. Watch the service with journalctl after every reboot and after reconnecting the USB radio.
$ sudo systemctl daemon-reload $ sudo systemctl enable meshtastic-gateway $ sudo systemctl start meshtastic-gateway $ sudo systemctl status meshtastic-gateway $ journalctl -u meshtastic-gateway -f # live logs
Querying Logged Data
$ sqlite3 /home/pi/meshtastic-data.db
# Recent telemetry from all nodes
SELECT datetime(ts, 'unixepoch', 'localtime') AS time,
node_id, battery_pct, voltage, rssi, snr
FROM telemetry
ORDER BY ts DESC
LIMIT 20;
# Average battery per node
SELECT node_id,
round(avg(battery_pct), 1) AS avg_batt,
round(min(battery_pct), 1) AS min_batt,
count(*) AS samples
FROM telemetry
WHERE ts > strftime('%s', 'now') - 86400 -- last 24 hours
GROUP BY node_id;
# Recent messages
SELECT datetime(ts, 'unixepoch', 'localtime'), from_id, text
FROM messages
ORDER BY ts DESC LIMIT 10;
Remote Access to the Gateway
# SSH tunnel — forward Pi's port 8080 (Grafana, etc.) to local machine $ ssh -L 8080:localhost:3000 pi@pi5-gateway.local # Or use a VPN (WireGuard) for persistent remote access: $ sudo apt install wireguard # Configure WireGuard to create a permanent tunnel to a VPS # Then access the Pi via its WireGuard IP from anywhere # Tailscale — easiest option for remote access without VPS: $ curl -fsSL https://tailscale.com/install.sh | sh $ sudo tailscale up # Pi is now accessible from anywhere via Tailscale's mesh VPN
Operational Checks
- Confirm the gateway sees the expected USB radio after each reboot.
- Alert on a missing telemetry report instead of only checking for process uptime.
- Check database size and free disk space; logs should not consume the system disk indefinitely.
- Keep a backup of the database and the Meshtastic configuration separately.
- Apply operating-system and Meshtastic updates deliberately, with a rollback note for field hardware.
The most useful gateway is boring: it restarts after a power cut, identifies its radio consistently, stores its data safely, and makes an outage obvious.
dispelled