Difference between revisions of "LoRaWAN Bridge"

From AllWize Wiki
Jump to navigation Jump to search
(Code)
Line 56: Line 56:
  
 
== Code ==
 
== Code ==
 +
 +
The code examples here are part of the LoRaWAN examples you can find with the AllWize library: [https://github.com/AllWize/allwize/tree/dev/examples/lorawan/lorawan_gateway/src lorawan_gateway] and [https://github.com/AllWize/allwize/blob/dev/examples/lorawan/lorawan_node/lorawan_node.ino lorawan_node].
 +
 +
=== Sender ===
 +
 +
Using the AllWize_LoRaWAN class is very simple.
 +
 +
=== Receiver ===

Revision as of 08:36, 20 August 2019

A LoRaWAN Network Server (LNS) is the piece of the LoRaWAN stack responsible for de-duplicating messages and route them to the Application Server where the user can configure the appropriate handlers to send the data to depending on the device address of the source node. Most of the servers available, including the The Things Network LNS and the LoRaServer open-source LNS support the Semtech Legacy Protocol. This protocol is a simple way to send data from a LoRaWAN Gateway to the LNS using UDP.

Theory

LoRaWAN Frame Format

The Semtech Legacy Protocol is open-spec and fairy simple so we can actually use it to send data to a LNS from wherever we want, the only requirement is that the data must be a LoRaWAN-compatible frame so the LNS can understand it. This is what a LoRaWAN frame looks like:

LoRaWAN Frame

The first row is the raw LoRa frame we don't have to worry about, the PHY Payload is the MAC part of the frame. In order:

  • MAC Header (type of message, version of the protocol,... for instance: 0x40 means unconfirmed uplink)
  • Device Address (the source device address)
  • Frame Control (LoRaWAN specific control bits: ACK, ADR, device class,...)
  • Frame Counter (a 16-bit autoincrement number)
  • Frame Options (LoRaWAN MAC options)
  • Frame Port (used to tell the frame payload format)
  • Frame Payload (the data, often LLP since the Application Server already have a decoder)
  • MIC (Message Integrity Code, used by the LNS to check the message is valid)


Wize Frame Format

Sending a LoRaWAN message over Wize

Of course the simpler option would be to encapsulate a LoRaWAN message inside the Wize Payload, but the total payload would have a lot of duplicated data:

LoRaWAN Wize Size
MAC Header C-Field 1
Device Address UID 4
Frame Control Wize Control 1
Frame Counter Wize Frame Counter 2
Frame Port Wize Application 1

As you can see we can actually map up to 9 bytes of the LoRAWAN frame to Wize specific frame bytes, and then just add the Frame Payload (encrypted using the Application Session Key) and the MIC (encrypted using the Network Session Key).

And this is what the AllWize_LoRaWAN class does:

  • Encrypts the payload using the Application Session Key
  • Generates the Message Integrity Code (MIC) using the Network Session Key
  • Configures the radio module to use a specific C-field (0x40)
  • Maps the LoRaWAN fields to Wize values
  • Sends only the required bytes

The same class can the be used in the receiver to rebuild the LoRaWAN Frame and forward the contents to a LoRaWAN Network Server using Semtech Legacy Protocol.

Code

The code examples here are part of the LoRaWAN examples you can find with the AllWize library: lorawan_gateway and lorawan_node.

Sender

Using the AllWize_LoRaWAN class is very simple.

Receiver