This the code that i’m currently using
// Set your credentials
#define OTAA_BAND (RAK_REGION_EU868)
#define OTAA_DEVEUI {0xAC, 0x1F, 0x09, 0xFF, 0xFE, 0x17, 0x87, 0xDB}
#define OTAA_APPEUI {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
#define OTAA_APPKEY {0x06, 0xA6, 0x98, 0x28, 0xAC, 0x7C, 0x00, 0x60, 0xCF, 0xD3, 0x1A, 0xF8, 0xCE, 0xFE, 0x77, 0x0B}
#if defined(WISBLOCK_BASE_5005) || defined(WISBLOCK_BASE_5005_O)
#define GREEN_LED_PIN LED_GREEN // Default green LED pin
#define BLUE_LED_PIN LED_BLUE // Default blue LED pin
#else
#define GREEN_LED_PIN WB_IO2 // Replace with your specific green LED pin
#define BLUE_LED_PIN WB_IO3 // Replace with your specific blue LED pin
#endif
void joinCallback(int32_t status) {
Serial.printf("Join status: %d\r\n", status);
}
void recvCallback(SERVICE_LORA_RECEIVE_T *data) {
if (data->BufferSize > 0) {
Serial.println("Downlink received!");
// Blink the green LED for downlink indication
for (int i = 0; i < 3; i++) { // Blink LED 3 times
digitalWrite(GREEN_LED_PIN, HIGH);
delay(200); // LED on for 200ms
digitalWrite(GREEN_LED_PIN, LOW);
delay(200); // LED off for 200ms
}
// Process received data
Serial.print("Received data: ");
for (int i = 0; i < data->BufferSize; i++) {
Serial.printf("0x%02X ", data->Buffer[i]);
}
Serial.println("\r\n");
}
}
void sendDataToGateway(const uint8_t *data, uint8_t size) {
uint8_t port = 1; // Application port
bool confirm = true; // Confirmed message
uint8_t retry = 3; // Retry attempts
if (api.lorawan.send(size, (uint8_t *)data, port, confirm, retry)) {
Serial.println("Data sent successfully!");
// Blink the blue LED for uplink indication
for (int i = 0; i < 3; i++) { // Blink LED 3 times
digitalWrite(BLUE_LED_PIN, HIGH);
delay(200); // LED on for 200ms
digitalWrite(BLUE_LED_PIN, LOW);
delay(200); // LED off for 200ms
}
} else {
Serial.println("Failed to send data.");
}
}
void setup() {
Serial.begin(115200, RAK_AT_MODE);
delay(2000);
Serial.println("RAKwireless LoRaWAN OTAA Example");
Serial.println("------------------------------------------------------");
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
// Set up LoRaWAN OTAA
uint8_t node_device_eui[8] = OTAA_DEVEUI;
uint8_t node_app_eui[8] = OTAA_APPEUI;
uint8_t node_app_key[16] = OTAA_APPKEY;
if (!api.lorawan.band.set(OTAA_BAND)) {
Serial.println("Failed to set band.");
return;
}
if (!api.lorawan.deviceClass.set(RAK_LORA_CLASS_C)) { // Continuous receive mode
Serial.println("Failed to set device class.");
return;
}
if (!api.lorawan.njm.set(RAK_LORA_OTAA)) { // Set OTAA mode
Serial.println("Failed to set network join mode.");
return;
}
api.lorawan.appeui.set(node_app_eui, 8);
api.lorawan.appkey.set(node_app_key, 16);
api.lorawan.deui.set(node_device_eui, 8);
api.lorawan.registerRecvCallback(recvCallback);
api.lorawan.registerJoinCallback(joinCallback);
// Attempt OTAA join with retry logic
for (int attempt = 0; attempt < 5; attempt++) {
if (api.lorawan.join()) {
Serial.println("LoRaWAN join successful!");
break;
}
Serial.printf("Retrying join... Attempt %d/5\n", attempt + 1);
delay(10000);
}
if (api.lorawan.njs.get() == 0) {
Serial.println("Failed to join network after 5 attempts.");
return;
}
Serial.println("RAK3172 is ready to send data and receive downlinks.");
}
void loop() {
uint8_t testData[] = {0x01, 0x02, 0x03}; // Test uplink data
sendDataToGateway(testData, sizeof(testData));
delay(3000); // Send data every 3 seconds
}