xg590 / SX1276

MicroPython Library for SX1276 (A Long Range Radio Chip from Semtech)
Apache License 2.0
34 stars 8 forks source link

Retry sending a certain number of times #5

Closed atonughosh closed 1 year ago

atonughosh commented 1 year ago

Hi,

Another feature that would be of great help is to set the number of retries to send a packet if the ACK is not received within the timeout. Also, without ACK sending messages (not broadcasts), i.e., 'REQ' without ACK.

I have implemented two-way communication by defining a new packet type that builds on the "REQ" type. I'm quite not sure how to go about it.

xg590 commented 1 year ago

It's easy to implement a retrying mechanism. I have implemented the same mechanism in another project (xg590/Tello-Python). I don't know what is "req without ack". Can you elaborate on that?

atonughosh commented 1 year ago

It's easy to implement a retrying mechanism. I have implemented the same mechanism in another project (xg590/Tello-Python). I don't know what is "req without ack". Can you elaborate on that?

Thanks for the reference to the repo for the retry mechanism.

By "req without ack" I mean, sending data to a particular node like a REQ packet but without an ACK mechanism like it is there in the REQ packet type now.

xg590 commented 1 year ago

But you can specify a receiver id no matter what the packet type (BRD is fine). In other words, you can broadcast a packet in which a receiver id is specified with the current code. An Ack packet will not be expected by the broadcaster and will not be send by the receiver.

atonughosh commented 1 year ago

But you can specify a receiver id no matter what the packet type. In other words, you can broadcast a packet in which a receiver is specified with the current code. An Ack packet will not be expected by the broadcaster and will not be send by the receiver.

Ok, I mistook the receiver ID in the BRD packet. Great! I will use this then. Could you please help me with the two-way communication?

xg590 commented 1 year ago

I will try to implement the two-way comm and retry mechanism this weekend.

atonughosh commented 1 year ago

I will try to implement the two-way comm and retry mechanism this weekend.

Thanks a ton. I'll wait for it.

xg590 commented 1 year ago

I will try to implement the two-way comm and retry mechanism this weekend.

Thanks a ton. I'll wait for it.

Hi bro, I have implemented the two-way comm, please try the following sample code and let me know your result.

Heltec WiFi LoRa 32 V2

LoRa_MISO_Pin = 19 LoRa_MOSI_Pin = 27 LoRa_SCK_Pin = 5 LoRa_CS_Pin = 18 LoRa_RST_Pin = 14 LoRa_DIO0_Pin = 26 LoRa_DIO1_Pin = 35 LoRa_DIO2_Pin = 34 SPI_CH = 1

random.seed(11)
channels2Hopping = [914_000_000+200_000 * random.randint(0,10) for i in range(128)] # 914~916 MHz

LoRa_id = 1 lora = SX1276(LoRa_RST_Pin, LoRa_CS_Pin, SPI_CH, LoRa_SCK_Pin, LoRa_MOSI_Pin, LoRa_MISO_Pin, LoRa_DIO0_Pin, LoRa_DIO1_Pin, LoRa_id, channels2Hopping, two_way_comm=True)

payload = str(random.randint(100,65536))+") Hello~" print(payload) lora.send(dst_id=0, msg=payload, pkt_type=lora.PKT_TYPE['REQ']) # Sender's lora_id is 1 and receiver's is 0

while not lora.is_available: time.sleep(1) print("lora is available now")

received_payload = None def get_payload(self, data, SNR, RSSI): global received_payload received_payload = data

lora.req_packet_handler = get_payload lora.brd_packet_handler = lambda self, data, SNR, RSSI: print("[New 'BRD' data]", data, SNR, RSSI)
lora.mode = 'RXCONTINUOUS'

while not lora.is_available: print("waiting") time.sleep(1)

print(received_payload)

* Receiver

from machine import Pin import time, urandom as random from lora import SX1276

Heltec WiFi LoRa 32 V2

LoRa_MISO_Pin = 19 LoRa_MOSI_Pin = 27 LoRa_SCK_Pin = 5 LoRa_CS_Pin = 18 LoRa_RST_Pin = 14 LoRa_DIO0_Pin = 26 LoRa_DIO1_Pin = 35 LoRa_DIO2_Pin = 34 SPI_CH = 1

random.seed(11)
channels2Hopping = [914_000_000+200_000 * random.randint(0,10) for i in range(128)] # Both sender and receiver need to know the sequence of frequences they are hopping on before the first hopping operation.

LoRa_id = 0 lora = SX1276(LoRa_RST_Pin, LoRa_CS_Pin, SPI_CH, LoRa_SCK_Pin, LoRa_MOSI_Pin, LoRa_MISO_Pin, LoRa_DIO0_Pin, LoRa_DIO1_Pin, LoRa_id, channels2Hopping, two_way_comm=True)

received_payload = None def get_payload(self, data, SNR, RSSI): global received_payload received_payload = data

lora.req_packet_handler = get_payload lora.brd_packet_handler = lambda self, data, SNR, RSSI: print("[New 'BRD' data]", data, SNR, RSSI)
lora.mode = 'RXCONTINUOUS'

