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:
- Each protocol runs on its own thread and communicate with other protocols using the NetAPI.
- Packets are dispatched using the Netreg API. Protocols and applications can subscribe to packets of interest.
- Protocols or network interfaces can be configured via the NetAPI using netopts.
- 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.
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.
- Iterate all GNRC interfaces. For that, use the the
gnrc_netif_iter
function. - Use the
gnrc_netapi_get
to get the device type. Use the PID of the candidate interface (netif->pid
). Use the netoptNETOPT_DEVICE_TYPE
to check forNETDEV_TYPE_LORA
.
-
Set they DevEUI, AppEUI and AppKey as strings in the variables
deveui_str
,appeui_str
andappkey_str
, respectively. -
Set the LoRaWAN keys using
gnrc_netapi_set
. Use the following netopts:
NETOPT_LORAWAN_ADDRESS_LONG
NETOPT_LORAWAN_APPEUI
NETOPT_LORAWAN_APPKEY
- 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.
-
Trigger the device activation with
gnrc_netapi_set
using theNETOPT_LINK
netopt (with valueNETOPT_ENABLE
). -
Check the activation status with
gnrc_netapi_get
using theNETOPT_LINK
netopt. The valueNETOPT_ENABLE
indicates a successfull activation.
-
Begin the implementation of the
send
function by allocating a packet snip that stores the counter value. Use thegnrc_pktbuf_add
function. Since the packet snip does not have footer, setnext
to NULL. Setdata
to&counter
and size tosizeof(counter)
.type
should be set toGNRC_NETTYPE_UNDEF
. -
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)
). -
Use the
gnrc_pkt_prepend
function to prepend the GNRC Netif hdr snip to the packet. -
Transmit the packet using the
gnrc_netapi_send
function. -
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
- Configure board and driver in the application Makefile:
E.g
BOARD ?= nucleo-l152re
USEMODULE += sx1276
- Build and flash the application. Open a serial communication:
$ make all flash term
- [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: []
};
}
-
Install the Mosquitto MQTT (command line) or Paho MQTT (Python library).
-
Create an API Key in TTN and grant all rights.
-
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.