A lightweight, containerized IoT backend stack with MQTT message broker, SQLite persistence, and Flask web interface.
This project provides a complete, minimalist backend infrastructure for IoT applications, combining three essential components:
- An MQTT broker for message passing between IoT devices
- A SQLite database for persistent data storage
- A Flask web application for data visualization and device management
The stack is fully containerized using Docker Compose, making it easy to deploy on any system with Docker installed. The Flask application performs dual roles - subscribing to MQTT topics to collect device data while also serving a web interface to visualize and interact with the stored information.
This project is designed as a companion to the ESP32 Gateway project, providing the server-side infrastructure to receive and process data from ESP32 devices.
A simple demonstration of the complete stack in action can be found here with matching dashboard.
The architecture follows a microservices approach with three main components:
-
MQTT Broker: Eclipse Mosquitto provides a lightweight message broker implementing the MQTT protocol, enabling efficient publish/subscribe communication between IoT devices and the backend.
-
SQLite Database: A lightweight, file-based SQL database that stores device data persistently with minimal resource requirements.
-
Flask Application: A Python web application that:
- Subscribes to MQTT topics to receive device data
- Processes and stores incoming messages in the SQLite database
- Provides a web interface for data visualization and device management
- Enables publishing messages back to devices via MQTT
- Docker and Docker Compose installed
- Minimum 512MB RAM and 1GB disk space
- Network connectivity for IoT devices to reach the MQTT broker
- Modern web browser for accessing the Flask interface
- Clone the repository:
git clone https://github.com/simlal/mqttbroker-sqlite-flask_minimalist-stack.git
cd mqttbroker-sqlite-flask_minimalist-stack
- Configure the MQTT broker by editing
mosquitto.conf
:
# Review and modify if needed, especially security settings
nvim mosquitto.conf
- Build and start the compose stack
docker compose -f docker-compose.yaml build
docker compose -f docker-compose.yaml up
- Access the web interface at
http://localhost:5000
The Mosquitto broker is configured through mosquitto.conf
. Key parameters include:
- Listener configuration (port 1883)
- Authentication settings (if enabled)
- Persistence options
- Security settings (TLS, if needed)
The Flask application is configured through environment variables in the Docker Compose file:
FLASK_APP
: Entry point for the Flask applicationFLASK_RUN_HOST
: Binding address (0.0.0.0 for container access)DATABASE
: Path to SQLite database fileMQTT_BROKER_URL
: Internal Docker network address of the MQTT broker
The project includes Docker Compose watch mode for development:
docker compose watch
This enables:
- Automatic syncing of local file changes to the container
- Container rebuilds when dependencies change (via uv.lock)
The MQTT broker handles communication between IoT devices and the backend system. Lightweight and efficient.
The database stores:
- Device information (MAC addresses, names, types)
- Sensor readings with timestamps
The SQLite file is stored in a Docker volume for persistence between container restarts.
The Flask application (app.py
) serves dual purposes:
-
MQTT Client: Subscribes to device topics and processes incoming messages
- Subscribes to two specific topic patterns:
MQTT_GATEWAY_TOPIC/#
for gateway device messages (with RSSI data)MQTT_TEMPERATURE_TOPIC/#
for temperature sensor data
- Automatically timestamps incoming messages
- Validates message content before database insertion
- Subscribes to two specific topic patterns:
-
Web Interface and API: Provides visualization and programmatic access
- Main routes (
/
,/devices
,/gateway-readings
,/sensor-temperature-readings
) - RESTful API endpoints with proper validation and error handling
- Support for date range filtering of sensor readings
- Main routes (
-
Database Management: SQLite connection pooling with application context
get_db()
: Provides singleton database connectionsquery_db()
: Executes SQL with transaction supportclose_connection()
: Ensures proper connection cleanup
-
Message Processing:
process_gateway_data()
: Validates and stores gateway RSSI readingsprocess_sensor_temp_data()
: Validates and stores temperature readings
-
MQTT Integration:
handle_mqtt_message()
: Processes incoming MQTT messageshandle_connect()
: Manages broker connections and topic subscriptions
-
Testing Endpoints:
/api/publish-gateway-test
: Simulates gateway messages for testing/api/publish-temperature-test
: Simulates temperature sensor messages
IoT devices need to:
- Connect to the MQTT broker at
host_ip:1883
- Publish data to topics following the pattern:
device/{mac_address}/data
- Subscribe to topics for receiving commands:
device/{mac_address}/command
The Flask application provides these RESTful API endpoints:
GET /api/devices
: Lists all devices, with optional filtering by internal_idGET /api/gateway-readings?macAddress=<mac>&readingsFrom=<date>&readingsTo=<date>
: Get gateway readings with optional date filteringPOST /api/gateway-readings
: Submit new gateway reading (requires JSON with macAddress, timestamp, rssi)GET /api/sensor-temperature-readings?macAddress=<mac>&readingsFrom=<date>&readingsTo=<date>
: Get temperature readings with optional date filteringPOST /api/sensor-temperature-readings
: Submit new temperature reading (requires JSON with macAddress, timestamp, temperature)POST /api/publish-gateway-test
: Test endpoint that publishes sample gateway data to MQTTPOST /api/publish-temperature-test
: Test endpoint that publishes sample temperature data to MQTT
Minimalist web interface provides:
- Device dashboard that shows all registered devices
- Gateway readings view with signal strength history
- Temperature readings view with temperature history
- Date range filtering for all historical data
- Device selection dropdowns filtered by device type
Potential enhancements for this project:
- Add user authentication for the web interface
- Implement MQTT authentication for device security
- Add data export/import functionality
- Create alert systems for anomalous readings
- Add time-series visualization for long-term trends
- Implement device firmware update functionality via MQTT
This minimalist IoT stack provides a solid foundation for collecting, storing, and visualizing data from connected devices. Its containerized architecture ensures easy deployment and maintenance, while the combination of MQTT, SQLite, and Flask offers a balanced approach to IoT backend development without the complexity and resource requirements of larger solutions.
The project demonstrates how working IoT systems can be built with lightweight, open-source components that can run on modest hardware while still providing reliable data handling and visualization capabilities.