Connecting an Arduino using LoRaWAN

Contents

Connecting an Arduino using LoRaWAN#

LoRaWan (Long Range Wide Area Network) is a low power, long range radio networking protocol. We will demonstrate how to connect an Arduino with connected sensors to an online IoT (Internet of Things) platform.

Requirements

  1. An Arduino (we have used an UNO)

  2. An Arduino Shield with LoRaWAN capabilities (we used a Dragino LA66 based shield)

  3. A sensor or signal of some type to connect to the Arduino (we used a photoionization sensor)

Code#

Example code is given in the labmoninator repository We shall summarise some key points

The programme sets up to serial connections. One is to any PC connected to the Arduino. The other is to the LA66 chip onboard the shield

void setup() {
  // Initialize serial communication with a baud rate of 9600 to USB
  Serial.begin(9600);
  // Initialize communication with the LA66 module using SoftwareSerial
  LA66.begin(9600);
  LA66.listen();
  // Reset the LA66 MCU
  LA66.println("ATZ");
  // Reserve memory for inputString
}

In the main loop the first step is to check that the LA66 is connected to the IoT provider. It also checks that the uplink interval has been passed. The provider we are connecting to has fair use limits so this interval should never be shorter than 30 s.

if ((millis() - previousMillis >= uplink_interval) && (network_joined_status == 1)) {

    //Is connected and uplink time passed
    //Therefore collect data from sensor

    //Format the payload to send through OOTA in hex-format
    char sensor_data_buff[128] = "\0"; //empty char array that will be transmitted
    //Command format:  AT+SENDB=<confirm_status>,<Fport>,<data_len>,<data>
    //if adding extra values to transmit, increase format string (in this example %02X) by two of the same formats
    snprintf(sensor_data_buff, 128, "AT+SENDB=%d,%d,%d,%02X%02X%02X%02X", 0, 2, 4,
        (short)(mean) >> 8 & 0xFF, (short)(mean) & 0xFF,
        (short)(sdd) >> 8 & 0xFF, (short)(sdd) & 0xFF);
    LA66.println(sensor_data_buff);
    //Transmitted, do any clean up of sensor required
}

The formatting the data for transmission is the most important part. Any data must first be turned into an integer and then converted to hexadecimal.

This line

snprintf(sensor_data_buff, 128, "AT+SENDB=%d,%d,%d,%02X%02X%02X%02X", 0, 2, 4,
        (short)(mean) >> 8 & 0xFF, (short)(mean) & 0xFF,
        (short)(sdd) >> 8 & 0xFF, (short)(sdd) & 0xFF);

does the job. It is made of several parts.

  • sensor_data_buff is the char array the values will be written into.

  • 128 is the size of the char array (this is also much larger than the LoRAWAN protocol allows)

  • “AT+SENDB= this is the begining of the command that **LA66 will perform AT commands are modem commands.

  • %d is a formatting code, it says that the bytes at this position represent integers. There are 3 as there are 3 numbers needed for transmission

    • confirm_status which is set to 0

    • Fport is the application port, set to 2

    • data_len is how long is how much data there is (in this example it is 4)

  • %02X is a formatting code that indicates that the values are Upper case Hexadecimal integers printed to at least 2 digits prepended with 0’s if needed. Two of these are required for each data value being sent

  • The begining of the encoding which does bitwise operations on the data

    • & 0xFF will leave only the lest significant bit

Next you need to either set up your own LoRaWAN gateway and server or register for a service