LoRa (Long Range) technology ensures reliable long-range communication between a node and a gateway, providing low power usage for devices over the long term. This article provides an explanation of LoRa node-to-gateway communication while presenting a node equipped with two buttons to control the flags. These buttons allow users to enable or disable specific actions associated with each button and update the status on the gateway.
From home automation to industrial monitoring, this enhanced LoRa communication configuration presents diverse possibilities for Internet of Things (IoT) deployments.
This tutorial will cover hardware configuration, protocol selection, and gateway configuration, facilitating real-time data transmission and visualization.
Abstract
Here’s what we’ll cover…
1. How to upload LoRa code to LoRa E5 Mini-Board
2. Circuit diagram for connecting an ST-Link and buttons to the LoRa E5 Mini-Board
3. How to configure an end node on the gateway
4. The program flow or algorithm with an explanation
Requirements
1. Make sure the LoRa gateway is already installed as shown in this article
2. STM32Cube IDE must be installed on your PC
3. The Seed Studio LoRa E5 Mini Card
4. The original ST-Link V2 programmer
5. A tactile switch and some basic welding knowledge
Compiling the code
1. Download the code and extract it
2. Open the folder and double click on “.project”
3. The project will be listed in the 'Project Explorer' tab on the left of the STM32CUBE IDE. Select the project and click the hammer icon in the top bar to compile the code.
4. After compiling the code, there should not be any error as shown in the image below. If there is an error message, follow the steps again to ensure success.
Loading the code
1. Locating a genuine ST-Link V2 is necessary for proper updating of the LoRa code, as clone variants of ST-Link are often not compatible. This means that it is essential to recognize the original ST-Link. A reliable method of identification involves examining the hardware components.
After thorough analysis, a genuine ST-Link V2 has an STM chip, indicating authenticity. Clones are typically imported from China and have a slightly different appearance.
This project requires a smooth “flashing process”, which occurs reliably with a genuine ST-Link with STM chip (see image below). Knowing the difference will ensure compatibility and proper programming.
2. Connect five wires to the LoRa E5 Mini Board as shown in the image below, including 3V3, SWDIO, SWCLK, RST and GND.
3. Go back to the STM32 Cube IDE and click 'Play; button as shown below to upload the code.
4. You should receive the message “Download verified successfully”.
Connecting the buttons
Connect the two tact switches as shown in this circuit diagram:
Adding a device to the gateway
1. If there is no device profile, add one. If you already have one, this same profile can be used for multiple nodes.
2. To add a profile, open ChirpStack and go to 'Device Profiles'.
3. Click 'Add Device Profile'.
4. Fill in the general information and click 'Submit'.
5. Turn OTAA off because the device will work in ABP mode. Fill in the RX3 data rate details and frequency.
6. Activate Class C mode as shown below.
7. Then add 'Applications' which contains all devices.
8. Click 'Add Applications'.
9. Fill in the registration details as indicated below in the image.
10. Click on the application that was just created and then click on the 'Add Device' button. This process must be done whenever a new device is added.
11. Start filling in the details as shown below. You'll need to find the device's EUI, which we'll cover in the next step.
Finding EUI and KEYs
1. To find the EUI device and keys, connect the device to a PC via USB cable.
2. Open any serial terminal (we are using Arduino) and select the device port. Open the Serial Terminal with a baud rate of 115200. Press the 'Reset' button to see the Keys.
3. Copy the EUI device from the serial terminal and paste it into ChirpStack Add the device section as shown in the image below.
4. Go to the 'Activation' section and fill in the device address, AppSkey and Network S key, which you can get from the Serial Terminal.
Receiving data
1. To receive data, go to the events section where there is data received every 10 seconds.
2. Data is in hexadecimal format. Sends the battery level, as well as the status of Button1 and Button2, in hexadecimal.
3. When Button1 is pressed, the Button1 flag will be HIGH in the following data (just click once with the mouse without holding it down).
4. When Button2 is pressed, the Button2 flag will be HIGH in the following data.
5. When both buttons are pressed, the flag will be zero.
Code algorithm
Code Explanation
1. LoRa-based STM32 code is slightly different from typical codes. The main.c file starts the process to run lora_app.c as the main file. So, open the lora_app.c which contains the code.
2. There's a lot going on in the code, but let's review what's relevant to this tutorial. The condition below starts a timer, which calls the OnTxTimer Event function that sends data to the gateway. The default time is 10 seconds, which can be changed by initializing TxPeriodicity in milliseconds. The image below removes the APP_TX_DUTYCYCLE and puts the required time delay in milliseconds.
This function handles all interrupts that occur on the interrupt pins. There is a 100 millisecond delay for debugging purposes.
Integrating smart meters
Smart meters with pulse sensors can be seamlessly integrated into the above code. Pulse sensors generate pulses whenever the meter reading increases, acting as if the button stops in this tutorial. By configuring a microcontroller (MCU) to recognize these pulses as interruptions, a user can effectively track meter readings in real time.
Whenever a pulse is received by the interrupt pin, the MCU will increment the meter reading and store it as a variable. The code can then be programmed to send these readings to the gateway at regular intervals, such as every 10 seconds. The transmission frequency can be adjusted to meet specific application requirements.
This integration improves the functionality of smart meters, enabling accurate and automated tracking of energy consumption. With real-time readings, utilities can efficiently monitor usage, identify patterns and optimize energy distribution. The versatile code can be adjusted for different pulse sensor configurations, so it can work with many smart meter designs.