How pesoen/4th-semester-project works
1. Overall shape
- This is a Django-based IoT monitoring system split into two main Django apps: backend for device/data logic and frontend for user-facing pages.
- Outside Django, it also includes an MQTT listener and a device simulator, so the system behaves like a small event-driven pipeline rather than a pure request/response web app.
2. Data ingestion pipeline
- Smart devices publish JSON payloads to the MQTT topic home/power.
- The payload includes a device identifier, power reading, and an api_key.
- A separate MQTT listener process receives the message, validates the API key against Django settings, removes the secret, and forwards the data into the Django side for persistence.
3. External runtime dependency
- The architecture depends on a local Mosquitto broker running on port 1883.
- This broker is the transport layer between devices/simulator and the server-side processing pipeline.
4. Backend domain layer
- The backend app owns the core IoT domain: devices, measurements, states, notifications, and consumption sessions.
- The migration history shows the data model evolved to support energy usage, pricing, manufacturing year, expected lifespan, and session tracking.
- This is the persistence boundary for operational data.
5. Business logic and calculations
- Files like backend/measurement.py, backend/FetchPrice.py, backend/signals.py, and utils/rul_calculator.py suggest the backend computes more than raw storage.
- Likely responsibilities include converting power readings into energy/cost metrics, reacting to model events, and estimating remaining useful life or similar device-health information.
6. Backend interfaces
- backend/views.py, backend/urls.py, and backend/urls2.py expose server-side endpoints for the application.
- backend/management/commands/add_measurement.py indicates there is also a command-line path for inserting measurement data, useful for testing or maintenance.
- backend/admin.py exposes the domain models in Django admin.
7. Frontend web UI
- The frontend app contains the browser-facing interface: templates, CSS, and view logic.
- Templates cover CRUD screens for devices, measurements, states, and notifications, plus authentication pages for login, signup, and logout.
- This app is the presentation layer over the backend data model.
8. User and device workflows
- The presence of CRUD templates and tests for “claim device” indicates users can associate devices with their account and manage device-related settings.
- State and notification pages imply users can configure when they want to be alerted and inspect device status history.
9. System orchestration
- run_full_program.py is the top-level launcher that starts three cooperating processes: Django, the MQTT listener, and the simulator.
- This makes local development a coordinated multi-process setup rather than a single-server run.
10. Device simulation
- device_simulator/improved_simulator.py acts as a synthetic IoT source that publishes fake power readings.
- It exists to exercise the ingestion pipeline without physical hardware.
11. Shared utilities
- utils/ holds cross-cutting helpers such as the RUL calculator and general utility functions.
- These are not tied to one app’s presentation or transport layer, which helps keep business logic reusable.
12. Testing structure
- Tests are organized by feature area: claim-device flows, data models, graphing, session behavior, and URL/view coverage.
- That structure reflects the key architectural seams: domain persistence, web endpoints, and analytics/visualization.
13. Clear boundaries for a diagram
- One boundary is the device/MQTT side: simulator or real hardware → MQTT broker → listener.
- The second boundary is the Django backend: ingest, validate, store, calculate, and expose data.
- The third boundary is the web frontend: render pages and CRUD forms for users.
Main entry points: models.py Domain models · backend/views.py Backend views · measurement.py Measurement logic · frontend/views.py Frontend views
Components 19
Runtime orchestration
Processes started together for local development and end-to-end device simulation.
- Program launcher · Starts Django, MQTT listener, and simulator together.
run_full_program.py - Django app · Main Django project configuration and request routing.
IoT_washing_machine/settings.py
Device ingest
MQTT transport and device-side message production.
- MQTT listener · Receives MQTT payloads, validates them, and forwards measurements into Django.
backend/mqtt_listener.py - Mosquitto broker · Local message transport on port 1883 between devices and listener.
- Device simulator · Publishes synthetic power readings to the MQTT topic.
device_simulator/improved_simulator.py
Django backend
Core domain, persistence, calculations, and server-side endpoints.
- Domain models · Devices, measurements, states, notifications, and sessions.
backend/models.py - Backend views · Server-side endpoints for backend-facing application flows.
backend/views.py - Backend routes · Primary backend URL mappings.
backend/urls.py - Extra routes · Secondary backend URL mappings.
backend/urls2.py - Model signals · React to model changes and trigger derived behavior.
backend/signals.py - Measurement logic · Turns raw power readings into domain measurements and derived values.
backend/measurement.py - Price fetcher · Obtains electricity price data for cost calculations.
backend/FetchPrice.py - Admin · Registers core domain models for Django admin use.
backend/admin.py - Add measurement · CLI entry point for inserting measurement data.
backend/management/commands/add_measurement.py
Web frontend
Browser-facing CRUD and auth pages.
- Frontend views · Renders the user-facing pages and CRUD workflows.
frontend/views.py - Templates · HTML screens for devices, measurements, states, notifications, and auth.
frontend/templates/master.html - Static styles · Shared CSS for the browser UI.
frontend/static/style.css
Shared utilities
Cross-cutting helpers used by backend logic and calculations.
- Utility helpers · Reusable helper functions and RUL calculations.
utils/utils.py - RUL calculator · Estimates remaining useful life or similar device-health metrics.
utils/rul_calculator.py
Connections 22
- Program launcher → Django app: starts · Launches the Django application runtime.
- Program launcher → MQTT listener: starts · Launches the MQTT ingestion worker.
- Program launcher → Device simulator: starts · Launches the synthetic device source.
- Device simulator → Mosquitto broker: publishes · Sends power payloads to the MQTT topic.
- Mosquitto broker → MQTT listener: delivers · Transports device messages to the listener.
- MQTT listener → Backend views: forwards · Hands validated payloads into the Django side for persistence.
- Backend views → Domain models: stores · Creates and updates operational records.
- Model signals → Domain models: reacts to · Triggers logic when domain records change.
- Measurement logic → Domain models: writes · Persists measurements and derived values.
- Measurement logic → Price fetcher: uses · Combines readings with electricity pricing.
- Measurement logic → Utility helpers: uses · Shares calculation helpers.
- Measurement logic → RUL calculator: uses · Feeds device history into health estimation.
- Backend views → Backend routes: exposed by · Primary request routing for backend endpoints.
- Backend views → Extra routes: exposed by · Secondary backend routes.
- Admin → Domain models: registers · Makes domain models available in admin.
- Add measurement → Domain models: inserts · CLI path for adding measurement records.
- Frontend views → Templates: renders · Serves HTML pages and forms.
- Frontend views → Domain models: reads · Loads domain data for the UI.
- Frontend views → Backend views: uses · Relies on backend-side application flows.
- Templates → Static styles: styles with · Uses shared CSS for presentation.
- Django app → Backend routes: includes · Project routing includes backend URLs.
- Django app → Frontend views: includes · Project routing includes frontend app pages.