#include <SimpleLMIC.h>
SimpleLMIC ttn;Initialize the library.
ttn.begin();Returns 1 on success, 0 on failure.
This function need to be on the loop to make the library work properly.
ttn.loop();Sleep the library
ttn.sleep();Stop the library
ttn.end();The pin mapping should be set like the arduino-lmic
const lmic_pinmap lmic_pins = {
.nss = 10,
.rxtx = LMIC_UNUSED_PIN,
.rst = LMIC_UNUSED_PIN,
.dio = {2, 2, 2}
};Set LoRaWAN link.
ttn.setLink(true);or
ttn.setLink(false);Set LoRaWAN Adaptive Data Rate.
ttn.setAdr(true);or
ttn.setAdr(false);Set the clock error percentage. Check the lmic_setclockerror for more information.
ttn.setError(error);error- Set the clock error, defaults to5
Show the events on the Serial monitor.
ttn.debug(true);or
ttn.debug(false);Set the Channel of the US region.
ttn.setSubBand(band);band- Set the subband plan, defaults to2- (1to8)
For OTAA you will use the join().
ttn.setDevEui(devEui);
ttn.setAppEui(appEui);
ttn.setAppKey(appKey);
ttn.join();ttn.setDevEui(devEui);
ttn.join(appEui, appKey);ttn.join(devEui, appEui, appKey);devEui- LoRaWAN Device EUI.appEui- LoRaWAN Application EUI.appKey- LoRaWAN App Key.
For ABP you will use the personalize().
ttn.personalize(devAddr, nwkSKey, appSKey);devAddr- LoRaWAN Device Address.nwkSKey- LoRaWAN Network Session Key.appSKey- LoRaWAN App Session Key.
Register a callback function for when a payload is received.
ttn.onMessage(onMessage);
void onMessage(uint8_t *payload, size_t size, uint8_t port) {
// ...
}onMessage- function to call when a valid payload is received.payload- array with the payload data.size- length of the payload data.port- port of the payload.
Write data to the payload buffer.
ttn.write(byte);
ttn.write(buffer, length);byte- single byte to write to packet
or
buffer- data to write to packetlength- size of data to write
Returns the number of bytes written.
Note: Other Arduino Print API's can also be used to write data into the packet
ttn.send(port, confirm);port- port number of the payload, defaults to1confirm- confirm payload, defaults tofalse
Check if the LoRaWAN is busy, like waiting the rx1 and rx2, ask request, etc.
void loop() {
ttn.loop();
if (!ttn.isBusy())
{
ttn.print("hello");
ttn.send();
}
}Check if the LoRaWAN has link.
void loop() {
ttn.loop();
if (ttn.isLink())
{
}
}