Skip to content

rymikula/hw4iot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Door Open/Close Detection (Step-by-step)

Clear, device-specific steps to get everything running. Two setups are provided:

  • PC-only with simulator (fastest)
  • Raspberry Pi with real IMU + PC for broker/cloud/web

What runs where

  • PC (Windows/macOS/Linux) with Docker Desktop:
    • Mosquitto MQTT broker (container)
    • Cloud forwarder (container)
    • Web dashboard (container)
  • Raspberry Pi (optional):
    • Device publisher reading the IMU (native Python on Pi)

A) PC-only quick start (simulator)

  1. On PC: install and start Docker Desktop.
  2. On PC: open a terminal in the project folder (e.g., E:\hw4iot).
  3. On PC (optional): create .env in repo root to tweak defaults:
MQTT_BROKER=broker
DEVICE_ID=door001
SAMPLING_RATE_HZ=50
GYRO_START_THRESHOLD_DPS=30
GYRO_STOP_THRESHOLD_DPS=10
MIN_EVENT_DURATION_S=0.3
MAX_EVENT_DURATION_S=5
QUIET_TIME_AFTER_EVENT_S=1
# MODEL_PATH=/app/model.joblib  # if you add a trained model
  1. On PC: start the full stack (broker + cloud + device [sim] + web):
docker compose up --build
  1. On PC: open the dashboard: http://localhost:8080.
    • You should see simulated open/close events updating the UI.
  2. On PC (optional): view MQTT traffic from another terminal:
docker run --rm -it --network host eclipse-mosquitto:2 mosquitto_sub -h localhost -t 'door/#' -v
  1. On PC: stop everything when done:
docker compose down

B) Real IMU on Raspberry Pi + PC (broker/cloud/web)

Prerequisites (hardware on Pi):

  • Enable I2C: sudo raspi-config → Interface Options → I2C → Enable → reboot.
  • Wire MPU6050 to Pi: VCC→3.3V, GND→GND, SCL→GPIO3, SDA→GPIO2.

Steps:

  1. On PC: start Docker stack (broker + cloud + web; device will still run in simulator in Docker but we’ll ignore it):
docker compose up --build
  1. On PC: find your PC IP address (for Pi to connect to broker):
    • Windows: ipconfig → use your LAN IPv4 (e.g., 192.168.1.50).
  2. On Raspberry Pi: get the code onto the Pi (copy the repo or sync only device/, common/, requirements.txt).
  3. On Raspberry Pi: install Python deps and run the device publisher against the PC broker:
python3 -m venv .venv
. .venv/bin/activate
pip install -r requirements.txt
export MQTT_BROKER=YOUR_PC_IP   # e.g., 192.168.1.50
export DEVICE_ID=door001
python device/publisher.py
  • The device will auto-detect the MPU6050 (if present) and stop using the simulator.
  1. On PC: open http://localhost:8080 to see live updates from the Pi.
  2. Keep the Pi process running (or supervise via screen, tmux, or a systemd service).

C) Train an SVM model and deploy it

You can train locally on the PC or inside a container. Collection should run where the IMU is (typically on the Pi).

Collect data (on Raspberry Pi with sensor attached):

python tools/collect.py open 120 data_open.csv
python tools/collect.py close 120 data_close.csv
python tools/collect.py none 60 data_none.csv

Train the model (on PC):

python -m venv .venv
. .venv/Scripts/activate   # Windows PowerShell: .venv\Scripts\Activate.ps1
pip install -r requirements.txt
python tools/train.py data_open.csv data_close.csv data_none.csv --out model.joblib

Use the trained model:

  • If using the Docker device (simulator or future hardware passthrough): place model.joblib in repo root and rebuild:
docker compose build device
docker compose up -d device

Or mount without rebuild via docker-compose.override.yml:

services:
  device:
    volumes:
      - ./model.joblib:/app/model.joblib:ro
    environment:
      - MODEL_PATH=/app/model.joblib
  • If running the device on the Raspberry Pi natively: copy model.joblib to the Pi and either place it next to device/publisher.py or set:
export MODEL_PATH=/full/path/to/model.joblib
python device/publisher.py

D) Reference

  • MQTT topics:
    • Device decisions: door/device/{DEVICE_ID}/decision
    • Cloud events (retained): door/events
  • JSON payload example:
{"device_id":"door001","label":"open","confidence":0.93,"timestamp":"2025-10-27T12:34:56","duration_s":0.84}
  • Logs (PC):
docker compose logs -f broker cloud device web
  • Tuning thresholds: edit .env (for Docker) or set env vars (for Pi). See keys in common/config.py.

E) Troubleshooting

  • No dashboard updates: confirm cloud and device are running; check docker compose ps and logs.
  • No messages on door/events: ensure broker is healthy and cloud subscribed to door/device/+/decision.
  • Over/under detection: adjust GYRO_START_THRESHOLD_DPS, GYRO_STOP_THRESHOLD_DPS, and timing windows; retrain model if needed.
  • Wrong labels: collect more representative data and retrain; ensure consistent IMU mounting orientation.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages