Skip to Content

LoRaWAN on Arduino PLCs

Prior configuration

Before attempting to send messages through LoRaWAN, make sure to have registered your device to the The Things Network Stack, as seen in the LoRaWAN setup chapter.

In addition, install the lmic library for LoRaWAN purposes.


Code snippet

The code to use is the one found in Arduino IDE -> File -> Examples -> MCCI LoRaWAN LMIC library -> ttn-otaa. Below is the start of the code.

#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>

//
// For normal use, we require that you edit the sketch to replace FILLMEIN
// with values assigned by the TTN console. However, for regression tests,
// we want to be able to compile these scripts. The regression tests define
// COMPILE_REGRESSION_TEST, and in that case we define FILLMEIN to a non-
// working but innocuous value.
//
#ifdef COMPILE_REGRESSION_TEST
# define FILLMEIN 0
#else
# warning "You must replace the values marked FILLMEIN with real values from the TTN control panel!"
# define FILLMEIN (#dont edit this, edit the lines that use FILLMEIN)
#endif

// This EUI must be in little-endian format, so least-significant-byte
// first. When copying an EUI from ttnctl output, this means to reverse
// the bytes. For TTN issued EUIs the last bytes should be 0xD5, 0xB3,
// 0x70.
static const u1_t PROGMEM APPEUI[8]={ FILLMEIN };
void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8);}

// This should also be in little endian format, see above.
static const u1_t PROGMEM DEVEUI[8]={ FILLMEIN };
void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8);}

// This key should be in big endian format (or, since it is not really a
// number but a block of memory, endianness does not really apply). In
// practice, a key taken from ttnctl can be copied as-is.
static const u1_t PROGMEM APPKEY[16] = { FILLMEIN };
void os_getDevKey (u1_t* buf) {  memcpy_P(buf, APPKEY, 16);}

static uint8_t mydata[] = "Hello, world!";
static osjob_t sendjob;

// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 60;

// Pin mapping
const lmic_pinmap lmic_pins = {
    .nss = A5,
    .rxtx = LMIC_UNUSED_PIN,
    .rst = 3,
    .dio = {LMIC_UNUSED_PIN, LMIC_UNUSED_PIN, LMIC_UNUSED_PIN},
};

In the previous chapter you created the end device, and generated 3 different keys (JoinEUI, DevEUI and AppKey). In this piece of code you need to modify variables APPEUI (JoinEUI), DEVEUI and APPKEY with its correspondent values. Be careful with the format of the variables (check the following table to set the correct values).

TTN JoinEUI

0000000000000000

IDE APPEUI

{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}

TTN DevEUI

0011223344556677

IDE DEVEUI

{0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00}

TTN AppKey

00112233445566778899AABBCCDDEEFF

IDE APPKEY

{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}

In addition to the keys, depending on the PLC you are using (Ardbox or M-Duino) the lmic_pins will vary.

  • NSS: 12 (M-Duino) or A5 (Ardbox)
  • RST: 13 (M-Duino) or 3 (Ardbox)
Rating
0 0

There are no comments for now.

to be the first to leave a comment.