From 4018141a45aa5a7606da13985b07348ff2f9e375 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 18 Jan 2020 14:11:00 -0800 Subject: [PATCH] generate DevEUI based on macaddr if not defined --- main/credentials.h | 7 +++--- main/ttn.ino | 55 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/main/credentials.h b/main/credentials.h index b9e746d..6d91159 100644 --- a/main/credentials.h +++ b/main/credentials.h @@ -7,8 +7,8 @@ Credentials file #pragma once // Only one of these settings must be defined -#define USE_ABP -//#define USE_OTAA +//#define USE_ABP +#define USE_OTAA #ifdef USE_ABP @@ -31,7 +31,8 @@ Credentials file static const u1_t PROGMEM APPEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // This should also be in little endian format, see above. - static const u1_t PROGMEM DEVEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + // Note: You do not need to set this field, if unset it will be generated automatically based on the device macaddr + static u1_t DEVEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // This key should be in big endian format (or, since it is not really a // number but a block of memory, endianness does not really apply). In diff --git a/main/ttn.ino b/main/ttn.ino index 5c8f809..b6c7717 100644 --- a/main/ttn.ino +++ b/main/ttn.ino @@ -58,7 +58,7 @@ void os_getDevKey (u1_t* buf) { } #ifdef USE_OTAA void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8); } -void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8); } +void os_getDevEui (u1_t* buf) { memcpy(buf, DEVEUI, 8); } void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16); } #endif @@ -91,6 +91,24 @@ void forceTxSingleChannelDr() { } +// DevEUI generator using devices's MAC address - from https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/lorawan.cpp +void gen_lora_deveui(uint8_t *pdeveui) { + uint8_t *p = pdeveui, dmac[6]; + int i = 0; + esp_efuse_mac_get_default(dmac); + // deveui is LSB, we reverse it so TTN DEVEUI display + // will remain the same as MAC address + // MAC is 6 bytes, devEUI 8, set first 2 ones + // with an arbitrary value + *p++ = 0xFF; + *p++ = 0xFE; + // Then next 6 bytes are mac address reversed + for (i = 0; i < 6; i++) { + *p++ = dmac[5 - i]; + } +} + + static void printHex2(unsigned v) { v &= 0xff; if (v < 16) @@ -98,6 +116,24 @@ static void printHex2(unsigned v) { Serial.print(v, HEX); } +// generate DevEUI from macaddr if needed +void initDevEUI() { + bool needInit = true; + for(int i = 0; i < sizeof(DEVEUI); i++) + if(DEVEUI[i]) needInit = false; + + if(needInit) + gen_lora_deveui(DEVEUI); + + Serial.print("DevEUI: "); + for(int i = 0; i < sizeof(DEVEUI); i++) { + if (i != 0) + Serial.print("-"); + printHex2(DEVEUI[i]); + } + Serial.println(); +} + // LMIC library will call this method when an event is fired void onEvent(ev_t event) { switch(event) { @@ -126,16 +162,16 @@ void onEvent(ev_t event) { Serial.println(devaddr, HEX); Serial.print("AppSKey: "); for (size_t i=0; i