added DEPLOYMENT.md

This commit is contained in:
grove5_admin committed 2026-04-27 19:52:38 -07:00
1 parent 27b10b407b
commit eaaefb9298
1 file changed
+431
+431
View File
@@ -0,0 +1,431 @@
# Cultivar — Deployment Guide
Build and deploy from source on bare metal.
Grove5 — Open source software, honest hardware.
https://grove5.com
---
## Overview
The Cultivar stack runs on an **Arduino UNO Q** — a dual-brain board with an STM32U585 MCU and a QRB2210 Linux processor. Two ENV-1-V0.1 sensor boards connect via I2C, one per chamber.
```
ENV-1 (colonization) → STM32U585 (MCU) → Serial1 → QRB2210 (Linux)
ENV-1 (fruiting) → STM32U585 (MCU) → Serial2 → ├── cultivar-daemon
└── cultivar-ui
```
---
## Prerequisites
### Hardware
- [ ] Arduino UNO Q (2GB or 4GB)
- [ ] 2× ENV-1-V0.1 sensor boards
- [ ] Colonization: BME690 populated, SCD43 DNP
- [ ] Fruiting: BME690 + SCD43 both populated
- [ ] I2C wiring per chamber (SDA, SCL, 3.3V, GND)
- [ ] USB-C cable + PD dongle (required to power UNO Q)
### Development machine
- [ ] Arduino IDE 2.x
- [ ] Python 3.11+
- [ ] Node.js 20+
- [ ] Git
### Accounts / access
- [ ] Gitea access to `grove5` org
- [ ] SSH key configured for Gitea
---
## Repository setup
```bash
# Clone all four repos into a common parent directory
mkdir cultivar && cd cultivar
git clone [email protected]:grove5/cultivar-types.git
git clone [email protected]:grove5/cultivar-firmware.git
git clone [email protected]:grove5/cultivar-daemon.git
git clone [email protected]:grove5/cultivar-ui.git
```
---
## 1. Firmware
### Arduino IDE board setup
| Setting | Value |
|---------|-------|
| Board | ESP32S3 Dev Module (dev) / STM32U585 (production UNO Q) |
| USB CDC On Boot | Enabled |
| Flash Size | 4MB |
| PSRAM | OPI PSRAM |
| Upload Speed | 921600 |
### Install dependencies
Via Arduino Library Manager:
- [ ] **Sensirion I2C SCD4x** by Sensirion
Manual install (copy into `cultivar_env/src/`):
- [ ] Download [Bosch BME690 SensorAPI v1.1.0](https://github.com/boschsensortec/BME690_SensorAPI)
- [ ] Copy `bme69x.h`, `bme69x.c`, `bme69x_defs.h` into `cultivar-firmware/lib/cultivar_env/src/`
Install `cultivar_env` library:
- [ ] Arduino IDE → Sketch → Include Library → Add .ZIP Library
- [ ] Select `cultivar-firmware/lib/cultivar_env/`
### Flash
- [ ] Connect ESP32S3 (dev) or UNO Q via USB-C
- [ ] Open `cultivar-firmware/cultivar_sensor/cultivar_sensor.ino`
- [ ] Set collection interval:
- Dev/test: `#define COLLECT_INTERVAL_MS (30UL * 1000UL)`
- Production: `#define COLLECT_INTERVAL_MS (5UL * 60UL * 1000UL)`
- [ ] Upload
- [ ] Open Serial Monitor at 115200 baud
- [ ] Confirm I2C scan shows expected devices:
- Colonization board: `0x62 ← SCD43` should NOT appear, `0x76 ← BME690` should appear
- Fruiting board: both `0x62` and `0x76` should appear
- [ ] Confirm JSON payloads appear with correct `chamber_id`
- [ ] Save 5–10 JSON lines to `cultivar-daemon/samples/colonization.jsonl` and `fruiting.jsonl`
---
## 2. Daemon
### Shared types symlink
```bash
cd cultivar-daemon
ln -s ../cultivar-types/python/cultivar_types.py cultivar_types.py
```
### Install Python dependencies
```bash
# Development (Mac/Linux)
pip install -r requirements.txt
# UNO Q Debian
pip install -r requirements.txt --break-system-packages
```
### Verify serial ports (UNO Q)
```bash
ls /dev/tty* | grep -E 'S|USB|AMA'
# Expected: /dev/ttyS0, /dev/ttyS1 (or similar)
# Confirm which port is which by unplugging one board at a time
```
Update environment variables if ports differ from defaults:
```bash
export COLONIZATION_PORT=/dev/ttyS0
export FRUITING_PORT=/dev/ttyS1
```
### Test — replay mode (no hardware)
```bash
cd cultivar-daemon
REPLAY_MODE=true \
ENABLE_FRUITING=false \
REPLAY_COLONIZATION=samples/colonization.jsonl \
REPLAY_DELAY=3.0 \
python cultivar_daemon.py
```
- [ ] Daemon starts without errors
- [ ] `http://localhost:8000/api/health` returns `{"status": "ok"}`
- [ ] `http://localhost:8000/docs` shows API documentation
- [ ] After 6 payloads: `http://localhost:8000/api/baseline/colonization` shows `established: true`
- [ ] `http://localhost:8000/api/environment/colonization/history` returns readings
- [ ] `http://localhost:8000/api/environment/colonization/sweep` returns sweep points
### Test — live hardware
```bash
cd cultivar-daemon
COLONIZATION_PORT=/dev/ttyS0 \
FRUITING_PORT=/dev/ttyS1 \
python cultivar_daemon.py
```
- [ ] Both chambers connect
- [ ] Payloads appear in logs every 5 minutes
- [ ] SQLite database created at `cultivar.db`
- [ ] API endpoints return live data
### SQLite quick checks
```bash
sqlite3 cultivar.db
# Check schema version
SELECT * FROM schema_version;
# Check payloads flowing
SELECT chamber_id, collected_at, temp_c, humidity_pct, co2_ppm
FROM payloads ORDER BY collected_at DESC LIMIT 10;
# Check sweep points
SELECT chamber_id, heater_temp_c, zone, resistance_raw, valid
FROM sweep_points ORDER BY collected_at DESC LIMIT 20;
# Check baseline
SELECT chamber_id, n_samples, humidity_ref, temp_ref, established_at
FROM baselines WHERE active = 1;
# Check anomalies
SELECT chamber_id, type, severity, message, detected_at
FROM anomalies WHERE dismissed = 0;
```
---
## 3. UI
### Install dependencies
```bash
cd cultivar-ui
npm install
```
### Development (mock data)
```bash
npm run dev
# Opens at http://localhost:5173
# Uses mock.ts — no daemon required
```
### Development (live daemon)
```bash
VITE_DAEMON_URL=http://localhost:8000 npm run dev
```
- [ ] Overview page loads
- [ ] Environment readings update in real time
- [ ] Cultivar Rhythm panel shows anomalies if any detected
- [ ] Field Guide loads species and contaminant data
### Build for production
```bash
npm run build
# Output: dist/
```
Copy `dist/` to the UNO Q so the daemon can serve it:
```bash
rsync -av dist/ [email protected]:/home/user/cultivar-daemon/cultivar-ui/dist/
```
- [ ] `http://cultivar.local:8000` serves the UI
- [ ] WebSocket connects (Live indicator shows green)
- [ ] Both chamber environment panels populate
---
## 4. Systemd service (UNO Q production)
Create `/etc/systemd/system/cultivar.service`:
```ini
[Unit]
Description=Cultivar Daemon
After=network.target
Wants=network.target
[Service]
Type=simple
User=grove5
WorkingDirectory=/home/grove5/cultivar-daemon
Environment=COLONIZATION_PORT=/dev/ttyS0
Environment=FRUITING_PORT=/dev/ttyS1
Environment=API_PORT=8000
Environment=DB_PATH=/home/grove5/cultivar-daemon/cultivar.db
ExecStart=/usr/bin/python3 cultivar_daemon.py
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
```
```bash
sudo systemctl daemon-reload
sudo systemctl enable cultivar
sudo systemctl start cultivar
# Check status
sudo systemctl status cultivar
# Follow logs
sudo journalctl -u cultivar -f
```
- [ ] Service starts on boot
- [ ] Service restarts automatically on crash
- [ ] Logs visible via journalctl
---
## 5. Network access
Set a static hostname on the UNO Q so the UI is reachable at `http://cultivar.local`:
```bash
sudo hostnamectl set-hostname cultivar
```
Or assign a static IP on your router and add a hosts entry:
```
192.168.x.x cultivar.local
```
- [ ] `http://cultivar.local:8000` reachable from tablet/desktop on same network
- [ ] `http://cultivar.local:8000/api/health` returns ok
---
## Trial checklist
Before running a cultivation trial:
### Hardware
- [ ] Both ENV-1 boards powered and scanning correctly
- [ ] Firmware flashed and outputting JSON with correct `chamber_id`
- [ ] Serial ports confirmed on UNO Q
### Daemon
- [ ] Daemon running (manually or via systemd)
- [ ] Both chambers receiving data
- [ ] API returning live readings
- [ ] `cultivar.db` growing in size
### Baseline
- [ ] Wait for baseline to establish (~30 min at 5-min intervals)
- [ ] Confirm `GET /api/baseline/colonization` and `/fruiting` both show `established: true`
- [ ] Zone scores appearing in payloads
### UI
- [ ] UI loads and WebSocket shows Live
- [ ] Both chamber environment panels showing real data
- [ ] 1h/6h/24h history selector working
- [ ] Anomaly panel clear (or showing expected calibration-phase info alerts)
### Run
- [ ] Start a cultivation run via API or UI:
```bash
curl -X POST http://localhost:8000/api/runs \
-H "Content-Type: application/json" \
-d '{"chamber_id": "colonization", "species_id": "SP-001", "species_name": "Blue Oyster", "substrate": "Wheat straw"}'
```
- [ ] Confirm run appears in `GET /api/runs/colonization/active`
---
## Troubleshooting
### No serial data
```bash
# Check port exists
ls -la /dev/ttyS*
# Check permissions
sudo usermod -aG dialout $USER
# Test port directly
cat /dev/ttyS0
```
### BME690 not found on I2C scan
- Confirm 3.3V power to ENV-1
- Check SDA/SCL wiring (GPIO42/41 on ESP32S3)
- Try 0x77 address if 0x76 fails
### Gas resistance stuck at 102400000
- Normal on first power-on — allow 15-30 min burn-in
- Expected in very clean air environments
- Breathe on sensor to confirm heater is firing
### Baseline not establishing
- Check humidity variance: if RH is swinging >5% the gate won't pass
- Lower `BASELINE_HUM_VARIANCE` environment variable temporarily
- Check `BASELINE_MIN_SAMPLES` — default is 6 (30 min at 5-min intervals)
### WebSocket not connecting
- Confirm `VITE_DAEMON_URL` is set correctly
- Check CORS — daemon allows all origins by default
- Confirm daemon is running: `curl http://localhost:8000/api/health`
### Database issues
```bash
# Check integrity
sqlite3 cultivar.db "PRAGMA integrity_check;"
# Check schema version
sqlite3 cultivar.db "SELECT * FROM schema_version;"
# Nuclear option — delete and let daemon recreate
# WARNING: loses all cultivation history
rm cultivar.db
```
---
## File locations (UNO Q production)
```
/home/grove5/
├── cultivar-daemon/
│ ├── cultivar_daemon.py
│ ├── cultivar_types.py ← symlink
│ ├── cultivar.db ← never commit
│ ├── samples/ ← .jsonl replay files
│ └── cultivar-ui/
│ └── dist/ ← built Svelte UI
└── cultivar-firmware/ ← source reference only
```
---
## Version reference
| Component | Version |
|-----------|---------|
| cultivar-daemon | 0.2.0 |
| cultivar-ui | 0.1.0-alpha |
| cultivar-firmware / cultivar_env | 0.2.0 |
| cultivar-types | 0.1.0 |
| FastAPI | 0.136.1 |
| Pydantic | 2.13.3 |
| aiosqlite | 0.22.1 |
| uvicorn | 0.46.0 |
| pyserial-asyncio | 0.6 |
| Bosch BME690 SensorAPI | 1.1.0 |
| Sensirion I2C SCD4x | latest |