1. ST P-nucleo-LrWAN3 kit

P-nucleo-lrwan3 is a complete suite for evaluating LoRaWAN networks, including a gateway and a node:

On the left is LoRa gateway, consisting of ST Nucleo-F746ZG baseboard and LRWAN_GS module of SX1301 of Ruixing Hengfang.

The LoRAa node on the right is composed of ST nucleo-L073 baseboard and ST LRWAN_NS1 expansion board. The LRWAN_NS1 expansion board integrates RHF0M003 LoRaWAN module of Ruixing Hengfang. And integrated temperature and humidity sensor HTS221, air pressure sensor LPS22HB, 3-axis magnetic sensor LIS3MDL, 6-axis attitude sensor LSM6DS3, a total of four I2C sensors.

I have uploaded all the data and firmware of the gateway to Baidu web disk, which can be obtained by following the public account “Mculover666” and replying to “Lora” :

2. Create a cloud LoRa device

Reference course: LoRa Describes how to connect a T/H sensor.

Create a project

The loginInternet of Things development platform consoleOn the New project page, enter the basic information of the project, and click Save.

Create a product

Enter the product list page of the project and click “New Product”. On the Page of “New Product”, fill in the basic product information and click “Save” :

Creating a Data Template

Click the product name to go to the product configuration page. Under User-defined Function, click Create Function to customize product functions.

Set LoRaWAN parameters

On the device development page, adjust the LoRaWAN parameter Settings as required. In this example, the default OTAA configuration is used:

Configure device data parsing

On the device development page, adjust device data parsing as needed. Because LoRa resources are limited, the device is not suitable for directly transmitting JSON data. Therefore, you can use Device Data Parsing to convert original device data to PRODUCT JSON data.

Device data protocol

In this example, the device uplink data is 4 bytes:

  • Byte 1: temperature.
  • Byte 2: Relative humidity.
  • Bytes 3 and 4: indicates the reporting period (in seconds).
  • The downstream data of the device is 2 bytes: report period (unit: second).

Data parsing script

The main function of the script for upstream data parsing is RawToProtocol, which takes fPort and bytes:

  • FPort: Indicates the fPort field of LoRaWAN data reported by the device.
  • Bytes: indicates the FRMPayload field of LoRaWAN data reported by the device.
  • The output parameter of the script main function is an object in the product data template protocol format.

In the upstream data parsing section, the javascript sample code is as follows:

function RawToProtocol(fPort, bytes) {
    var data = {
        "method": "report"."clientToken" : new Date(),
        "params": {}}; data.params.temperature = bytes[0];
    data.params.humidity = bytes[1];
    data.params.period = bytes[2] | (bytes[3] < <8);
    return data;
}
Copy the code

The main protocol function of downstream data parsing is ProtocolToRaw, where the input parameter is an object of the product data template protocol format and the output parameter is an array of at least 3 bytes:

  • Byte 1: The FPort field of LoRaWAN protocol data sent to the device.
  • Byte 2: bytes indicates the MType of LoRaWAN Data sent to the device. 0 indicates Unconfirmed Data Down, and 1 indicates Confirmed Data Down.
  • Byte 3: starts the FRMPayload field of LoRaWAN protocol data sent to the device.

In the downstream data parsing section, the javascript sample code is as follows:

function ProtocolToRaw(obj) {
    var data = new Array(a); data[0] = 5;// fport=5
    data[1] = 0;// unconfirmed mode
    data[2] = obj.params.period & 0x00FF;
    data[3] = (obj.params.period >> 8) & 0x00FF;
    return data;
}
Copy the code

The configuration is as follows:

Script simulation test data parsing

The uplink simulation data is as follows: [17,69,30,0]. Fill in the edit box of the device’s uplink data and click “run” to view the result on the right side of the simulation debugging interface:

Then test downlink data. The simulated test data is as follows, and fill it into the edit box of downlink data of the equipment:

{
  "params": {
      "period": 15}}Copy the code

