set dev eui from MCU-ID (UID) of STM32WL

This commit is contained in:
xlemmingx 2025-06-05 09:30:42 +02:00
parent bee5012e04
commit 55fbfaf1d5
2 changed files with 21 additions and 4 deletions

View File

@ -14,7 +14,9 @@ LOG_MODULE_REGISTER(lorawan_class_a);
extern bool relais_state; extern bool relais_state;
const static struct device *lora_dev; const static struct device *lora_dev;
static struct lorawan_join_config join_cfg; static struct lorawan_join_config join_cfg;
static uint8_t dev_eui[8];
static uint8_t app_key[] = LORAWAN_APP_KEY; static uint8_t app_key[] = LORAWAN_APP_KEY;
static uint8_t join_eui[] = LORAWAN_APP_EUI;
static void dl_callback(uint8_t port, uint8_t flags, int16_t rssi, int8_t snr, uint8_t len, static void dl_callback(uint8_t port, uint8_t flags, int16_t rssi, int8_t snr, uint8_t len,
const uint8_t *hex_data) const uint8_t *hex_data)
@ -46,6 +48,20 @@ struct lorawan_downlink_cb downlink_cb = {
void init_lorawan() void init_lorawan()
{ {
/* set dev eui from MCU-ID (UID) of STM32WL */
uint32_t uid_l = HAL_GetUIDw0();
uint32_t uid_h = HAL_GetUIDw1();
dev_eui[0] = (uid_h >> 24) & 0xFF;
dev_eui[1] = (uid_h >> 16) & 0xFF;
dev_eui[2] = (uid_h >> 8) & 0xFF;
dev_eui[3] = (uid_h) & 0xFF;
dev_eui[4] = (uid_l >> 24) & 0xFF;
dev_eui[5] = (uid_l >> 16) & 0xFF;
dev_eui[6] = (uid_l >> 8) & 0xFF;
dev_eui[7] = (uid_l) & 0xFF;
int err; int err;
lora_dev = DEVICE_DT_GET(DT_ALIAS(lora0)); lora_dev = DEVICE_DT_GET(DT_ALIAS(lora0));
@ -69,8 +85,9 @@ void init_lorawan()
lorawan_register_downlink_callback(&downlink_cb); lorawan_register_downlink_callback(&downlink_cb);
join_cfg.mode = LORAWAN_ACT_OTAA; join_cfg.mode = LORAWAN_ACT_OTAA;
join_cfg.dev_eui = dev_eui;
join_cfg.otaa.join_eui = join_eui;
join_cfg.otaa.app_key = app_key; join_cfg.otaa.app_key = app_key;
join_cfg.otaa.dev_nonce = sys_rand16_get();
} }
void join_network_otaa() void join_network_otaa()
@ -79,8 +96,6 @@ void join_network_otaa()
while (lorawan_join(&join_cfg) < 0) while (lorawan_join(&join_cfg) < 0)
{ {
LOG_ERR("lorawan_join_network failed: %d, retrying..", ret); LOG_ERR("lorawan_join_network failed: %d, retrying..", ret);
// use another dev nonce
join_cfg.otaa.dev_nonce = sys_rand16_get();
k_sleep(K_MSEC(5000)); k_sleep(K_MSEC(5000));
} }
LOG_INF("Succesfully joined network over OTAA"); LOG_INF("Succesfully joined network over OTAA");

View File

@ -7,9 +7,11 @@
#define LORAWAN_APP_KEY {0x71, 0x5A, 0x39, 0xB2, 0x86, 0xC3, \ #define LORAWAN_APP_KEY {0x71, 0x5A, 0x39, 0xB2, 0x86, 0xC3, \
0x37, 0xA3, 0xC4, 0xF0, 0x78, 0xF9, \ 0x37, 0xA3, 0xC4, 0xF0, 0x78, 0xF9, \
0x0F, 0x33, 0x07, 0x7D} 0x0F, 0x33, 0x07, 0x7D}
#define LORAWAN_APP_EUI {0x1F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, \
0x0F, 0x0F}
#define RETRY_DELAY K_MSEC(1000) #define RETRY_DELAY K_MSEC(1000)
#define SEND_INTERVALL K_SECONDS(20) #define SEND_INTERVALL K_MINUTES(5)
void init_lorawan(); void init_lorawan();