Welcome to the IOT Smart Warehouse project! This project aims to develop a smart warehouse system using Python, Flask, MQTT, React-JS and MongoDB. The system will enable real-time monitoring and control of various warehouse operations.
To set up the project on your local machine, follow the instructions below.
- Python installed on your system
- MongoDB installed and running
- Up and running Redis client
- Docker installed and running on your system
-
Clone the repository:
-
Navigate to the project directory:
for the backend
cd BackEnd
If you want to install redis via docker
$ docker run -d --name="SmartWareHouse-redis" -p 6379:6379 redis
-
Create a virtual environment (optional but recommended):
python -m venv env
Activate the virtual environment:
-
For macOS and Linux:
source env/bin/activate
-
For Windows:
env\Scripts\activate
-
Install the dependencies:
pip install -r requirements.txt
-
There are 4 configurations
development
,test
,staging
andproduction
inconfig.py
. Default isdevelopment
-
Create a
.env
file from.env.example
and set appropriate environment variables before running the project -
Replace
<your-mongodb-uri>
with the connection URI for your MongoDB database. Set<mqtt-broker-url>
,<mqtt-username>
, and<mqtt-password>
with the appropriate MQTT broker details.
- Docker installed and running on your system
-
To configure the MQTT broker, update the configuration file located at
broker/config/mosquitto.conf
with the desired settings. -
The folder
broker/keys
contains the certificates and a script for generating the certificates. There is aconfig.ini
file with settingCOMMON_NAME
for mqtt host name keys setting, the default islocalhost
To generate the certificates execute the following command in thebroker/keys
directorybash make_keys.sh
-
Start the MQTT broker container with the updated configuration:
docker-compose -f broker/docker-compose.yml up -d
Or navigate to broker/
docker compose up -d
-
To change the broker username and password
docker-compose exec mosquitto mosquitto_passwd -c /broker/config/mosquitto.passwd <username>
replace the variable
<username>
with a new username. Then enter the password twice. -
Restart the MQTT broker container for the changes to take effect:
docker-compose -f broker/docker-compose.yml restart
-
Build and start the Docker container with the provided docker-compose.yml file:
docker-compose -f broker/docker-compose.yml up -d
This command starts the MQTT broker container and maps port 1883 (unencrypted) and 8883 (encrypted).
The default username is
mosquitto
, the default password ismosquitto
- To stop the MQTT broker container, use the following command:
docker-compose -f broker/docker-compose.yml down
-
From the keys the generated on broker config,
ca.cert
,client.crt
andclient.key
, copy the files to theapp/mqtt/certs
directory. -
Change the configs for the broker connection in the .env file
- There are three main topics: TO_HOST, TO_DEVICE, and CLIENT_CONNECTIONS.
The TO_HOST
topic is used for messages being sent from devices to the server.
Topic format: TO_HOST/<device_group>/<device_subgroup>/<device_id>
Key | Description |
---|---|
data | The payload to be processed by the server. |
Example message:
{
"data": {
"name": "John",
"status": "active",
"ip": "192.168.0.1"
}
}
The TO_DEVICE
topic is used for messages being sent from the server to the devices.
Topic format: TO_DEVICE/<device_group>/<device_subgroup>/<device_id>
Key | Description |
---|---|
data | The payload for the device to process. |
Example message:
{
"data": {
"command": "start",
"duration": 10
}
}
The CLIENT_CONNECTIONS
topic is used for logging device connections to the MQTT broker.
Topic: CLIENT_CONNECTIONS
Topic format: CLIENT_CONNECTIONS/<device_id>
Key | Description |
---|---|
data | Information about the connected device. |
Example message:
{
"data": {
"name": "Device1",
"status": "connected",
"ip": "192.168.0.2"
}
}
Devices can also log actions happening on them in this topic.
Topic format: CLIENT_CONNECTIONS/activity/<device_id>
Example message:
{
"data": {
"name": "main_door",
"action": "opened",
"meta_data": {
"user_id": "2g36y489derty87",
"timestamp": "2023-05-14 12:12"
}
}
}
These are the main topics and message structures used in the MQTT communication. Devices publish to the TO_HOST
topic and subscribe to the TO_DEVICE
topic based on their device group, subgroup, and ID. The CLIENT_CONNECTIONS
topic is used to log device connections to the MQTT broker.
To start the server, run the following command:
python server.py
To start in production, run:
gunicorn -c gunicorn_config.py server:app
The server will start running on the default port. You can modify the port number in the server.py
file if needed.
- Logs would be generated under
log
folder
- Run redis locally before running celery worker
- Celery worker can be started with following command
# run following command in a separate terminal
$ celery -A celery_worker.celery worker --loglevel='INFO'
# (append `--pool=solo` for windows)
Includes preconfigured packages to kick start flask app by just setting appropriate configuration.
Package | Usage |
---|---|
celery | Running background tasks |
redis | A Python Redis client for caching |
flask-cors | Configuring CORS |
python-dotenv | Reads the key-value pair from .env file and adds them to environment variable. |
marshmallow | A package for creating Schema, serialization, deserialization |
webargs | A Python library for parsing and validating HTTP request objects |
autopep8
& flake8
as dev
packages for linting and formatting
Test if this app has been installed correctly and it is working via following curl commands (or use in Postman)
- Check if the app is running via
status
API
$ curl --location --request GET 'http://localhost:5000/status'
If you have any questions or need further assistance, feel free to contact us at smart warehouses.