The SmartBin Management System is an IoT-based solution for efficient waste management. It consists of a Flask web application and an MQTT client that work together to collect, process, and store data from smart waste bins.
- Real-time data collection from smart bins via MQTT
- Support for multiple MQTT brokers (Mosquitto and The Things Network)
- Data validation and processing
- Storage of bin data in a PostgreSQL database
- RESTful API for data access and management (provided by Flask)
- Flask Web Application: Handles API requests and database operations
- MQTT Client: Receives and processes messages from smart bins
- Database: Stores smart bin data using PostgreSQL
- Python 3.7+
- PostgreSQL database
- MQTT broker (e.g., Mosquitto, TTN)
The application uses the following environment variables:
DB_USER
: PostgreSQL usernameDB_PASSWORD
: PostgreSQL passwordDB_HOST
: PostgreSQL host addressDB_NAME
: PostgreSQL database name
MQTT_BROKER
: Mosquitto broker addressMQTT_PORT
: Mosquitto broker port (default: 1883)MQTT_USER
: Mosquitto usernameMQTT_PASSWORD
: Mosquitto passwordMQTT_TOPIC
: Mosquitto topic to subscribe to
TTN_MQTT_BROKER
: TTN broker addressTTN_MQTT_PORT
: TTN broker port (default: 1883)TTN_MQTT_USER
: TTN usernameTTN_MQTT_PASSWORD
: TTN passwordTTN_MQTT_TOPIC
: TTN topic to subscribe to
-
Clone the repository:
git clone https://github.com/yourusername/smartbin-management-system.git cd smartbin-management-system
-
Install required packages:
pip install -r requirements.txt
-
Set up environment variables (see above)
-
Initialize the database:
flask db upgrade
-
Run the application:
python app.py
- The Flask application will start and listen for incoming connections (default port: 6969)
- The MQTT client will connect to the configured broker(s) and start listening for messages
- Smart bin data will be automatically processed and stored in the database
GET /
: Check if the application is running- (Add other API endpoints as implemented in your Flask app)
The project includes comprehensive unit tests for both the Flask application and the MQTT subscriber. These tests ensure the correct functionality of data processing, payload validation, and database operations.
To run the tests, use the following commands:
-
For MQTT subscriber tests:
python -m unittest tests.test_mqtt_subscriber
-
For Flask application tests:
python -m unittest tests.test_flask_app
The unit tests cover the following areas:
- Payload decoding
- Payload validation
- Message handling (creating new bins and updating existing bins)
- Error handling for invalid payloads
- Index route functionality
To run all tests together, you can use:
python -m unittest discover tests