This repository is an example of application for a thermal cabinet system, composed by an ESP32 as the IoT device for measure temperature and sense the state of a door (open/closed). This sends the measurements to a server (deployed in a cloud platform or any other) which execute an API for receive and process the signals. So, data is sent by HTTP using APIRest (Exists other protocols as MQTT or WebSockets, but in this practice, we pretend to use APIRest).
The server is a hub, processing and serving data to other instances. For data storage, AWS was used, implementing Lambda functions through API-Gateway, which process and sends or retrieves data from DynamoDB.
A client UI was implemented, using Qt PySide6 for the client application. This retrieves the temperature, door state and the last twelve measures of temperature.
If the temperature raises upper than a threshold or the door was opened or closed, a notice message is sent through Telegram, to any group.
- An ESP32.
- A server deployed in any cloud or place.
- AWS DynamoDB, Lambda functions and API-Gateway knowledge.
First, you can clone this repository in your local PC, with:
git clone https://github.com/gggfred/dcca-cc-thermal-cabinet/tree/main
Next, install the different instances. Let's follow the next instructions:
For this part, you must install some packages in your server, after this copy the files from server
folder in your server machine, at your prefered platform. Then, you need to configure the server. Here you can find the instructions:
sudo apt update
sudo apt install python3-venv nginx
- Copy the folder
myproject
into the home folder at your server. - Copy
myproject.service
into/etc/systemd/system
- Copy
myproject.conf
into/etc/nginx/sites-available
To config myproject
you must create a Python virtual environment and then install the requirements.txt
into this, as follows:
- Get into the home folder.
cd ~
- Create the environmet and source it.
python3 -m venv ./venv
source venv/bin/activate
- Change into project folder and install the required packages
cd myproject
pip3 install -r requirements.txt
After, you must specify the url of the AWS api service, in the file cloud_services_api.py
by changing the variable API_GATEWAY_URL
for your url.
Also, you must specify the YOUR_BOT_TOKEN
and CHAT_ID
variables of telegram.py
with your telegram credentials, for use this service. To achieve this, please refer to the next link https://core.telegram.org/api
Now configure the SystemD daemon and Nginx to brings the public access.
For config the daemon, you must run the next command:
sudo systemctl enable myproject.service
sudo systemctl start myproject.service
Always you can stop or restart the service with:
sudo systemctl stop myproject.service
sudo systemctl restart myproject.service
For config Nginx, you must run the next:
sudo ln -s /etc/nginx/sites-available/myproject.conf /etc/nginx/sites-enabled
So, you can restart Nginx by running:
sudo systemctl restart nginx.service
This practice is focused on IoT devices, so you need to burn the firmware include in sensor
folder into your ESP32. For this, you need the next:
- Arduino IDE.
- Install the Espressif Systems's esp32 board support.
- Install the Benoit Blanchon's ArduinoJson library.
Then, follow the next steps:
- Please, change the next variables to customize the application to your needs:
...
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD"; // 0 if the network is open
...
String serverName = "SERVER_URL"; // Replace with your server URL
...
- Connect your ESP32 and upload the firmware.
The temperature signals were simulated for this practice, the door state is sense by pin 23
being 0 closed, 1 open.
At this point, you should create the database in AWS, using DynamoDB for storage the temperature data, and Lambda functions for save and retrieve the data.
First, is necessary to create a user role, with the permissions for access to AWS, as follows:
- Access to
IAM
console. - Open
Access management->Roles
tab. - Click on
Create role
- Step 1 - Select trusted entity: Select Lambda on
Service or use case
and click next. - Step 2 - Search by policy name and select:
- AWSLambda_FullAccess
- AWSLambdaBasicExecutionRole
- AWSLambdaDynamoDBExecutionRole
- AmazonDynamoDBFullAccess
- Step 3 - Give a name, for example
roleDynamoDB
. To finish click onCreate role
.
Next, are the steps for install the AWS cloud:
- Access to DynamoDB Dashboard.
- Create a DynamoDB Table.
- Enter
Measurements
as table name. - Enter
unique_id
as partition key. - Enter
timestamp
as sort key.
Now, it is necessary to create the Lambda functions, which interacts with DynamoDb by writing and reading data. The code for this functions are in the files measurement_to_dynamodb.py
and measurement_from_dynamodb.py
. To achieve this, follow the next steps:
- Access to Lambda Dashboard.
- Create a Lambda function.
- Enter
MeasurementToDynamoDB
as funcion name. - Choose Python 3.12 as Runtime.
- Change the
execution role
to use an existing role, and choose theroleDynamoDB
role. - Open the Lambda function
MeasurementsToDynamoDB
to edit it and select theCode
tab. - Select
lambda_function
tab and paste the code inmeasurement_to_dynamodb.py
.
Repeat this process to create other function, using MeasurementFromDynamoDB
as the name and paste the code in the file measurement_from_dynamodb.py
.
For last, it is necessary to create the API as such, by following the next steps:
- Access to API Gateway.
- Click on
Create API
. - Choose
REST API
to build. - Choose
New API
and enter an API name. - Click on
Create API
Once created the API, you should create the resources (endpoints). In the menu API Gateway->APIs
click on your API name, and create resources as follows:
- Click on
Create resource
. - Select the path (endpoint) on which you want to create the resource.
- Enter a name for your resource
- Click on
Create resource
In this case, the process was repeated three times, in order to create the next endpoints tree:
- /cc_api_final
- /cc_api_final/history
- /cc_api_final/measurements
Now it's necessary to create methods for this endpoints. In this case, we should have the next results:
Path | Method | Lambda Function |
---|---|---|
/cc_api_final/history | GET | MeasurementsFromDynamoDB |
/cc_api_final/measurements | POST | MeasurementsToDynamoDB |
To achieve this, we should follow the next steps:
- Select the resource in which you want to create the method.
- Click on
Create method
. - Select a method type (GET, POST).
- Select
Lambda function
as integration type. - Choose the
Lambda function
. - Click on
Create method
to finish.
Once you have created the two methods, you should deploy the API, following the next:
- Click on
Deploy API
- Select a stage or create a new one if you don't have.
- Click on
Deploy
This application is going to run in your local PC. It's recommended to create a virtual environment into the app client
folder and install the packages in requirements.txt
, to achieve this, you can follow the next steps:
- Change into the
client
folder and create a virtual enviroment.
cd ~/myproject/client
python3 -m venv ./venv
source venv/bin/activate
- Install the packages using pip3
pip3 install -r requirements.txt
- You must change the variable
url
inapi.py
by the url of the server implemented in the section 1, replaceSERVER_URL
.
url = "SERVER_URL"
Now you can run this application with:
python3 main.py