The test results are as follows:

When the test is complete, click Submit!

Creating a Test device

On the device debugging page, click “New Device”. The device name dev001 and DevEUI can be obtained from the sticker on the back of LoRa node development board.

3. Add the LoRa gateway to the cloud

Log in to the console of Internet of Things development platform, and select the corresponding project in “LoRa node for Console Operation” above.

Choose Service Center > LoRa Gateway Management from the tool list on the left. The LoRa gateway management page is displayed, and click Add Gateway.

On the New Gateway page, enter the basic gateway information.

  • Gateway name: GW_Mculover666 in this example.
  • GwEUI: indicates the unique gateway ID.

4. Configure the LoRa gateway to connect to the server and the frequency band

Power on and configure the gateway according to the method described in SECTION 01 – LoRa Gateway Configuration View and Modify Method (ST P-nucleo-Lrwan3 suite is used as an example). Configure the connection between the gateway and Tencent cloud as follows:

Configuring the Server Address

LoRa server address of Tencent Cloud Iot development platform (access domain name: loragw.things.qcloud.com, access port: 1700), command:

AT+PKTFWD=loragw.things.qcloud.com, 1700170Copy the code

Configure the LoRa frequency band

Adjust the frequency point information to 486.3mhZ-487.7mhz, and modify the instruction as follows (need to send one by one) :

AT + CH = 0486.3, A AT + CH = 1486, A AT + CH = 2486.7, A AT + CH = 3486.9, A AT + CH = 4487 2 PCT, B AT + CH = 5487.3, AT + CH = 6487 B, B The AT + CH = 7487.7, B AT + CH = 8, OFF the AT + CH = 9, OFFCopy the code

After all configurations, the information is shown as follows:

Enable log printing of the gateway

AT+LOG=ON
Copy the code

Restart the module to connect to the server

After the configuration is complete, run the following command to restart the module. After the restart, the module automatically obtains an IP address and connects to the server:

AT+RESET
Copy the code

5. The LoRa node is connected to the network cloud

Compile and download the LoRaWAN routine

Download the source code from TencentOS Tiny’s official open source repository, go to the < tencentos-Tiny \board\NUCLEO_STM32L073RZ\KEIL\lorawan> directory and open the Tencentos_tiny. uvprojx project. Example projects include STM32L073 peripheral driver, TencentOS Tiny kernel, AT framework, RHF76 LoRaWAN module driver, LoRaWAN example cases.

  • (1) to modify the\examples\LoRaWAN\lora_demo.c:
tos_lora_module_join_otaa("< fill DevEUI >"."< fill AppKey >");
Copy the code
  • (2) modified\devices\rhf76_lora\RHF76.h:
#define RHF76_ATCMD_SET_CHANNEL                 "at+ch=num,0-7\r\n"
Copy the code

Since the 80-87 channel is planned for this example, adjust to:

#define RHF76_ATCMD_SET_CHANNEL                 "at+ch=num,80-87\r\n"
Copy the code

After compiling and downloading the project, the node automatically restarts after downloading the firmware. You can view the run logs from the serial port.

If the following log is displayed on the serial port, the LoRa node is successfully connected to the network through the gateway:

--->+JOIN: Network joined
--->+JOIN: NetID 000035 DevAddr 6A:40:DA:3F
Copy the code

View device data on the cloud

Keep LoRa node and LoRa gateway in the running state, go to [Console] > [Product Development] > [Device Debugging], and view dev001. Click [Debugging] to enter the device details page:

After debugging, click Device Properties to query the latest and historical data reported by the device to the development platform.

  • Latest value of device property: The latest data reported by the device is displayed.
  • Update time of device properties: Update time of display data.

The cloud sends commands to the device

You can see it in serial Assistant:

In this example, the LoRa node is A LoRaWAN Class A device. The device does not deliver data immediately. The server downlinks data to the device only after data is upstream. Therefore, you can view the delivered cycle adjustment commands only after the LoRa node reports data.