python backend arduino service

This commit is contained in:
Mikkeli Matlock
2026-01-26 11:52:29 +09:00
parent 2a379ede20
commit 8f22966eb0
5 changed files with 252 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
# Backend
Python GPS service for Smart Serow. Connects to `gpsd`, buffers positions, exposes HTTP API.
Python GPS and Arduino telemetry service for Smart Serow. Connects to `gpsd` and Arduino serial, buffers data, exposes HTTP API.
## Setup
@@ -28,9 +28,11 @@ uv run flask --app main run --host 0.0.0.0 --port 5000 --reload
| Endpoint | Description |
|----------|-------------|
| `GET /health` | Health check, shows gpsd connection status |
| `GET /health` | Health check, shows gpsd and Arduino connection status |
| `GET /gps` | Latest GPS fix (lat, lon, alt, speed, track) |
| `GET /gps/history` | Last 100 buffered positions |
| `GET /gps/history` | Last 100 buffered GPS positions |
| `GET /arduino` | Latest Arduino telemetry (voltage, rpm, eng_temp, gear) |
| `GET /arduino/history` | Last 100 buffered Arduino readings |
## Test from SSH
@@ -38,6 +40,8 @@ uv run flask --app main run --host 0.0.0.0 --port 5000 --reload
curl http://localhost:5000/health
curl http://localhost:5000/gps
curl http://localhost:5000/gps/history | jq
curl http://localhost:5000/arduino
curl http://localhost:5000/arduino/history | jq
```
## gpsd Setup (Pi)
@@ -62,9 +66,48 @@ gpsmon
cgps -s
```
## Arduino Setup (Pi)
The Arduino Nano connects via USB serial (typically `/dev/ttyUSB0` or `/dev/ttyACM0`).
```bash
# Check available ports
ls /dev/tty*
# May need dialout group for serial access
sudo usermod -aG dialout $USER
# Then log out and back in
```
### Arduino Protocol
**Production format (JSON at 115200 baud):**
```json
{"v":12.45,"rpm":4500,"eng":85,"gear":3}
```
**Legacy text format (also supported):**
```
V_bat: 12.45V
RPM: 4500
ENG: 85C
```
The parser tries JSON first, falls back to regex for legacy format.
### Configuring the Port
Edit `main.py` to change the default port:
```python
arduino = ArduinoService(port="/dev/ttyACM0", baudrate=115200)
```
## Stub Mode
If `gpsdclient` isn't installed or gpsd isn't running, the service generates fake GPS data for UI testing.
- **GPS**: If `gpsdclient` isn't installed or gpsd isn't running, generates fake GPS data
- **Arduino**: If `pyserial` isn't installed or serial port unavailable, generates fake telemetry
Both services run in stub mode for UI testing without hardware.
## Deploy