The modified ABP example. I couldn't add the library's which arduino ABP example is using. If you
find the example in arduino then you can replace this code and you have the right library's.
#define ABP_PERIOD (10000)
/*************************************
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 ABP_BAND (RAK_REGION_AU915)
#define ABP_DEVADDR {0x26, 0x0C, 0x1A, 0x71}
#define ABP_APPSKEY {0x39, 0xCA, 0x25, 0x2D, 0x8D, 0x02, 0x89, 0xC4, 0x9B, 0xA0, 0x37, 0xE7, 0xB2, 0x9A, 0x48, 0xE6}
#define ABP_NWKSKEY {0xC0,0xD3,0xE1,0x2C,0xFA,0x7A,0x2E,0x12,0x23,0x0C,0xE8,0x0C,0x1E,0x6B,0x51,0x8C}
/** 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);
}
void sendCallback(int32_t status)
{
if (status == 0) {
Serial.println("Successfully sent");
} else {
Serial.println("Sending failed");
}
}
void setup()
{
Serial.begin(115200, RAK_AT_MODE);
Serial.println("RAKwireless LoRaWan ABP Example");
Serial.println("------------------------------------------------------");
// ABP Device Address MSB first
uint8_t node_dev_addr[4] = ABP_DEVADDR;
// ABP Application Session Key
uint8_t node_app_skey[16] = ABP_APPSKEY;
// ABP Network Session Key
uint8_t node_nwk_skey[16] = ABP_NWKSKEY;
if (!api.lorawan.daddr.set(node_dev_addr, 4)) {
Serial.printf("LoRaWan ABP - set device addr is incorrect! \r\n");
return;
}
if (!api.lorawan.appskey.set(node_app_skey, 16)) {
Serial.printf
("LoRaWan ABP - set application session key is incorrect! \r\n");
return;
}
if (!api.lorawan.nwkskey.set(node_nwk_skey, 16)) {
Serial.printf
("LoRaWan ABP - set network session key is incorrect! \r\n");
return;
}
if (!api.lorawan.band.set(ABP_BAND)) {
Serial.printf("LoRaWan ABP - set band is incorrect! \r\n");
return;
}
if (!api.lorawan.deviceClass.set(RAK_LORA_CLASS_A)) {
Serial.printf("LoRaWan ABP - set device class is incorrect! \r\n");
return;
}
if (!api.lorawan.njm.set(RAK_LORA_ABP)) // Set the network join mode to ABP
{
Serial.printf("LoRaWan ABP - set network join mode is incorrect! \r\n");
return;
}
if (!api.lorawan.adr.set(true)) {
Serial.printf("LoRaWan ABP - set adaptive data rate is incorrect! \r\n");
return;
}
if (!api.lorawan.rety.set(1)) {
Serial.printf("LoRaWan ABP - set retry times is incorrect! \r\n");
return;
}
if (!api.lorawan.cfm.set(1)) {
Serial.printf("LoRaWan ABP - set confirm mode is incorrect! \r\n");
return;
}
if (!api.lorawan.join()) {
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);
}
// /** 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", ABP_PERIOD);
Serial.println("");
api.lorawan.registerRecvCallback(recvCallback);
api.lorawan.registerJoinCallback(joinCallback);
api.lorawan.registerSendCallback(sendCallback);
api.lorawan.jn1dl.set(5000);
api.lorawan.jn2dl.set(6000);
api.lorawan.txp.set(5);
api.lorawan.rx1dl.set(1000);
api.lorawan.rx2dl.set(2000);
api.lorawan.rx2fq.set(923300000);
api.lorawan.nwm.set(1);
api.lorawan.rety.set(1);
api.lorawan.cfm.set(1);
api.lorawan.dr.set(5);
}
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");
Serial.println("Hij probeert te senden");
} else {
Serial.println("Sending failed");
}
}
void loop()
{
static uint64_t last = 0;
static uint64_t elapsed;
if ((elapsed = millis() - last) > ABP_PERIOD) {
uplink_routine();
last = millis();
}
//Serial.printf("Try sleep %ums..", ABP_PERIOD);
api.system.sleep.all(ABP_PERIOD);
//Serial.println("Wakeup..");
//Serial.println( !api.lorawan.txp.get());
Serial.printf("The transmit power is %d\r\n", api.lorawan.txp.get());
delay(500);
}