computenodes / dragino

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

Add tx_done flag #17

Closed BNNorman closed 3 years ago

BNNorman commented 3 years ago

In order to manage air time I need to know how long a transmission has taken then wait 99x that to adhere to the 1% duty cycle.

send_bytes() returns immediately after passing the payload to the radio so that call duration alone doesn't give me the airtime which can be several seconds at some SFs.

my solution:

in dragino.py _init()_ add a tx_done flag e.g.

self.tx_done=False

in send_bytes() add this just before the switch to TX mode.

self.tx_done=False

Also, in join() just before the switch to TX mode add the above line.

In on_tx_done() add

self.tx_done=True

Now it become possible to know how long a transmission took and adjust the duty cycle accordingly. (This is not complete code)

from time import time,sleep()
from dragino import dragino
D=dragino(...)

D.join()
while not D.registered:
   sleep(0.1) # must be v small or use a non-blocking wait

start=time()
D.send("Hello world")
while not D.tx_done:
    sleep(0.1)
airtime=time()-start

# now we wait 99xairtime before the next transmission.

Ok this isn't microsecond accurate because send calls send_bytes() and both do some work before the radio switches to TX mode. But it helps.

This also helps with setting up for receiving in RX1 and/or RX2 - though I haven't got that bit working yet. I guess your use case doesn't have those problems because, as you said in an earlier exchange, you are in close proximity to your gateway. Mine became a brick when I tried to put it on V3 - ho hum.

pjb304 commented 3 years ago

This is a great idea, thank you. For our use we were able to do static analysis and work things out that way so this wasn't needed, but this is definitely worth having.