The Bastwan board is a low-power tool that is based on the management of LoRa, in addition to being compatible with the Arduino IDE, its presentation in a feather format makes it a great option for project development.
Bastwan has been chosen for the development of this project because it has a RAK4260 processor, with 32 bits of power, it also has a micro USB port, which provides convenience when programming it, with 3.3V logic operation, 256K FLASH, and 32K of RAM. In addition to having a perfect LoRa and LoRawan module for this practice.
For more specifications, you can consult their Github page: https://github.com/ElectronicCats/BastWAN
How does Wing Modbus RS485 work?This Wing has a feather format compatible with the Bastwan board and is specially designed for use on the Bast family plates with feather format.
RS485 is a communication standard widely used in the industry that we can use in processors like Bastwan to read or write to other devices.
Wing RS485 is a shield for the Bastwan board that helps to communicate with devices managed through the Modbus protocol, this works through its communication pins (Re, De, Di, Ro). It is generally used together with the Modbus communication protocol
Where:
RO: It is the receiver (RX)
DI: It is the transmitter (TX)
RE: Digital pin (Corresponds to Bastwan pin 10)
DE: Digital pin (Corresponds to Bastwan pin 11)
What is Modbus?Modbus is a communications protocol, based on the client/server architecture, designed to work with programmable logic controllers (PLCs).
Modbus allows the control of a network of devices and communicates the results to a computer.
This protocol can work with RS485 or RS232.
C4E Modbus SensorThe C4E sensor is a temperature and conductivity sensor that uses the Modbus protocol to deliver readings, it is capable of operating with a voltage that ranges from 5v-12v and has 4 electrodes (2 graphs and 2 platinum) to sense levels of conductivity capable of sensing from -10 ° - 60 ° and with 5 pressure bars.
For this practice, we will only use pins 1 (V), 3 (GND), 4 (B), and 5 (A).
Project explanationIn this practice we will explain the connection to use, the Arduino code for its operation, and how it should be displayed in The Things Network and Ubidots with a BastWan board, Feather Wing RS485 ModBus, and the C4E temperature sensor.
ConnectionsTo carry out the connections of this practice we need to mount our Wing on the Bastwan board, then identify the pins for the configuration, and connect our sensor; We will only need four connections:
V: is the sensor supply voltage (in this case it is recommended to connect the VBUS of the Bastwan card)
GND: It is the ground of our circuit (Bastwan also has pins)
B: It will be connected to the output of the same name (B) of our Wing RS485 Modbus
A: It will be connected to the output of the same name (A) of our Wing RS485 Modbus
Knowing that De and Re must go to digital pins that enable them, there is nothing left but to mount our board and keep in mind which pins are the activators to declare in the program (in this case they are D10 and D11).
Code for sending the C4E sensor readingIn this practice, we will use the aqualab library
#include <SPI.h>
#include <LoRa.h>
#include <AqualaboSensors.h>
How to install the library?
- We will open the Arduino IDE
- Click on the toolbar and select "tools"
- In the tools menu, look for the section "Library manager"
- When opening the Library Manager, a window with a search engine will appear.
- In this search engine, we will enter the name of the library "AqualabSensor"
- Now it only remains to install the library.
At this point, we name what type of device we are going to manipulate
aqualaboModbusSensorsClass aqualaboModbusSensors
We start serial port1 at 9600 baud to read the pins on the board and wait for it to open
void setup () {
Serial.begin (9600);
Serial1.begin (9600);
while (!Serial);
Serial.println("Modbus Sensor C4E");
We declare the slave or device connected to our Wing, in this case, the pins where we can find the sensor as well as the default ID of the sensor.
aqualaboModbusSensors.begin(30, 11, 10, Serial1 );
Sensor and LoRa parameter settings
Serial.println("Init sensor");
aqualaboModbusSensors.initSensor();
Serial.println("Ready... ");
Serial.println("LoRa Sender");
pinMode(RFM_TCX_ON,OUTPUT);
pinMode(RFM_SWITCH,OUTPUT);
pinMode(LED_BUILTIN,OUTPUT);
LoRa.setPins(SS,RFM_RST,RFM_DIO0);
if (!LoRa.begin(915E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
Reading and sending the data obtained by the sensor through the Bastwan card (Lora) with the Modbus shield to another Bastwan board.
void loop() {
aqualaboModbusSensors.read();
Serial.print("Temperature: ");
Serial.println(aqualaboModbusSensors.sensorC4E.temperature, 2);
Serial.print("Conductivity: ");
Serial.println(aqualaboModbusSensors.sensorC4E.conductivity, 2);
Serial.print("Sending packet: ");
float temperature = aqualaboModbusSensors.sensorC4E.temperature;
float conductivity = aqualaboModbusSensors.sensorC4E.conductivity;
digitalWrite(LED_BUILTIN,1);
An information packet is sent through Lora
LoRa.beginPacket();
digitalWrite(RFM_SWITCH,0);
LoRa.print("temperatura: ");
LoRa.print(temperature);
LoRa.print(" conductividad: ");
LoRa.print(conductivity);
LoRa.endPacket();
delay(500);
digitalWrite(LED_BUILTIN,0);
delay(500);
}
The transmitter prints us the data that is being sent, the conductivity, and the temperature.
Receiver code of our data with LoRa
#include <SPI.h>
#include <LoRa.h>
We activate the speed to 9600 baud and declare the parameters for our LoRa card as a receiver.
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(RFM_TCX_ON,OUTPUT);
pinMode(RFM_SWITCH,OUTPUT);
digitalWrite(RFM_SWITCH,1);
Serial.println("LoRa Receiver");
LoRa.setPins(SS, RFM_RST, RFM_DIO0);
if (!LoRa.begin(915E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
We are looking for an information package.
void loop() {
int packetSize = LoRa.parsePacket();
if (packetSize) {
Serial.print("Received packet '");
We read the received package and print the data inside the package.
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
As we can see, the receiver is obtaining the data through LoRa printing the temperature, conductivity, and spring factor.
The Things Network seeks to offer an open communication network for the Internet of Things.
Within this network we can create applications, registering our devices to be able to manage them from the website this platform is capable of receiving and sending data emulating the communication with which LoRa works.
It is known that data can be read from one plate to another through the LoRa technology, but remember that our module also has LaRaWan capable of transmitting the data to the platform of the things networks with the help of the library Cayenne LPP.
Once registered, we will have access to our console where we can find the applications and gateways.
At this point, we will have to add an application to register our sensor.
When starting the application, it will send us to a section where we will add an identifier (in lowercase letters and without space) to our device and a description.
When we have identified our application, it will show us the following menu, where we will find the general description, without devices.
Immediately after this, we will enter the “devices” section, where we will register our Bastwan plate.
We will make an identifier for our plate and automatically generate the "device EUI".
Once our device has been registered in our application, the following panel will be shown in which we can see our application keys, device keys, and the configuration.
There is also a toolbar, where we can choose between seeing the description and status of our device, observing the data we are receiving, and our communication settings.
In this section, we will find the general settings and local settings of the device, in general, we will find a label for our device and its activation methods, where we will have to change to ABP.
When selecting the ABP method, a panel will be displayed where we will have to generate the keys that we will later use. In this section, it will be enough to save the data.
Once the activation method has been changed, we will obtain the keys that we will need to be able to connect our device to The things network.
To carry out this program we will need the following libraries
#include <AqualaboSensors.h>
#include<CayenneLPP.h>
#include <SPI.h>
#include <lorawan.h>
CayenneLPP lpp(51);
How to install the cayenne LPP or Beelan library?
To obtain the library, we must enter our platform on Github: in which we will select the master branch.
Once the master branch is selected, we will go to the "code" icon and select the option to download the ZIP file.
Once the file is downloaded, we open it and unzip the files in our Arduino libraries folder.
With this, the library should be available
Then we will assign our credentials from the things networks, or the keys that we previously obtained.
const char *devAddr = "26013557";
const char *nwkSKey = "040A022068FB1C137888440CE072ACF0";
const char *appSKey = "3D3E011CBB4CEF72885D12B91C85D680";
We will assign a time interval between each information package sent as well as a counter that will help us know how many packages we have sent.
const unsigned long interval = 5000;
unsigned long previousMillis = 0;
unsigned int counter = 0;
char myStr[50];
char outStr[255];
byte recvStatus = 0;
declare the pins that we will use from our Lora module, and start our sensor.
const sRFM_pins RFM_pins = {
.CS = SS,
.RST = RFM_RST,
.DIO0 = RFM_DIO0,
.DIO1 = RFM_DIO1,
.DIO2 = RFM_DIO2,
.DIO5 = RFM_DIO5,
};
aqualaboModbusSensorsClass aqualaboModbusSensors;
We will start the serial 9600 baud to monitor the readings of our sensor.
void setup () {
Serial.begin (9600);
while (! Serial);
We will start our Lora module
Serial.println("Modbus Sensor C4E");
if(!lora.init()){
Serial.println("RFM95 not detected");
delay(5000);
return;
}
pinMode(RFM_TCX_ON,OUTPUT);
pinMode(RFM_SWITCH,OUTPUT);
pinMode(LED_BUILTIN,OUTPUT);
We declare the type of device that we will use, in this case, its class.
lora.setDeviceClass (CLASS_A);
lora.setDataRate (SF8BW125);
lora.setChannel (MULTI);
Here we will put our ABP keys previously defined
lora.setNwkSKey(nwkSKey);
lora.setAppSKey(appSKey);
lora.setDevAddr(devAddr);
We will initiate Modbus communication through Serial1
Serial1.begin(9600);
We define the pins in which we will find our Modbus device
aqualaboModbusSensors.begin(30, 11, 10, Serial1 );
we set the parameters of our sensor
Serial.println("Init sensor");
aqualaboModbusSensors.initSensor();
Serial.println("Ready... ");
}
We start with the reading of our sensor
void loop() {
aqualaboModbusSensors.read();
We assign the variable to degrees centigrade
Serial.print("Temperature: ");
Serial.println(aqualaboModbusSensors.sensorC4E.temperature, 2);
We assign the conductivity variable to μS / cm
Serial.print("Conductivity: ");
Serial.println(aqualaboModbusSensors.sensorC4E.conductivity, 2);
We send the data to the things network
if(millis() - previousMillis > interval) {
previousMillis = millis();
Serial.print("Sending: ");
printVariables();
lora.sendUplink((char *)lpp.getBuffer(), lpp.getSize(), 0, 1);
}
recvStatus = lora.readData(outStr);
if(recvStatus) {
Serial.println("Received: ");
Serial.println(outStr);
}
lora.update();
}
We print our variables in things network
void printVariables()
{
lpp.reset();
int sensorValue = aqualaboModbusSensors.sensorC4E.conductivity;
Serial.print(F("conductivity="));
Serial.print(sensorValue, 2);
lpp.addBarometricPressure(1, sensorValue);
int temp = aqualaboModbusSensors.sensorC4E.temperature;
Serial.print(F(", Tempetarure="));
Serial.print(temp, 2);
lpp.addTemperature(2, temp);
}
Connection with UbidotsTo connect our device to a visualization application, such as Ubidots, we need to enter the "Integrations" section in our taskbar.
In this section, we will have to add the integration of our device.
When starting with the integration, a list of applications will be displayed in which we must look for the one from Ubidots.
When selecting our application, it will ask us for a Token, an access key, and an identifier that will be used to recognize the device in Ubidots.
Our identifier must be something with which your device or application is located within Ubidots.
The access key is default.
We will find this token in our Ubidots profile in the API credentials section.
When opening this section, all we have to do is copy our Token number and paste it into The Things Network.
Once the integration is complete, we will be able to see the Ubidots logo in the description of our device.
After this, we can see in the devices section of Ubidots the name of our component.
Now we will go to the Data section where we will select Dashboards, in this section, we will be able to customize the sample and the handling of the data obtained from our sensor.
When adding a widget to see our data, it shows us a large list of options to be able to visualize the behavior of our device.
Once the widget is selected we will have to add our variables, in this case, temperature and conductivity that will be displayed according to the channel we have chosen.
Once the variable is selected we will have to add a name to the widget, in this case, they have been named according to the variable.
Once the Widget is saved, we will be able to observe the changes in our sensor in a completely personalized way.
It only remains to compare with our serial monitor and The thongs networks, to verify that the data is the same.
When working with our devices, it is important to take their specifications into account, to avoid any damage, since temperature, current, and voltage factors can cause the quality of operation to vary if they are not adequate.
Acquired knowledge- Connection and handling of Modbus devices with our Wing RS485 Modbus
- Use of LoRa and LoRawan technology through Bastwan
- Manipulation of management platforms such as The things network
- Using visualization applications such as Ubidots
- Management of programming software such as Arduino IDE
Comments