LoRaWAN application example.
#include <Arduino.h>
#include <SPI.h>
#define SCHED_MAX_EVENT_DATA_SIZE APP_TIMER_SCHED_EVENT_DATA_SIZE
#define SCHED_QUEUE_SIZE 60
#define LORAWAN_APP_DATA_BUFF_SIZE 64
#define LORAWAN_APP_TX_DUTYCYCLE 10000
#define APP_TX_DUTYCYCLE_RND 1000
#define JOINREQ_NBTRIALS 3
hw_config hwConfig;
#ifdef ESP32
int PIN_LORA_RESET = 4;
int PIN_LORA_NSS = 5;
int PIN_LORA_SCLK = 18;
int PIN_LORA_MISO = 19;
int PIN_LORA_DIO_1 = 21;
int PIN_LORA_BUSY = 22;
int PIN_LORA_MOSI = 23;
int RADIO_TXEN = -1;
int RADIO_RXEN = -1;
#endif
#ifdef ESP8266
int PIN_LORA_RESET = 0;
int PIN_LORA_NSS = 2;
int PIN_LORA_DIO_1 = 15;
int PIN_LORA_BUSY = 16;
int PIN_LORA_SCLK = SCK;
int PIN_LORA_MISO = MISO;
int PIN_LORA_MOSI = MOSI;
int RADIO_TXEN = -1;
int RADIO_RXEN = -1;
#endif
#ifdef NRF52_SERIES
int PIN_LORA_RESET = 4;
int PIN_LORA_NSS = 28;
int PIN_LORA_SCLK = 12;
int PIN_LORA_MISO = 14;
int PIN_LORA_DIO_1 = 11;
int PIN_LORA_BUSY = 29;
int PIN_LORA_MOSI = 13;
int RADIO_TXEN = -1;
int RADIO_RXEN = -1;
SPIClass
SPI_LORA(NRF_SPIM2, 14, 12, 13);
#endif
static void lorawan_has_joined_handler(void);
static void lorawan_join_failed_handler(void);
static void send_lora_frame(void);
static uint32_t timers_init(void);
static uint8_t m_lora_app_data_buffer[LORAWAN_APP_DATA_BUFF_SIZE];
static lmh_app_data_t m_lora_app_data = {m_lora_app_data_buffer, 0, 0, 0, 0};
lorawan_rx_handler, lorawan_has_joined_handler,
lorawan_confirm_class_handler, lorawan_join_failed_handler};
#ifdef NRF52_SERIES
#include <bluefruit.h>
void initBLE();
extern bool bleUARTisConnected;
extern BLEUart bleuart;
#endif
#ifndef LED_BUILTIN
#ifdef ESP32
#define LED_BUILTIN 2
#endif
#ifdef NRF52_SERIES
#define LED_BUILTIN 17
#endif
#endif
uint8_t nodeDeviceEUI[8] = {0x00, 0x95, 0x64, 0x1F, 0xDA, 0x91, 0x19, 0x0B};
uint8_t nodeAppEUI[8] = {0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x02, 0x01, 0xE1};
uint8_t nodeAppKey[16] = {0x07, 0xC0, 0x82, 0x0C, 0x30, 0xB9, 0x08, 0x70, 0x0C, 0x0F, 0x70, 0x06, 0x00, 0xB0, 0xBE, 0x09};
uint32_t nodeDevAddr = 0x260116F8;
uint8_t nodeNwsKey[16] = {0x7E, 0xAC, 0xE2, 0x55, 0xB8, 0xA5, 0xE2, 0x69, 0x91, 0x51, 0x96, 0x06, 0x47, 0x56, 0x9D, 0x23};
uint8_t nodeAppsKey[16] = {0xFB, 0xAC, 0xB6, 0x47, 0xF3, 0x58, 0x45, 0xC7, 0x50, 0x7D, 0xBF, 0x16, 0x8B, 0xA8, 0xC1, 0x7C};
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
hwConfig.PIN_LORA_RESET = PIN_LORA_RESET;
hwConfig.PIN_LORA_NSS = PIN_LORA_NSS;
hwConfig.PIN_LORA_SCLK = PIN_LORA_SCLK;
hwConfig.PIN_LORA_MISO = PIN_LORA_MISO;
hwConfig.PIN_LORA_DIO_1 = PIN_LORA_DIO_1;
hwConfig.PIN_LORA_BUSY = PIN_LORA_BUSY;
hwConfig.PIN_LORA_MOSI = PIN_LORA_MOSI;
hwConfig.RADIO_TXEN = RADIO_TXEN;
hwConfig.RADIO_RXEN = RADIO_RXEN;
hwConfig.USE_DIO2_ANT_SWITCH = true;
hwConfig.USE_DIO3_TCXO = true;
hwConfig.USE_DIO3_ANT_SWITCH = false;
Serial.begin(115200);
Serial.println("=====================================");
Serial.println("SX126x LoRaWan test");
Serial.println("=====================================");
#ifdef NRF52_SERIES
pinMode(30, OUTPUT);
digitalWrite(30, HIGH);
initBLE();
#endif
uint32_t err_code = timers_init();
if (err_code != 0)
{
Serial.printf("timers_init failed - %d\n", err_code);
}
if (err_code != 0)
{
Serial.printf("lora_hardware_init failed - %d\n", err_code);
}
err_code =
lmh_init(&lora_callbacks, lora_param_init,
false);
if (err_code != 0)
{
Serial.printf("lmh_init failed - %d\n", err_code);
}
{
Serial.println("lmh_setSubBandChannels failed. Wrong sub band requested?");
}
}
void loop()
{
#ifdef ESP8266 // Handle Radio events
#endif
}
static void lorawan_join_failed_handler(void)
{
Serial.println("OVER_THE_AIR_ACTIVATION failed!");
Serial.println("Check your EUI's and Keys's!");
Serial.println("Check if a Gateway is in range!");
}
static void lorawan_has_joined_handler(void)
{
#if (OVER_THE_AIR_ACTIVATION != 0)
Serial.println("Network Joined");
#else
Serial.println("OVER_THE_AIR_ACTIVATION != 0");
#endif
}
{
Serial.printf("LoRa Packet received on port %d, size:%d, rssi:%d, snr:%d\n",
{
case 3:
{
{
case 0:
break;
case 1:
break;
case 2:
break;
default:
break;
}
}
break;
break;
default:
break;
}
}
{
Serial.printf("switch to class %c done\n", "ABC"[Class]);
}
static void send_lora_frame(void)
{
{
Serial.println("Did not join network, skip sending frame");
return;
}
uint32_t i = 0;
m_lora_app_data.
buffer[i++] =
'H';
m_lora_app_data.
buffer[i++] =
'e';
m_lora_app_data.
buffer[i++] =
'l';
m_lora_app_data.
buffer[i++] =
'l';
m_lora_app_data.
buffer[i++] =
'o';
m_lora_app_data.
buffer[i++] =
' ';
m_lora_app_data.
buffer[i++] =
'w';
m_lora_app_data.
buffer[i++] =
'o';
m_lora_app_data.
buffer[i++] =
'r';
m_lora_app_data.
buffer[i++] =
'l';
m_lora_app_data.
buffer[i++] =
'd';
m_lora_app_data.
buffer[i++] =
'!';
{
}
Serial.printf("lmh_send result %d\n", error);
}
static void tx_lora_periodic_handler(void)
{
Serial.println("Sending frame");
send_lora_frame();
}
static uint32_t timers_init(void)
{
TimerInit(&appTimer, tx_lora_periodic_handler);
return 0;
}