hi
i’m working with rak3172 and gateway rak7289 and i want to make an automate system in which, in order to connect the rak3172 to the gateway, i will not need to enter manually the DevEUI I like this
so i tried to configure here a way to make it automatic by enabling “Auto Add LoRa Device.”
and in the code of rak3172 i configured it like this
#include <Wire.h>
// Set your credentials
#define OTAA_BAND (RAK_REGION_EU868)
#define OTAA_DEVEUI {0xAC, 0x1F, 0x09, 0xFF, 0xFE, 0x17, 0x87, 0xDB}
#define OTAA_APPEUI {0x0c, 0xad, 0xbb, 0x68, 0x06, 0xeb, 0xf6, 0x5e} // LSB format
#define OTAA_APPKEY {0x06, 0xA6, 0x98, 0x28, 0xAC, 0x7C, 0x00, 0x60, 0xCF, 0xD3, 0x1A, 0xF8, 0xCE, 0xFE, 0x77, 0x0B}
// RAK-specific pin definitions (for RAK13007 Relay)
#define RELAY_PIN WB_IO4 // Relay control pin (previously used in your code)
#define blue_led WB_IO2
#define DOWNLINK_PORT 2 // Must match ThingsBoard fPort
void joinCallback(int32_t status) {
Serial.printf("Join status: %d\r\n", status);
}
void recvCallback(SERVICE_LORA_RECEIVE_T *data) {
if (data->BufferSize > 0 && data->Port == DOWNLINK_PORT) {
// Process relay command (first byte only)
uint8_t command = data->Buffer[0];
if (command == 0x01) { // ON (AQ==)
digitalWrite(blue_led, HIGH); // Relay ON
Serial.println("Relay: ON");
}
else if (command == 0x00) { // OFF (AA==)
digitalWrite(blue_led, LOW); // Relay OFF
Serial.println("Relay: OFF");
}
}
}
void sendDataToGateway(const uint8_t *data, uint8_t size) {
uint8_t port = 1; // Application port
bool confirm = false;//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!");
} else {
Serial.println("Failed to send data.");
}
}
void setup() {
// Initialize Serial (with RAK_AT_MODE for RAK3172)
Serial.begin(115200, RAK_AT_MODE);
delay(2000); // Stabilization delay
// Startup message (minimal)
Serial.println("RAK3172 Relay Controller");
Serial.println("-----------------------");
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Relay OFF
pinMode(blue_led, OUTPUT);
digitalWrite(blue_led, 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};//{0x01, 0x02, 0x03}; // Test uplink data
sendDataToGateway(testData, sizeof(testData));
delay(10000); // Send data every 10 seconds
}
but the device didn’t autoadd. i even tried “#define OTAA_APPEUI {0x5e, 0xf6, 0xeb, 0x06, 0x68, 0xbb, 0xad, 0x0c}”
because i doubt in Application EUI is Little Endian in Firmware
so how to fix this problem