computenodes / dragino

LoRaWAN implementation in python
GNU Affero General Public License v3.0
27 stars 10 forks source link

Change the DevNonce when retrying a join. #14

Closed BNNorman closed 3 years ago

BNNorman commented 3 years ago

During testing my end device on TTN V3, I created a loop (see below) which tried to re-join after a period of time when the code didn't get a JOIN_ACCEPT back (that's another issue) .

NOTE: This is not really an issue once the device has had a successful join and cached the keys.

D = Dragino("dragino.ini", logging_level=logLevel)
D.join()

print("Waiting for JOIN ACCEPT")
start=time.time()

while not D.registered():
    if time.time()-start>15:
      start=time.time()
      print("Retrying join")
      D.join()
    sleep(2)

The TTN V3 console shows an error something like 'devNonce already used'.

If the program is halted and restarted that isn't an issue but my use case involves a remote end-node which cannot be accessed manually although I could add code to use systemctl to restart my service. However I think this suggestion is neater and easier.

edit dragino.py Immediately after line 291 in join() add the line self.devnonce=[randrange(256),randrange(256]]. That way, whenever a call to join() occurs the devnonce is changed.

pjb304 commented 3 years ago

Yes, I see the issue, I'd not come across it because the application I'm using it in it joins once, and then caches the credentials, and that process is called manually. Another option with the existing code would be to create a new instance of the Dragino object (which would give it a new devNonce), but I agree that moving the dev nonce generation code into join makes sense. Although we seem to have differing line numbers.

BNNorman commented 3 years ago

line numbers yes - my mistake. I use gpsd to get the GPS info and cache the TPV reading with a timestamp so that calls to get_gps() don't block my main loop. The gps time reading is then corrected using the timestamp later.