LoraWAN, HeartRate and Enviroment Monitor.
Introduction and problemI created a Pre-Post-hospital monitoring system based on Lora communication, for patients who suffered Covid-19 (but can be adapted to any disease afterwards). The solution will track the patient's heart rate using an ECG sensor. It has been shown that worldwide those with pre-existing conditions such as cardiac disease are the ones with more risk.
The solution will track the patient's heart rate using an ECG sensor (Arduino compatible Module: https://sharvielectronics.com/product/max30100-pulse-oximeter-heart-rate-sensor-module/) in order to be able to measure your total recovery from the disease and avoid a relapse.
Solution:With this solution the patient will be able to track their recovery and a web app to see their progress, the doctor could have a database of their patients which can be made public to developers and doctors around the world by generating a public API. With AWS and also in the future with a lot of data, we might be able to design an AI that is capable of determining the recovery status of a patient using that data.
I used the Thunderboard Sense 2 to obtain all the environmental data from the multiple sensors it has and serve as a tracking of the patient's environment in their recovery, in order to rule out variables such as the weather to measure the patient's recovery
Connection Diagram:Arduino Circuit Diagram:
- Assemble the circuit elements:
- Put everything in a case.
- Correct electrodes position:
The first step is to create an account in Arduino Create, this process will help us to flash our Arduino MKR 1300 with the correct software.
Obtain the DevEUI:After already having our account, we will open the following link to code 1 that will provide them, this will allow us to obtain the DevEUI of our board, this number is necessary to be able to consume the LoraWAN services of The Things Network (TTN).
We can see how the code shows us the DevEUI that we will need:
Before we can start sending information through Lora, we have to create an account on the TTN network.
TTN Setup:Create a TTN account:
https://www.thethingsnetwork.org/
Once the account is created, we enter the console to create our first application.
NOTE: the applications are used to associate the devices in groups and on them implement integrations such as AWSIoT or API.
https://console.thethingsnetwork.org/
Click on create an app:
The configuration will vary according to the region of the world in which you are, in this case I came to Aveiro, Portugal to develop my project since in Mexico there is no good LoraWAN infrastructure.
Now we get the AppEUI, this will serve us later.
Register a new device:
When registering the device we must put the Device EUI that we obtained in the Arduino console.
The last number we need is the AppKey that the platform will give us.
Since we have those 3 numbers, we can go to configure our Arduino MKR 1300 to send data to the TTN network.
Arduino MKR Setup:Already with the AppEUI and the AppKEY we will open the following code in Arduino Create and paste our our credentials.
https://create.arduino.cc/editor/Altaga/c0f3d3a8-daef-440e-8d9c-3abeb6229670/preview
Paste your credentials here:
Get the whole system working and you will get the following on the TTN platform.
Since the values reach the TTN platform, we will have to send the data to AWSIoT, this can be done in 2 ways, officially TTN has a guide on how to do this process, but I will show you a second way to do it.
TTN official Guide: https://www.thethingsnetwork.org/docs/applications/aws/
In our case we preferred to use the following connection diagram:
AWS works through roles, these roles are credentials that we create so that the services can communicate with each other, in order to carry out all our integration we need to create a role that allows the effective transmission of all services, therefore that will be the first thing To make.
Note: always start here when doing a project with AWS.
IAM:- Enter the IAM console.
- Enter through the role tab and click "Create role".
- Create a role focused on the IoT platform.
- Press next till review.
- Now we have to add the additional permissions to the Role, in the roles tab enter the role we just created and press the Attach policies button.
Inside policies add the following:
- AmazonIoTFullAccess
Once that is finished, now we can start configuring the AWS Lambda but before this we need the AWS IoT Core Endopoint.
AWS IoT Endpoint:- First we have to access our AWS console y look for the IoT core service:
- Obtain your AWS endpoint, save it because we will use it to setup the Lambda and the Node-RED.
- We configure the lambda in the following way and create it:
- Once the lambda has been created we go down to the Execution role section and press the View the YOUR_ROLE button on the IAM console to be able to send the data from AWS Gateway to AWS IoT Core:
- Once that is finished, we select the lambda in our rule to finish configuring the lambda.
- The code that we are going to execute is in the "Lambda Code" folder, we have to paste it in the lambda and modify the following parameters.
Paste your endpoint in the code and place the Topic that you consider convenient to send the information to the IoT Core.
API Gateway:Since we have our lambda created, it is time to create our AWS Gateway endpoint to send the information from TTN.
Within the service we press the create button.
In our case we will use an HTTP API, there are many ways to configure it but this is the easiest in our view.
The API Gateway configuration will be as follows in my case the Lambda function is LoraHR.
The way to call her will be as follows.
The rest we will leave with its Default configuration and we will go to the CORS tab to configure TTN access to the function.
For ease of this tutorial we will configure the CORS as follows.
Once this is done we go to the API section to get the endpoint.
The API endpoint along with the function will be the following in my case.
https://XXXXXXXXXX.execute-api.us-east-1.amazonaws.com/PostHR
Since we have this number now if we can create the integration in TTN.
TTN Integration:In our application we will go to the integration section.
We add a new integration as follows.
And ready if you did everything correctly you should see the data arrive at AWS IoT in the following way.
NOTE: the data that says -48NaN are values that the platform sends by Default, however these will be filtered in NodeRED, although if you prefer you can carry out this filtering from the Lambda.
IoT Things:Since we have all our platform ready, we have to create the accesses to communicate with it. So we will have to create two Things in this case, the first is for our RaspberryPi Gateway and the other will be for the NodeRed UI.
First we have to access our AWS console and look for the IoT core service:
Get your AWS endpoint, save it because we will use it to setup the RSL10 App and the webpage.
- In the lateral panel select the "Onboard" option and then "Get started".
- Select "Get started".
- At "Choose a platform" select "Linux/OSX", in AWS IoT DEvice SDK select "Python" and then click "Next".
- At Name, write any name, remember that you will have to do this process twice, so name things ion order that you can differentiate the credentials that you will put in NodeRed. Then click on "Next step".
- At "Download connection kit for" press the button "Linux/OSX" to download the credential package (which we will use later) and click on "Next Step".
- Click "Done".
- Click "Done".
- On the lateral bar, inside the Manage/Things section we can see our thing already created. Now we have to set up the policy of that thing for it to work without restrictions in AWS.
- At the lateral bar, in the Secure/Policies section we can see our thing-policy, click on it to modify it:
- Click on "Edit policy document".
Copy-paste the following text in the document and save it.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:*",
"Resource": "*"
}
]
}
- Once this is done, we will go to our pc and to the folder with the credentials previously downloaded, extract them.
- Save this files for later.
For software reasons, the thunderboard already comes with its bluetooth SDK, in addition to that Silicon Labs already provides an application to view this data on a cell phone as seen below.
Board:
Platform:
The first plan was to use the application as a Gateway to the Cloud to send the data to AWS, but that feature was recently removed.
The second plan was to make our own Android application to make the Gateway, however the SDK is not available yet.
So the final plan was to make a RaspberryPi Zero W Gateway which would read the data over BLE and send it over WiFi to AWS IoT.
Diagram:
As the first part of the data collection process, we will connect the Thunderboard Sense 2 to the PC and with a serial monitor we will obtain its Bluetooth Mac ID.
With that number we will go to configure the raspberry.
RaspberryPi Setup:Download the operating system of the Raspberry Pi.
To download the operating system of the Raspberry enter the following link:
- Link: https://www.raspberrypi.org/downloads/raspbian/
- Download the lastest version.
- Flash the operating system in the SD.
Software: https://www.balena.io/etcher/
- Through Etcher flash the raspberry operating system but DO NOT put it inside the raspberry yet.
- Create a wpa_supplicant for the connection of the raspberry to the internet.
- Since you have flashed the operating system, copy and paste the files from the "RaspberryPiFiles" folder directly into the SD card.
- Then open the "wpa_supplicant.conf" file with a text editor.
- In between the quotes in the ssid line write your wifi network and in psk the network key.
country = us
update_config = 1
ctrl_interface =/var/run/wpa_supplicant
network =
{
scan_ssid = 1
ssid = "yourwifi"
psk = "yourpassword"
}
- We save the changes and remove the SD from the PC.
We then place the SD in the raspberry and connect it to its power source.
- The power source of a Raspberry Pi is recommended to be from 5 volts to 2.5A minimum. We recommend the official ower supply for the Raspberry pi.
Once the Raspberry has already started, we need to access it through SSH or with a keyboard and a monitor.
- If you want to access it through SSH we need your IP.
- In order to analyze your network and obtain the number we will have to use one of the following programs.
- Advanced IP Scanner (Windows) or Angry IP Scanner program (Windows, Mac and Linux).
- In the following image you can see how we got the Raspberry IP.
- Connect the raspberry with ssh.
- To connect using ssh to the raspberry we need the Putty program.
- Link: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
- This program will let us access the command console of the raspberry.
In Linux, just open the terminal and put the following command.
ssh pi@RASPBERRYIP
- Password: “raspberry”
First, we will install the necessary libraries for our program to work.
For it to work we just have to input the following command.
sudo apt-get update
sudo apt-get install python3-pip libglib2.0-dev libatlas-base-dev git -y
sudo pip3 install bluepy Crypto crc16 paho-mqtt
Copy the "Rpi Software" folder into the raspberry and paste the certificates we created earlier into the "Certs" subfolder.
NOTE: you can rename the certificates as they are in the image if you don't want to edit them in the code.
Inside the main.py file set up your AWS IoT Core Endpoint correctly.
If all the previous process was done correctly, run the following code to start sending data to AWS IoT.
sudo python3 main.py YOURMAC
The data should look like this.
Since we have all the hardware and cloud backend configured, we will configure the frontend in Node-RED
Node-Red Setup:Node Red is a tool for NodeJS where we can integrate services easily, without code and, of course, create excellent dashboards.
- Node Red is a tool for NodeJS where we can integrate services easily, without code and, of course, create excellent dashboards.
- NodeJS installation guide: https://www.guru99.com/download-install-node-js.html
- NodeRED installation guide: https://nodered.org/docs/getting-started/windows
- NodeRED installation guide: https://flows.nodered.org/node/node-red-dashboard
- The file "flows.json" in the folder "Node-RED Flow", has all the information to import the flow into your NodeRED.
- Once that is done we will edit the MQTT node to enter our credentials.
- Set Server and Port.
- Press in the pencil in TSL configuration to add the certificates.
- Note: RootCA certificate inside "Certs" folder.
- Select the correct topic in each MQTT nodes.
In my case:
- /Device1/EnvironmentData
- /Device1/HR
- If everything works fine press the "Deploy" button and enter the following URL to check the Dashboard.
Desktop:
Mobile:
- This node receives the broker's payloads, filters according to the sensor which graph it has to go to and sends it to graph and deploy.
Comments