rgot-org / TheThingsNetwork_esp32

82 stars 24 forks source link

Saving and restoring join() keys after sleep / wakeup #20

Closed David-2D3FE9 closed 3 years ago

David-2D3FE9 commented 3 years ago

Hello, I am building a node witch will going to sleep for hour(s) before sending data to TTN and going to sleep again ( what originality! :-) )

My aim is to avoid proceeding a join request after each wake up. So I try differents examples and tests, but each time a join request is sended to TTN and new keys are returned.

My basic test sketch consist in this code execution while i am also looking at the "live data" in my TTN console:

Display_the_keys_brefore();
Make_the_join_request();
Display_the_keys_after();

esp_sleep_enable_timer_wakeup(60*1000000);
esp_deep_sleep_start();

So at the first loop I have printed: NwkSKey: 0000000000000000 After the first joint the key is not empty anymore (normal it is joined) NwkSKey: 9589D7F246FC4CC2E586B7CA5F2A190 But after the sleep / wake up the key is back to NwkSKey: 0000000000000000 and a new join request is sent

I tried to use the saveKeys(); just after joining ,and restoreKeys(); at the begin of the void setup( ){ But it is still display empty keys before joining.

Without sending the board to sleep the keys are correctly kept and an new request is not resent:

Make_the_join_request();
Delay(60 000);
Make_the_join_request();

Does anyone have an exemple of how to deal with storing keys with sleep mode ?

David-2D3FE9 commented 3 years ago

Ok, I've just used a more reasonable sleep mode: esp_light_sleep_start(); instead of esp_deep_sleep_start();

So no need of saveKeys(); or restoreKeys(); No more useless join request, keys are restored automaticaly everything work great !