IDE arduino 2.2.1
Using example-> Rak Wisblock → RUI examples-> example → LoraWan_OTAA.
Región:
- RAK_REGION_US915
Datarate - Test with: DR0,DR1,DR2,DR3,DR4
ADR - Test with ADR enable, and ADR disable
confirmed message retry number - Retry is set to 1 as it is in the loRaWan_OTAA example
How far is the gateway from the node? - The gateway is more 20 meters away
Output
RAKwireless LoRaWan OTAA Example
LoRaWan OTAA - seting chanels 8-15!
Get channel mask Success
Channel mask = 0002
Wait for LoRaWAN join…+EVT:JOINED
Duty cycle is ON
Packet is UNCONFIRMED
Device Address is 00395EA1
Uplink period is 40000ms
Current Work Mode: LoRaWAN.
Data Packet:
0x74 0x65 0x73 0x74
Sending is requested
Sending failed with 2
+EVT:SEND_CONFIRMED_FAILED(2)
Using local Chirpstack server
On some occasions after resetting the module it sends a packet and then all the others fail.
code:
/***
-
This example shows LoRaWan protocol joining the network in OTAA mode, class C, region US868.
-
Device will send uplink every 40 seconds.
***/
#define OTAA_PERIOD (40000)
/*************************************
LoRaWAN band setting:
RAK_REGION_EU433
RAK_REGION_CN470
RAK_REGION_RU864
RAK_REGION_IN865
RAK_REGION_EU868
RAK_REGION_US915
RAK_REGION_AU915
RAK_REGION_KR920
RAK_REGION_AS923
*************************************/
#define OTAA_BAND (RAK_REGION_US915)
#define OTAA_DEVEUI {0x50, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
#define OTAA_APPEUI {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
#define OTAA_APPKEY {0xFF, 0x54, 0x53, 0x3D, 0x68, 0x77, 0x6F, 0x75, 0x70, 0x20, 0x4E, 0x61, 0x69, 0x61, 0x14, 0x17}
uint16_t maskBuff = 0x0002; // configure the mask for channels 8-15
/** Packet buffer for sending */
uint8_t collected_data[64] = { 0 };
void recvCallback(SERVICE_LORA_RECEIVE_T * data)
{
if (data->BufferSize > 0) {
Serial.println(“Something received!”);
for (int i = 0; i < data->BufferSize; i++) {
Serial.printf("%x", data->Buffer[i]);
}
Serial.print("\r\n");
}
}
void joinCallback(int32_t status)
{
Serial.printf(“Join status: %d\r\n”, status);
}
/*************************************
- enum type for LoRa Event
RAK_LORAMAC_STATUS_OK = 0,
RAK_LORAMAC_STATUS_ERROR,
RAK_LORAMAC_STATUS_TX_TIMEOUT,
RAK_LORAMAC_STATUS_RX1_TIMEOUT,
RAK_LORAMAC_STATUS_RX2_TIMEOUT,
RAK_LORAMAC_STATUS_RX1_ERROR,
RAK_LORAMAC_STATUS_RX2_ERROR,
RAK_LORAMAC_STATUS_JOIN_FAIL,
RAK_LORAMAC_STATUS_DOWNLINK_REPEATED,
RAK_LORAMAC_STATUS_TX_DR_PAYLOAD_SIZE_ERROR,
RAK_LORAMAC_STATUS_DOWNLINK_TOO_MANY_FRAMES_LOSS,
RAK_LORAMAC_STATUS_ADDRESS_FAIL,
RAK_LORAMAC_STATUS_MIC_FAIL,
RAK_LORAMAC_STATUS_MULTICAST_FAIL,
RAK_LORAMAC_STATUS_BEACON_LOCKED,
RAK_LORAMAC_STATUS_BEACON_LOST,
RAK_LORAMAC_STATUS_BEACON_NOT_FOUND,
*************************************/
void sendCallback(int32_t status)
{
if (status == RAK_LORAMAC_STATUS_OK) {
Serial.println(“Successfully sent”);
} else {
Serial.printf(“Sending failed with %ld\n”, status);
}
}
void setup()
{
Serial.begin(115200, RAK_AT_MODE);
delay(2000);
Serial.println("RAKwireless LoRaWan OTAA Example");
Serial.println("------------------------------------------------------");
// Start BLE UART advertisement for ever
api.ble.settings.blemode(RAK_BLE_UART_MODE);
api.ble.advertise.start(0);
if(api.lorawan.nwm.get() != 1)
{
Serial.printf("Set Node device work mode %s\r\n",
api.lorawan.nwm.set(1) ? "Success" : "Fail");
api.system.reboot();
}
// OTAA Device EUI MSB first
uint8_t node_device_eui[8] = OTAA_DEVEUI;
// OTAA Application EUI MSB first
uint8_t node_app_eui[8] = OTAA_APPEUI;
// OTAA Application Key MSB first
uint8_t node_app_key[16] = OTAA_APPKEY;
if (!api.lorawan.appeui.set(node_app_eui, 8)) {
Serial.printf("LoRaWan OTAA - set application EUI is incorrect! \r\n");
return;
}
if (!api.lorawan.appkey.set(node_app_key, 16)) {
Serial.printf("LoRaWan OTAA - set application key is incorrect! \r\n");
return;
}
if (!api.lorawan.deui.set(node_device_eui, 8)) {
Serial.printf("LoRaWan OTAA - set device EUI is incorrect! \r\n");
return;
}
if (!api.lorawan.band.set(OTAA_BAND)) {
Serial.printf("LoRaWan OTAA - set band is incorrect! \r\n");
return;
}
Serial.printf("LoRaWan OTAA - seting chanels 8-15! \r\n");
Serial.printf("Get channel mask %s\r\n",
api.lorawan.mask.get(&maskBuff) ? "Success" : "Fail");
Serial.printf("Channel mask = %04X\r\n", maskBuff);
delay(1000);
if (!api.lorawan.deviceClass.set(RAK_LORA_CLASS_C)) {
Serial.printf("LoRaWan OTAA - set device class is incorrect! \r\n");
return;
}
if (!api.lorawan.njm.set(RAK_LORA_OTAA)) // Set the network join mode to OTAA
{
Serial.printf("LoRaWan OTAA - set network join mode is incorrect! \r\n");
return;
}
if (!api.lorawan.join()) // Join to Gateway
{
Serial.printf("LoRaWan OTAA - join fail! \r\n");
return;
}
/** Wait for Join success */
while (api.lorawan.njs.get() == 0) {
Serial.print("Wait for LoRaWAN join...");
api.lorawan.join();
delay(10000);
}
if (!api.lorawan.adr.set(false)) {
Serial.printf("LoRaWan OTAA - set adaptive data rate is incorrect! \r\n");
return;
}
if (!api.lorawan.rety.set(1)) {
Serial.printf("LoRaWan OTAA - set retry times is incorrect! \r\n");
return;
}
if (!api.lorawan.cfm.set(0)) {
Serial.printf("LoRaWan OTAA - set confirm mode is incorrect! \r\n");
return;
}
/** Check LoRaWan Status*/
Serial.printf("Duty cycle is %s\r\n", api.lorawan.dcs.get()? "ON" : "OFF"); // Check Duty Cycle status
Serial.printf("Packet is %s\r\n", api.lorawan.cfm.get()? "CONFIRMED" : "UNCONFIRMED"); // Check Confirm status
uint8_t assigned_dev_addr[4] = { 0 };
api.lorawan.daddr.get(assigned_dev_addr, 4);
Serial.printf("Device Address is %02X%02X%02X%02X\r\n", assigned_dev_addr[0], assigned_dev_addr[1], assigned_dev_addr[2], assigned_dev_addr[3]); // Check Device Address
Serial.printf("Uplink period is %ums\r\n", OTAA_PERIOD);
Serial.println("");
api.lorawan.registerRecvCallback(recvCallback);
api.lorawan.registerJoinCallback(joinCallback);
api.lorawan.registerSendCallback(sendCallback);
}
void uplink_routine()
{
/** Payload of Uplink */
uint8_t data_len = 0;
collected_data[data_len++] = (uint8_t) ‘t’;
collected_data[data_len++] = (uint8_t) ‘e’;
collected_data[data_len++] = (uint8_t) ‘s’;
collected_data[data_len++] = (uint8_t) ‘t’;
Serial.println("Data Packet:");
for (int i = 0; i < data_len; i++) {
Serial.printf("0x%02X ", collected_data[i]);
}
Serial.println("");
/** Send the data package */
if (api.lorawan.send(data_len, (uint8_t *) & collected_data, 2, true, 1)) {
Serial.println("Sending is requested");
} else {
Serial.println("Sending failed");
}
}
void loop()
{
static uint64_t last = 0;
static uint64_t elapsed;
if ((elapsed = millis() - last) > OTAA_PERIOD) {
uplink_routine();
last = millis();
}
//Serial.printf("Try sleep %ums..", OTAA_PERIOD);
api.system.sleep.all(OTAA_PERIOD);
//Serial.println("Wakeup..");
}