Hello,
I’m trying to pair the RAK4631 with Dragino’s DLOS8, so far I’ve been able to get data to appear to be received by the gateway, but for some reason the gateway rejects it, citing an invalid fcnt every time. I’m using ABP mode with the gateway’s ABP decryption function (local network server).
Data appearing on gateway:
Mon Oct 4 21:49:43 2021 daemon.info lora_pkt_fwd[2661]: RXTX~ {"rxpk":[{"tmst":1027488172,"time":"2021-10-04T21:49:46.434830Z","tmms":4434,"chan":3,"rfch":0,"freq":902.900000,"stat":1,"modu":"LORA","datr":"SF8BW125","codr":"4/5","lsnr":12.5,"rssi":-78,"size":33,"data":"gDU0MzKAAAACaoCirlQyU4FKWw0TeeZ2vnpEM4obVZiq"}]}
Sketch that sends data:
#include <Arduino.h>
#include <LoRaWan-RAK4630.h> //http://librarymanager/All#SX126x
#include <SPI.h>
// RAK4630 supply two LED
#ifndef LED_BUILTIN
#define LED_BUILTIN 35
#endif
#ifndef LED_BUILTIN2
#define LED_BUILTIN2 36
#endif
bool doOTAA = false; // OTAA is used by default.
#define SCHED_MAX_EVENT_DATA_SIZE APP_TIMER_SCHED_EVENT_DATA_SIZE /**< Maximum size of scheduler events. */
#define SCHED_QUEUE_SIZE 60 /**< Maximum number of events in the scheduler queue. */
#define LORAWAN_DATERATE DR_3 /*LoRaMac datarates definition, from DR_0 to DR_5*/
#define LORAWAN_TX_POWER TX_POWER_5 /*LoRaMac tx power definition, from TX_POWER_0 to TX_POWER_15*/
#define JOINREQ_NBTRIALS 0 /**< Number of trials for the join request. */
DeviceClass_t g_CurrentClass = CLASS_A; /* class definition*/
LoRaMacRegion_t g_CurrentRegion = LORAMAC_REGION_US915; /* Region:EU868*/
lmh_confirm g_CurrentConfirm = LMH_CONFIRMED_MSG; /* confirm/unconfirm packet definition*/
uint8_t gAppPort = LORAWAN_APP_PORT; /* data port*/
/**@brief Structure containing LoRaWan parameters, needed for lmh_init()
*/
static lmh_param_t g_lora_param_init = {LORAWAN_ADR_ON, LORAWAN_DATERATE, LORAWAN_PUBLIC_NETWORK, JOINREQ_NBTRIALS, LORAWAN_TX_POWER, LORAWAN_DUTYCYCLE_OFF};
// Foward declaration
static void lorawan_has_joined_handler(void);
static void lorawan_join_failed_handler(void);
static void lorawan_rx_handler(lmh_app_data_t *app_data);
static void lorawan_confirm_class_handler(DeviceClass_t Class);
static void send_lora_frame(void);
/**@brief Structure containing LoRaWan callback functions, needed for lmh_init()
*/
static lmh_callback_t g_lora_callbacks = {BoardGetBatteryLevel, BoardGetUniqueId, BoardGetRandomSeed,
lorawan_rx_handler, lorawan_has_joined_handler, lorawan_confirm_class_handler, lorawan_join_failed_handler
};
//OTAA keys !!!! KEYS ARE MSB !!!!
uint8_t nodeDeviceEUI[8] = {0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x33, 0x33};
uint8_t nodeAppEUI[8] = {0xB8, 0x27, 0xEB, 0xFF, 0xFE, 0x39, 0x00, 0x00};
uint8_t nodeAppKey[16] = {0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88};
// ABP keys
uint32_t nodeDevAddr = 0x32333435;
uint8_t nodeNwsKey[16] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x12, 0x13, 0x14, 0x14, 0x15, 0x16};
uint8_t nodeAppsKey[16] = {0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32};
// Private defination
#define LORAWAN_APP_DATA_BUFF_SIZE 21 /**< buffer size of the data to be transmitted. */
#define LORAWAN_APP_INTERVAL 30000 /**< Defines for user timer, the application data transmission interval. 20s, value in [ms]. */
static uint8_t m_lora_app_data_buffer[LORAWAN_APP_DATA_BUFF_SIZE]; //< Lora user application data buffer.
static lmh_app_data_t m_lora_app_data = {m_lora_app_data_buffer, 0, 0, 0, 0}; //< Lora user application data structure.
TimerEvent_t appTimer;
static uint32_t timers_init(void);
static uint32_t count = 0;
static uint32_t count_fail = 0;
unsigned int thingNum = 1130;
byte appNum = 69;
byte mac[] = {0x26, 0x01, 0x16, 0xF9, 0xFF, 0xFF};
unsigned long time = 1632942051000;
float flo = 2.31;
int in = -1500;
byte bs = 0b01010101;
byte Lpacket[20];
union Union
{
float flt;
unsigned char flBytes[4];
};
void setup() {
// put your setup code here, to run once:
preparePacket();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
// Initialize LoRa chip.
lora_rak4630_init();
// Initialize Serial for debug output
time_t timeout = millis();
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 5000)
{
delay(100);
}
else
{
break;
}
}
Serial.println("=====================================");
Serial.println("Welcome to RAK4630 LoRaWan!!!");
if (doOTAA)
{
Serial.println("Type: OTAA");
}
else
{
Serial.println("Type: ABP");
}
switch (g_CurrentRegion)
{
case LORAMAC_REGION_AS923:
Serial.println("Region: AS923");
break;
case LORAMAC_REGION_AU915:
Serial.println("Region: AU915");
break;
case LORAMAC_REGION_CN470:
Serial.println("Region: CN470");
break;
case LORAMAC_REGION_EU433:
Serial.println("Region: EU433");
break;
case LORAMAC_REGION_IN865:
Serial.println("Region: IN865");
break;
case LORAMAC_REGION_EU868:
Serial.println("Region: EU868");
break;
case LORAMAC_REGION_KR920:
Serial.println("Region: KR920");
break;
case LORAMAC_REGION_US915:
Serial.println("Region: US915");
break;
}
Serial.println("=====================================");
//creat a user timer to send data to server period
uint32_t err_code;
err_code = timers_init();
if (err_code != 0)
{
Serial.printf("timers_init failed - %d\n", err_code);
return;
}
// Setup the EUIs and Keys
if (doOTAA)
{
lmh_setDevEui(nodeDeviceEUI);
lmh_setAppEui(nodeAppEUI);
lmh_setAppKey(nodeAppKey);
}
else
{
lmh_setNwkSKey(nodeNwsKey);
lmh_setAppSKey(nodeAppsKey);
lmh_setDevAddr(nodeDevAddr);
}
// Initialize LoRaWan
err_code = lmh_init(&g_lora_callbacks, g_lora_param_init, doOTAA, g_CurrentClass, g_CurrentRegion);
lmh_setSubBandChannels(1);
if (err_code != 0)
{
Serial.printf("lmh_init failed - %d\n", err_code);
return;
}
// Start Join procedure
lmh_join();
}
void loop() {
// put your main code here, to run repeatedly:
}
void preparePacket() {
Union a;
a.flt = flo;
Union b;
b.flt = flo;
int i = 0;
Lpacket[i++] = highByte(thingNum);
Lpacket[i++] = lowByte(thingNum);
Lpacket[i++] = appNum;
Lpacket[i++] = mac[0];
Lpacket[i++] = mac[1];
Lpacket[i++] = mac[2];
Lpacket[i++] = mac[3];
Lpacket[i++] = mac[4];
Lpacket[i++] = mac[5];
Lpacket[i++] = a.flBytes[0];
Lpacket[i++] = a.flBytes[1];
Lpacket[i++] = a.flBytes[2];
Lpacket[i++] = a.flBytes[3];
Lpacket[i++] = b.flBytes[0];
Lpacket[i++] = b.flBytes[1];
Lpacket[i++] = b.flBytes[2];
Lpacket[i++] = b.flBytes[3];
Lpacket[i++] = highByte(in);
Lpacket[i++] = lowByte(in);
Lpacket[i++] = bs;
}
void lorawan_has_joined_handler(void)
{
Serial.println("OTAA Mode, Network Joined!");
lmh_error_status ret = lmh_class_request(g_CurrentClass);
if (ret == LMH_SUCCESS)
{
delay(1000);
TimerSetValue(&appTimer, LORAWAN_APP_INTERVAL);
TimerStart(&appTimer);
}
}
/**@brief LoRa function for handling OTAA join failed
*/
static void lorawan_join_failed_handler(void)
{
Serial.println("OTAA join failed!");
Serial.println("Check your EUI's and Keys's!");
Serial.println("Check if a Gateway is in range!");
}
/**@brief Function for handling LoRaWan received data from Gateway
@param[in] app_data Pointer to rx data
*/
void lorawan_rx_handler(lmh_app_data_t *app_data)
{
Serial.printf("LoRa Packet received on port %d, size:%d, rssi:%d, snr:%d, data:%s\n",
app_data->port, app_data->buffsize, app_data->rssi, app_data->snr, app_data->buffer);
}
void lorawan_confirm_class_handler(DeviceClass_t Class)
{
Serial.printf("switch to class %c done\n", "ABC"[Class]);
// Informs the server that switch has occurred ASAP
m_lora_app_data.buffsize = 0;
m_lora_app_data.port = gAppPort;
lmh_send(&m_lora_app_data, g_CurrentConfirm);
}
void send_lora_frame(void)
{
if (lmh_join_status_get() != LMH_SET)
{
//Not joined, try again later
return;
}
Union a;
a.flt = flo;
Union b;
b.flt = flo;
uint32_t i = 0;
memset(m_lora_app_data.buffer, 0, LORAWAN_APP_DATA_BUFF_SIZE);
m_lora_app_data.port = gAppPort;
m_lora_app_data.buffer[i++] = highByte(thingNum);
m_lora_app_data.buffer[i++] = lowByte(thingNum);
m_lora_app_data.buffer[i++] = appNum;
m_lora_app_data.buffer[i++] = mac[0];
m_lora_app_data.buffer[i++] = mac[1];
m_lora_app_data.buffer[i++] = mac[2];
m_lora_app_data.buffer[i++] = mac[3];
m_lora_app_data.buffer[i++] = mac[4];
m_lora_app_data.buffer[i++] = mac[5];
m_lora_app_data.buffer[i++] = a.flBytes[0];
m_lora_app_data.buffer[i++] = a.flBytes[1];
m_lora_app_data.buffer[i++] = a.flBytes[2];
m_lora_app_data.buffer[i++] = a.flBytes[3];
m_lora_app_data.buffer[i++] = b.flBytes[0];
m_lora_app_data.buffer[i++] = b.flBytes[1];
m_lora_app_data.buffer[i++] = b.flBytes[2];
m_lora_app_data.buffer[i++] = b.flBytes[3];
m_lora_app_data.buffer[i++] = highByte(in);
m_lora_app_data.buffer[i++] = lowByte(in);
m_lora_app_data.buffer[i++] = bs;
m_lora_app_data.buffsize = i;
lmh_error_status error = lmh_send(&m_lora_app_data, g_CurrentConfirm);
if (error == LMH_SUCCESS)
{
count++;
Serial.printf("lmh_send ok count %d\n", count);
}
else
{
count_fail++;
Serial.printf("lmh_send fail count %d\n", count_fail);
}
}
/**@brief Function for handling user timerout event.
*/
void tx_lora_periodic_handler(void)
{
TimerSetValue(&appTimer, LORAWAN_APP_INTERVAL);
TimerStart(&appTimer);
Serial.println("Sending frame now...");
send_lora_frame();
}
/**@brief Function for the Timer initialization.
@details Initializes the timer module. This creates and starts application timers.
*/
uint32_t timers_init(void)
{
TimerInit(&appTimer, tx_lora_periodic_handler);
return 0;
}
I’ve tried changing the devaddr, nwskey, and appskey on both the node and gateway side to try to get the two sides to sync frame counters but to no avail. Any ideas?