# Demeter Server

## Devices under test

| Device name |     MAC      |       Serial       | FW version |      Note      | Flashed |
|-------------|--------------|--------------------|------------|----------------|---------|
|  demeter_1  | 28848590D334 | ESP32S3_5B90154256 | 20260803 | Not powered    |    x    |
|  demeter_2  | A4CB8FD77608 | ESP32S3_5B90153402 | 20260808 | /w SQ5 Yannick |    x    |
|  demeter_3  | 28848590D30C | ESP32S3_5B90154282 | 20260803 | /w SQ5 David   |    x    |
|  demeter_4  | A4CB8FD77674 | ESP32S3_5B90153436 | 20260808 | NO SQ5 wired   |    x    |
|  demeter_5  | 28848590D3E0 | ESP32S3_5B90154229 | 20260803 | Not powered    |    x    |
|  demeter_6  | A4CB8FD77554 | ESP32S3_5B90153370 | 20260803 | NO SQ5 wired   |    x    |
|  demeter_7  | A4CB8FD772C8 | ESP32S3_5B79023936 | 20260808 | /w SQ5 David   |    x    |
|  demeter_8  | A0F262E3AC58 | ESP32S3_5B91047762 | 20260803 | Not powered    |    x    |
|  demeter_9  | A0F262E3AC60 | ESP32S3_5B91047760 | 20260803 | Not powered    |    x    |
|  demeter_10 | 28848590D3D0 | ESP32S3_5B90154228 | 20260803 | Not powered    |    x    |
|  gmr_1      | CD3AFA190000 | ESP32S3_5B5E128327 | 20260808 | /w SQ5 David   |    x    |

NOTE : gmr_1 is ESP32P4-SQ-Display
TODO: enumerating script with esptool command for ESP32P4

## ESPConnect (remote)

Browser UI for ESP32-S3 boards attached to demeter.

- URL: **https://demeter:8082/** (Caddy HTTPS with internal CA → Vite → remote serial bridge)
- Alternate HTTP: **http://demeter:5173/**
- Trust the HTTPS cert once: download **http://demeter:8080/caddy-local-root.crt** and install as a trusted root CA on your PC
- Click **Connect** to pick a live board (demeter_2, demeter_3, ...) and talk to it over the network.

### Services

| Unit | Role | Port |
|------|------|------|
| `espconnect-remote.service` | Device list + WebSocket serial bridge | `127.0.0.1:8090` |
| `espconnect-vite.service` | ESPConnect frontend (Vite) | `0.0.0.0:5173` |
| Caddy `demeter:8082` | HTTPS reverse proxy to Vite | `8082` |

```bash
sudo systemctl status espconnect-remote espconnect-vite
curl -s http://127.0.0.1:8090/api/devices
```

API:
- `GET /api/health`
- `GET /api/devices` — currently connected ESP32-S3 (mapped to demeter_N names)
- `WS /api/serial?device=demeter_2` — binary serial bridge


# Utils

## Proxy 
To expose the HTTP server running on demeter using tailscale to the rest of the internet
```
sudo tailscale funnel --bg 8080
```
Available on the internet:
https://demeter.tailb8cd1e.ts.net/
|-- proxy http://127.0.0.1:8080

Funnel started and running in the background.
To disable the proxy, run: tailscale funnel --https=443 off

## Server

**Host a web server forwarded through tailscaled**

### Run the command from the Server folder
```
python3 -m http.server 8080
```

## Flash a single SQ-Link
### Run the flashing command from the **bin** folder
Identify the port of your device is connected to, first hand

1. Identify current device \
unplug your device
```
dmesg -w
```
plug your device

You should see the path of the device it has been enumerated.

2. List all devices connected to /dev/ttyACM*

```
for dev in /dev/ttyACM*; do     echo "=== $dev ===";     udevadm info -a -n $dev | grep -E 'serial|product|manufacturer|idVendor|idProduct' | head -5;     echo; done
```

Identify if the current controler is an ESP32S3 or ESP32P4 
```
esptool --port /dev/ttyACM* chip-id
``` 

3. Flashing command
```
esptool --chip esp32s3 --port /dev/ttyACM# --baud 921600 --before default_reset --after hard_reset -v write_flash -z --flash_mode dio --flash_freq 80m --flash_size detect 0x0 bootloader.bin 0x8000 partitions.bin 0xE000 boot_app0.bin 0x10000 firmware.bin
```



## SQLinkLogger

```
	./SQLinkLogger /path/to/device outputLogFileName.txt
	./SQLinkLogger /dev/ttyAMC8  
```

### Auto-enumerating rule

The waveshare board has an USB multiplexor with this signature.

```
    ATTRS{idProduct}=="55d5"
    ATTRS{idVendor}=="1a86"
    ATTRS{manufacturer}=="WCH.CN"
    ATTRS{product}=="USB Quad_Serial"
    ATTRS{serial}=="BC0432ABCD"
```

Create a rule in /etc/udev/rules.d/
```
	sudo nano /etc/udev/rules.d/100-esp32.rules
```

## What COM Port is the SQ-Link connected to ?


### Set the server as a systemd sevice

The service is called "bench-server.service" \
bench-server.service file should exist in /etc/systemd/system/

```
[Unit]
Description=My HTTP Web Server
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=recomputer
WorkingDirectory=/home/recomputer/server
ExecStart=/usr/bin/python3 -m http.server 8080
Restart=on-failure
RestartSec=3s

[Install]
WantedBy=multi-user.target
```

Useful systemctl commands

```
# Reload the systemd manager configuration
sudo systemctl daemon-reload

# Start HTTP bench-server service immediately
sudo systemctl start bench-server.service

# Enable the server to run automatically on system boot
sudo systemctl enable bench-server.service

# Verify server health
sudo systemctl status bench-server.service

# View real-time server log
sudo journalctl -u bench-server.service -f

# Stop service
sudo systemctl stop bench-server.service

# Restart service
sudo systemctl restart bench-server.service
```



Note : 

Déméter est la déesse grecque antique de l'agriculture, des céréales et de la fertilité de la terre. Elle incarne la force nourricière qui soutient la vie à travers la croissance des récoltes et le cycle des saisons. En tant que l'une des douze divinités de l'Olympe, elle est représentée comme une femme mûre tenant souvent des symboles tels que des gerbes de blé, des torches et des pavots, qui représentent sa domination sur les moissons et les mystères de la végétation. Son nom, dérivé de « Da-mētēr » signifiant « Terre-Mère », souligne son lien profond avec le sol et les richesses de la terre.

![Demeter](../docs/demeter.jpg)
