In this project, the RAK1906 sensor was used to monitor the ambient environment: temperature, pressure and humidity. The sensor data will be transmitted via LoRaWAN to cloud platforms such as b, and displayed in a web page created with the Codeigniter framework, using JavaScript and MySQL databases for data storage and analysis.
Story (Step-by-Step Guide)
Step 1: Mounting the WisBlock Components
• Mount and fix the microcontroller on the motherboard:
• Mount and fix the sensor on the motherboard:
• Connect the LoRa antenna:
1. Using the USB-C cable to connect the mode to the computer.
2. In the Arduino IDE, support for WisBlock is added:
Enable the options called Build and Load, and in the field called Additional Panel Manager URLs, insertthe following link:
3. Install the libraries for WisBlock tiles:
4. Choose the WisBlock board RAK4631:
1. Log in to TTN - https://eu1.cloud.thethings.network
2. Create a new app:
After filling in the required fields, click Create App to complete the setup.
3. To add a new device, follow these steps:
4. Choose the manual method of registration:
5. Set up your device to connect with TTN by following these steps:
- In the given example, it consists of three files:
o lora_handler.cpp: This file manages LoRaWAN communication, including network parameters such as authentication keys and network settings.
o main.h: This file contains global variables and declarations of functions used in the program. The core libraries for the application.
o RAK4631-DeepSleep-LoRaWan.ino: the main file where sensor data is collected and transmitted over the network. It also handles power management, including deep sleep modes between transmissions.
- after verification and uploading the code to the device, a check can be made in the TTN console where a "Hello" message in HEX format should be received:
m_lora_app_data_buffer[buffSize++] = 'H';m_lora_app_data_buffer[buffSize++] = 'e';m_lora_app_data_buffer[buffSize++] = 'l';m_lora_app_data_buffer[buffSize++] = 'l';m_lora_app_data_buffer[buffSize++] = 'o';
Step 4: Code
To connect the RAK1906_Environment_BME690 sensor, follow these steps:
1. install the SX126x-Arduino library required for the RAK1906 sensor:
2. check the environmental sensor in the Arduino IDE starting from an example for the sensor. For this you can access:
File > Examples > RAK WisBlock Examples > RAK 4631 > Sensors > RAK1906_Environment_BME690
3. After compiling and loading the sample, the data is displayed in the serial monitor:
After the sensor has been checked, the two examples, for the transmission of data in TTN and the sensor, will be merged. For this, we start from the LoRaWAN example in the Arduino IDE, which is modified as follows:
- opens the RAK4631-DeepSleep-LoRaWan.ino file where the libraries for the environmental sensor are included:
#include <Adafruit_Sensor.h>
#include <Adafruit_BME680.h>
and the following functions:
void bme680_init()
void bme680_get()
- In the setup() function, the sensor is initialized by calling the bme680_init() function
- Change the loop() function to read data from the sensor:
if (! bme.performReading())
{
Serial.println("Failed to perform reading :(");
}
bme680_get();
- update the sendLoRaFrame function as follows:
uint8_t buffSize = 0;
uint16_t temp = (temperature*100)+5000;
uint16_t press = pressure/100;
uint16_t hum = (humidity*100)+5000;
uint16_t gs = gas_resistence/1000;
Reset buffer size
buffSize = 0;
Adding temperature
m_lora_app_data_buffer[buffSize++] = 0x01;
m_lora_app_data_buffer[buffSize++] = (uint8_t)(temp / 256);
m_lora_app_data_buffer[buffSize++] = (uint8_t)(temp % 256);
Adding pressure
m_lora_app_data_buffer[buffSize++] = 0x02;
m_lora_app_data_buffer[buffSize++] = (uint8_t)(press / 256);
m_lora_app_data_buffer[buffSize++] = (uint8_t)(press % 256);
Adding humidity data
m_lora_app_data_buffer[buffSize++] = 0x03;
m_lora_app_data_buffer[buffSize++] = (uint8_t)(hum / 256);
m_lora_app_data_buffer[buffSize++] = (uint8_t)(um % 256);
Adding gas data
m_lora_app_data_buffer[buffSize++] = 0x04;
m_lora_app_data_buffer[buffSize++] = (uint8_t)(gs / 256);
m_lora_app_data_buffer[buffSize++] = (uint8_t)(gs % 256);
m_lora_app_data.buffsize = buffSize;
In the TTN application, you go to Payload Formatter to the Uplink option where you choose Custom JavaScript Formatter. A function will be written to decode the data from the HEX format as follows:
function decodeUplink(input) {
var data = {};
if(input.fPort === 2){
if (input.bytes[0] === 0x01){
Temperature
date. Temp = parseFloat(((((input.bytes[1] * 256.0) + input.bytes[2])-5000) / 100.0).toFixed(2));
}
if (input.bytes[3] === 0x02){
humidity
date. Hum = parseFloat(((((input.bytes[4] * 256.0) + input.bytes[5])-5000) / 100.0).toFixed(2));
}
if (input.bytes[6] === 0x03){
humidity
date. Press = parseFloat(((((input.bytes[7] * 65536.0) + (input.bytes[8]*256)+input.bytes[9]))/100).toFixed(2));
}
if (input.bytes[10] === 0x04){
date. Gas = parseFloat(((((input.bytes[11] * 65536.0) + (input.bytes[12]*256)+input.bytes[13])) / 100.0).toFixed(2));
}
}
return {
date: date,
warnings: [],
Errors: []
};
}
After testing and uploading the code to the TTN app, the values from the RAK1906 sensor should be received successfully.
The data collected in the cloud can be transferred to external applications, such as Ubidots, using webhooks. In this instance, we have selected the option to transmit to a custom service. To accomplish this, the following steps should be followed:
1. In the created TTN application choose Webhook, from the available templates choose Custom webhook.
2. In General settings, an ID and the url that will be called when new data is received from the sensor and the format in which the data is sent are entered.
3. In the Enabled event types section, choose Uplink message:
The data sent using the TTN application will be saved in a MySQL database and will be displayed in a web page that can be found at https://ds-iot.ro/weather
Comments