Skip to content
This repository was archived by the owner on Feb 14, 2024. It is now read-only.

Files

Latest commit

 

History

History

10-lorawan-sensor

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

LoRaWAN Sensor application

The goal of this exercise is to transmit a counter value over LoRaWAN using the GNRC Network Stack.

To ensure flexibility, the GNRC Network Stack follows some design principles:

  1. Each protocol runs on its own thread and communicate with other protocols using the NetAPI.
  2. Packets are dispatched using the Netreg API. Protocols and applications can subscribe to packets of interest.
  3. Protocols or network interfaces can be configured via the NetAPI using netopts.
  4. Packets are stored in a centralized Packet Buffer and are represented using linked lists (packet snips). This simplify the process of adding headers to existing packets. The Packet Buffer API provides functions to gather/scatter packet snips.

GNRC Network Interfaces are essentially threads for protocols below the network layer. A RIOT application can have more than one network interface, for eg: IEEE 802.15.4, Ethernet, BLE or LoRaWAN.

The proposed application increments a counter each time the button is pressed and periodically sends the counter value to LoRaWAN Application Server.

Note regarding GNRC NetAPI

Some GNRC NetAPI functions allow to pass a context argument. Since the netopts of this exercise do not use any context, set context=0 if required by the function.

Task 1

  1. Iterate all GNRC interfaces. For that, use the the gnrc_netif_iter function.
  2. Use the gnrc_netapi_get to get the device type. Use the PID of the candidate interface (netif->pid). Use the netopt NETOPT_DEVICE_TYPE to check for NETDEV_TYPE_LORA.

Task 2

  1. Set they DevEUI, AppEUI and AppKey as strings in the variables deveui_str, appeui_str and appkey_str, respectively.

  2. Set the LoRaWAN keys using gnrc_netapi_set. Use the following netopts:

  • NETOPT_LORAWAN_ADDRESS_LONG
  • NETOPT_LORAWAN_APPEUI
  • NETOPT_LORAWAN_APPKEY
  1. Enable OTAA activation, disable confirmed transmissions and set the Datarate. Use the following netopts and values:
  • NETOPT_OTAA
  • NETOPT_ACK_REQ
  • NETOPT_LORAWAN_DR

NOTE: Use the sizeof() operator to calculate the size of each value.

  1. Trigger the device activation with gnrc_netapi_set using the NETOPT_LINK netopt (with value NETOPT_ENABLE).

  2. Check the activation status with gnrc_netapi_get using the NETOPT_LINK netopt. The value NETOPT_ENABLE indicates a successfull activation.

Task 3

  1. Begin the implementation of the send function by allocating a packet snip that stores the counter value. Use the gnrc_pktbuf_add function. Since the packet snip does not have footer, set next to NULL. Set data to &counter and size to sizeof(counter). type should be set to GNRC_NETTYPE_UNDEF.

  2. The GNRC Network interface requires that the packet contains a special GNRC Netif Header snip that contains information about the source and destination address. Use the gnrc_netif_hdr_build function to build the snip. Set the source address to none (src = NULL, src_len = 0). Set the destination address to the port (dst = &port, dst_len = sizeof(port)).

  3. Use the gnrc_pkt_prepend function to prepend the GNRC Netif hdr snip to the packet.

  4. Transmit the packet using the gnrc_netapi_send function.

  5. Use the online Time on Air calculator to estimate a suitable value for the TRANSMISSION_INTERVAL macro. Assume the following values:

  • Spreading Factor=(12 - DR)
  • Bandwidth=125KHz
  • Code rate=1
  • Payload length=14 (LoRaWAN header/footer + 1 byte for counter value)
  • Preamble length=8 (as per EU868 regional parameters).
  • Explicit header=Yes
  • CRC=Yes
  1. Configure board and driver in the application Makefile:

E.g

BOARD ?= nucleo-l152re
USEMODULE += sx1276
  1. Build and flash the application. Open a serial communication:
$ make all flash term
  1. [OPTIONAL] Configure a Payload Formatter in the TTN Dashboard. Use Custom Javascript as Formatter type and paste the following snippet:
function decodeUplink(input) {
  return {
    data: {
      counter: input.bytes
    },
    warnings: [],
    errors: []
  };
}

Task 4

  1. Install the Mosquitto MQTT (command line) or Paho MQTT (Python library).

  2. Create an API Key in TTN and grant all rights.

  3. Use the following parameters for the MQTT subscription:

  • User: <Application ID>@ttn
  • Password: <API Key>
  • Host: eu1.cloud.thethings.network
  • Topic = v3/<Application ID>@ttn/devices/+/up

NOTE: '+' is a wildcard. In this case it is used to match any device. In order to subscribe to all available topics, set the topic to "#". To learn more about the available TTN topics, check the TTN MQTT Server documentation.

If using mosquitto, run the following command:

mosquitto_sub -h eu1.cloud.thethings.network -t "#" -u "<Application ID>@ttn" -P "<API Key>"

If using Paho MQTT, use the mqtt.py script.