Writing APPKEY & DEVEUI to EEPROM removes leading zeros

When we have need to deploy multiple nodes on the same application, it entails replicating same code and changing APPKEYs and DEVEUIs. This in my view limits the ability to scale without a certain level of expertise.

As a solution, we decided to write APPKEYs and DEVEUIs to the device EEPROM. And reading them out from there in the sketch. However in doing so I notice the EEPROM write commands eliminate the leading zero in any byte. So for example, 0x08, 0x09, 0x0F, 0x4F, 0x0C when written to EEPROM is read out as 0x8, 0x9, 0xF, 0x4F, 0xC.

I would appreciate any help getting the leading zeros retained.

Thanks

Looks to be an issue with your output format.
The input and output values are equivalent with and without the leading 0.

To display a hex character with leading 0’s use “%02X” with printf.
http://www.cplusplus.com/reference/cstdio/printf/

3 Likes

Yes that is so. From your response, I figured it might not be the leading zero and tried with a DEVEUI which had no leading zeros. when keys are read from EEPROM the node is unable to join.
DEVEUI is declared as …
static uint8_t DEVEUI[8];

I use this code to read them from EEPROM
for ( int s = 0; s < 8; ++s ){ DEVEUI[s] = EEPROM.read(s); }

Can’t figure out where the issue is from.

So when you hardcode values for EUI and Keys you can get it to join?
But when you read from the EEPROM you cannot?

You will need to output both hardcoded values and EEPROM values to find the difference.

1 Like

Thank you. I found the error. These lines

`void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8);}`

`void os_getDevKey (u1_t* buf) {  memcpy_P(buf, APPKEY, 16);}`

had to be changed to

`void os_getDevEui (u1_t* buf) { memcpy(buf, DEVEUI, 8);}`

`void os_getDevKey (u1_t* buf) {  memcpy(buf, APPKEY, 16);}`

"memcpy_P" (required to read from flash (program) memory) needs to be changed to “memcpy”. Works like a charm now.

1 Like