Start data Logging subproject

This commit is contained in:
Andreas committed 2026-04-14 13:39:03 +02:00
1 parent 011a4171cc
commit a6b31fe5bf
5 files changed
+395 -4

No files matched your search

+188
View File
@@ -0,0 +1,188 @@
# Data Logging
Captures all serial debug output from the Arduino and writes it to a timestamped
log file. Designed to run automatically on a Raspberry Pi with no user interaction.
---
## Files
| File | Purpose |
|---|---|
| `log_serial.py` | Python logger script |
| `arduino-logger.service` | systemd unit file — runs the script on boot |
| `arduino.log` | Log output (created automatically on first run) |
---
## How it works
- `log_serial.py` auto-detects the Arduino's USB serial port on startup.
- Every line received from the Arduino is written to `arduino.log` with a
full timestamp (`YYYY-MM-DD HH:MM:SS.mmm`).
- The log file is always **appended**, never overwritten — all sessions are
preserved in one file.
- If the Arduino is restarted or unplugged, the script reconnects automatically
within a few seconds.
- If the Pi is restarted, systemd brings the script back up on boot.
---
## Log file format
```
────────────────────────────────────────────────────────────
Logger started 2026-04-14 19:30:00
────────────────────────────────────────────────────────────
[2026-04-14 19:30:01.042] [CONNECT] Opening /dev/ttyACM0 at 9600 baud …
[2026-04-14 19:30:01.108] [CONNECT] Connected to /dev/ttyACM0
[2026-04-14 19:30:05.210] ----------------------------
[2026-04-14 19:30:05.212] [BUTTON] Q2 / Button 3 (pin D7)
[2026-04-14 19:30:05.213] [RESULT] HALF-CORRECT
[2026-04-14 19:30:05.215] [TEMP] 4 → 5 sections
[2026-04-14 19:30:05.217] [STATE] Q1:- Q2:HALF Q3:OK Q4:WRONG
[2026-04-14 19:30:12.001] [DISCONNECT] Lost connection to /dev/ttyACM0: …
[2026-04-14 19:30:12.002] [RECONNECT] Retrying in 3s …
[2026-04-14 19:30:15.110] [CONNECT] Connected to /dev/ttyACM0
```
---
## Raspberry Pi setup
### 1. Install Python dependency
```bash
pip install pyserial
```
### 2. Add your user to the `dialout` group
This allows the script to access serial ports without root.
```bash
sudo usermod -aG dialout pi
```
Log out and back in (or reboot) for the group change to take effect.
### 3. Copy the repo to the Pi
```bash
# On the Pi, clone the repo or copy the folder.
# The path used in the service file is /home/pi/ExploringUncertanties — adjust if needed.
git clone <your-repo-url> /home/pi/ExploringUncertanties
```
### 4. Edit the service file
Open `arduino-logger.service` and update the two lines that reference paths/username:
```ini
User=pi # ← change if your Pi username is different
ExecStart=/usr/bin/python3 /home/pi/ExploringUncertanties/dataLogging/log_serial.py
```
Check where Python 3 lives on your Pi:
```bash
which python3 # typically /usr/bin/python3
```
### 5. Install and enable the service
```bash
# Copy the unit file to systemd
sudo cp /home/pi/ExploringUncertanties/dataLogging/arduino-logger.service \
/etc/systemd/system/arduino-logger.service
# Reload systemd so it sees the new file
sudo systemctl daemon-reload
# Enable the service (auto-start on every boot)
sudo systemctl enable arduino-logger
# Start it now without rebooting
sudo systemctl start arduino-logger
```
### 6. Verify it is running
```bash
sudo systemctl status arduino-logger
```
You should see `Active: active (running)`.
---
## Useful commands
```bash
# Check current status
sudo systemctl status arduino-logger
# See the last 50 lines of systemd output (useful for debugging startup)
journalctl -u arduino-logger -n 50
# Follow live output in the terminal
journalctl -u arduino-logger -f
# Or tail the log file directly
tail -f /home/pi/ExploringUncertanties/dataLogging/arduino.log
# Stop the service
sudo systemctl stop arduino-logger
# Disable auto-start (won't start on next boot)
sudo systemctl disable arduino-logger
```
---
## Port auto-detection
The script finds the Arduino port automatically in this order:
1. Any port whose description contains **"Arduino"** (official boards)
2. Any port whose description contains **"CH340"** or **"CH341"** (common clones)
3. Any **ttyACM\*** or **ttyUSB\*** device (fallback for any USB-serial adapter)
If no port is found, it retries every 5 seconds and logs a `[WAIT]` message.
Once the Arduino is plugged in it connects without any user action.
To force a specific port (e.g. if you have two USB serial devices):
```bash
# Edit the ExecStart line in the service file:
ExecStart=/usr/bin/python3 /home/pi/ExploringUncertanties/dataLogging/log_serial.py --port /dev/ttyACM1
```
---
## Reconnection behaviour
| Event | What happens |
|---|---|
| Arduino restarted (button or power cycle) | Script detects disconnect, waits 3 s, reconnects |
| Arduino unplugged and replugged | Script scans for the port again, reconnects |
| Pi restarted / powered on | systemd starts the script automatically after boot |
| Script crashes | systemd restarts it after 5 s (`Restart=always`) |
---
## Manual usage (without systemd)
```bash
# Auto-detect port, log to default file
python3 log_serial.py
# Specific port
python3 log_serial.py --port /dev/ttyACM1
# Different log file
python3 log_serial.py --out /home/pi/logs/session.log
# List available ports
python3 log_serial.py --list-ports
```