while not lora.is_available: print("waiting") time.sleep(1)

if received_payload[-6:] == b'Hello~': payload = str(random.randint(100,65536))+") Hi ~ I have received your hello" print('[sending...]', payload) lora.send(dst_id=1, msg=payload, pkt_type=lora.PKT_TYPE['REQ']) else: payload = str(random.randint(100,65536))+") Hi ~ I have not receive your hello" print('[anyway, still sending...]', payload) lora.send(dst_id=1, msg=payload, pkt_type=lora.PKT_TYPE['REQ'])


Edit: Just made a blunder that I used a print function in the packet_handler. Delete two print functions...
xg590 commented 1 year ago

I think a retry mechanism is not necessary in lora.py. You can implement it in sender.py

atonughosh commented 1 year ago

I will try to implement the two-way comm and retry mechanism this weekend.

Thanks a ton. I'll wait for it.

Hi bro, I have implemented the two-way comm, please try the following sample code and let me know your result.

  • Sender
from machine import Pin 
import time, urandom as random
from lora import SX1276

# Heltec WiFi LoRa 32 V2
LoRa_MISO_Pin = 19
LoRa_MOSI_Pin = 27
LoRa_SCK_Pin  =  5
LoRa_CS_Pin   = 18
LoRa_RST_Pin  = 14
LoRa_DIO0_Pin = 26
LoRa_DIO1_Pin = 35
LoRa_DIO2_Pin = 34
SPI_CH        =  1  

random.seed(11)   
channels2Hopping = [914_000_000+200_000 * random.randint(0,10) for i in range(128)] # 914~916 MHz    

LoRa_id = 1
lora = SX1276(LoRa_RST_Pin, LoRa_CS_Pin, SPI_CH, LoRa_SCK_Pin, LoRa_MOSI_Pin, LoRa_MISO_Pin, LoRa_DIO0_Pin, LoRa_DIO1_Pin, LoRa_id, channels2Hopping, two_way_comm=True) 

payload = str(random.randint(100,65536))+") Hello~"
print(payload)
lora.send(dst_id=0, msg=payload, pkt_type=lora.PKT_TYPE['REQ']) # Sender's lora_id is 1 and receiver's is 0

while not lora.is_available:
    time.sleep(1)
print("lora is available now")

received_payload = None
def get_payload(self, data, SNR, RSSI):
    global received_payload
    received_payload = data

lora.req_packet_handler = get_payload
lora.brd_packet_handler = lambda self, data, SNR, RSSI: print("[New 'BRD' data]", data, SNR, RSSI)  
lora.mode = 'RXCONTINUOUS' 

while not lora.is_available:
    print("waiting")
    time.sleep(1)

print(received_payload)
  • Receiver
from machine import Pin 
import time, urandom as random
from lora import SX1276

# Heltec WiFi LoRa 32 V2
LoRa_MISO_Pin = 19
LoRa_MOSI_Pin = 27
LoRa_SCK_Pin  =  5
LoRa_CS_Pin   = 18
LoRa_RST_Pin  = 14
LoRa_DIO0_Pin = 26
LoRa_DIO1_Pin = 35
LoRa_DIO2_Pin = 34
SPI_CH        =  1 

random.seed(11)   
channels2Hopping = [914_000_000+200_000 * random.randint(0,10) for i in range(128)] # Both sender and receiver need to know the sequence of frequences they are hopping on before the first hopping operation.

LoRa_id = 0
lora = SX1276(LoRa_RST_Pin, LoRa_CS_Pin, SPI_CH, LoRa_SCK_Pin, LoRa_MOSI_Pin, LoRa_MISO_Pin, LoRa_DIO0_Pin, LoRa_DIO1_Pin, LoRa_id, channels2Hopping, two_way_comm=True) 

received_payload = None
def get_payload(self, data, SNR, RSSI): 
    global received_payload
    received_payload = data

lora.req_packet_handler = get_payload
lora.brd_packet_handler = lambda self, data, SNR, RSSI: print("[New 'BRD' data]", data, SNR, RSSI)  
lora.mode = 'RXCONTINUOUS'

while not lora.is_available:
    print("waiting")
    time.sleep(1)

if received_payload[-6:] == b'Hello~':
    payload = str(random.randint(100,65536))+") Hi ~ I have received your hello"
    print('[sending...]', payload)
    lora.send(dst_id=1, msg=payload, pkt_type=lora.PKT_TYPE['REQ'])
else:
    payload = str(random.randint(100,65536))+") Hi ~ I have not receive your hello"
    print('[anyway, still sending...]', payload)
    lora.send(dst_id=1, msg=payload, pkt_type=lora.PKT_TYPE['REQ']) 

Edit: Just made a blunder that I used a print function in the packet_handler. Delete two print functions...

Thank you so much, I'll implement and let you know in a while.

xg590 commented 1 year ago

I have updated lora.py and put new sample code in sender.py and receiver.py.

atonughosh commented 1 year ago

I have updated lora.py and put new sample code in sender.py and receiver.py.

Hi, I was out of the station. Just returned. Implemented the sender and receiver, works like a charm. Thanks again.

xg590 commented 1 year ago

You are welcome~