This repository contains a demo IoT command center with a FastAPI backend and a React dashboard. The backend exposes REST, WebSocket and Server-Sent Event (SSE) interfaces that push real-time updates to the dashboard. A simulation mode periodically generates environmental readings, device status changes and alarm events so the entire stack can be demonstrated without connecting to real devices.
The backend lives in backend/ and is built with FastAPI. It exposes REST endpoints under /api for querying recent data and provides a WebSocket endpoint at /api/ws plus an SSE stream at /api/events. A background scheduler ingests incoming payloads, persists them to SQLite and broadcasts the events to connected clients.
cd backend
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
uvicorn app.main:app --reloadEnvironment variables prefixed with IOT_BOARD_ can be used to override configuration, for example IOT_BOARD_SIMULATION_MODE=false to disable the synthetic data generator.
The frontend lives in frontend/ and is a small Vite + React project. It uses a dedicated realtime service (src/services/realtime.ts) that handles WebSocket lifecycles, automatic reconnection and SSE fallback. Dashboard widgets subscribe to relevant events and refresh themselves instantly when new payloads arrive.
cd frontend
npm install
npm run devThe dev server proxies API calls to http://localhost:8000 by default.
- When the backend starts, a simulation worker pushes demo data every few seconds. This keeps the dashboard lively in demos.
- The backend uses SQLite via SQLAlchemy's async engine. Database schema is created automatically on startup.
- Realtime broadcasts are logged in the
realtime_dispatch_logtable for traceability.
The repository ships with a full testing stack covering the backend, frontend and end-to-end flows. All commands below assume the repository root as the working directory.
cd backend
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
pytestThese tests exercise the async data ingestion pipeline, API endpoints and the simulation-mode switch using an isolated SQLite database.
cd frontend
npm install
npm run test:runVitest together with Testing Library validates component state management, realtime event subscriptions and dashboard wiring.
cd frontend
npm install
npx playwright install --with-deps
npm run test:e2ePlaywright boots the Vite dev server, stubs backend APIs and verifies the dashboard’s critical user journeys including realtime mode transitions.
A GitHub Actions workflow (.github/workflows/ci.yml) runs linting, backend pytest, frontend unit tests, the production build and Playwright end-to-end checks on every push. The workflow definition can be used as a template for other CI platforms.
This repository contains the data access layer for an agriculture IoT board. It uses SQLAlchemy as the ORM, Alembic for migrations, and targets a PostgreSQL database by default (any SQLAlchemy-compatible database such as MySQL can also be used).
backend/
├── __init__.py
├── alembic/
│ ├── env.py
│ ├── versions/
│ │ └── 20240229_0001_initial.py
│ └── alembic.ini
├── config.py
├── crud.py
├── database.py
├── models.py
├── requirements.txt
└── seed.py
| Table | Purpose | Key fields |
|---|---|---|
fields |
Basic information about managed plots | name, location, area_hectares, soil_type, created_at |
crops |
Crops planted within a field | field_id, name, variety, planting_date, growth_stage, expected_harvest_date |
devices |
IoT hardware installed on a field | field_id, name, device_type, manufacturer, status, installed_at |
sensor_readings |
Measurements recorded by devices | device_id, sensor_type, value, unit, recorded_at, notes |
operations |
Operational log for field/crop maintenance | field_id, crop_id, operation_type, description, performed_at, operator |
Relationships between entities are defined using SQLAlchemy ORM relationships
and exposed through the convenience CRUD helpers in backend/crud.py. These
functions are meant to be shared by both the administration backend and large
screen APIs, providing a single source of truth for data access.
python -m venv .venv
source .venv/bin/activate
pip install -r backend/requirements.txt
Set the DATABASE_URL environment variable to point to your PostgreSQL or
MySQL instance, e.g.
export DATABASE_URL=postgresql+psycopg2://iot:iot@localhost:5432/iot_board
Optional environment variables:
DATABASE_ECHO: set to1to enable SQL logging.
Execute Alembic from the repository root so the backend package is discoverable
by Python:
The project uses Alembic for schema management. The configuration is located in
backend/alembic.ini and uses the database URL from the environment.
alembic -c backend/alembic.ini upgrade head
To revert:
alembic -c backend/alembic.ini downgrade base
python -m backend.seed
This script inserts sample fields, crops, devices, sensor readings, and operations to quickly visualize data in the admin interface or dashboard.
from backend.database import session_scope
from backend import crud
with session_scope() as session:
fields = crud.list_fields(session)
first_field = crud.get_field(session, fields[0].id)
These helpers return ORM objects that can be serialized or further processed by API layers.
- The ORM models live in
backend/models.pyand can be extended with additional fields or relationships as required. - Keep Alembic migrations in sync when the models change by running
alembic revision --autogenerate -m "<message>". - Seed data is intentionally lightweight; adjust the sample payload in
backend/seed.pyto match your demo needs.