import time import uuid from sx1262 import SX1262 import uasyncio as asyncio from queue import Queue class FixedDeque: def __init__(self, maxlen): self.maxlen = maxlen self.data = [] def append(self, x): if len(self.data) >= self.maxlen: self.data.pop(0) self.data.append(x) def __iter__(self): return iter(self.data) class LoRaMessage: def __init__(self, sender_id: str, recipient_id: str, payload: bytes): self.sender_id = sender_id self.recipient_id = recipient_id self.payload = payload self.message_id = str(uuid.uuid4()) self.lifetime = 7 def encode(self) -> bytes: encoded = bytearray() encoded.append(len(self.sender_id)) encoded.extend(self.sender_id.encode("utf-8")) encoded.append(len(self.recipient_id)) encoded.extend(self.recipient_id.encode("utf-8")) encoded.extend(self.payload) return bytes(encoded) @staticmethod def decode(data: bytes): sender_id_len = data[0] sender_id = data[1:1+sender_id_len].decode("utf-8") recipient_id_len = data[1+sender_id_len] recipient_id = data[2+sender_id_len:2+sender_id_len+recipient_id_len].decode("utf-8") payload = data[2+sender_id_len+recipient_id_len:] return LoRaMessage(sender_id=sender_id, recipient_id=recipient_id, payload=payload) class LoRaHandler: def __init__(self): print("Initializing LoRa...") self.my_id = str(uuid.uuid4()) # initialize our radio, im using the HAT SX1262 hat for the pico self.radio = SX1262(spi_bus=1, clk=10, mosi=11, miso=12, cs=3, irq=20, rst=15, gpio=2) self.radio.begin(freq=915, bw=125, power=22) self.radio.setBlockingCallback(False, self.irq) self.recent_messages = FixedDeque(32) self.neighbours = set() self.message_queue = Queue() def send(self, data: bytes, recipient_id: str): msg_bytes = LoRaMessage(sender_id=self.my_id, recipient_id=recipient_id, payload=data).encode() self.radio.send(msg_bytes) def has_seen_message(self, msg: LoRaMessage) -> bool: if msg.message_id in self.recent_messages: return True self.recent_messages.append(msg.message_id) return False def forward(self, msg: LoRaMessage): # ensure we don't process a message twice if self.has_seen_message(msg): return msg_bytes = msg.encode() self.radio.send(msg_bytes) def advertise(self): # send an advertisement message to let neighbors know we're here self.send(b"ADVERTISE", recipient_id="BROADCAST") def on_receive(self, msg: LoRaMessage): # ensure we don't process a message twice if self.has_seen_message(msg): return # messages has a lifetime to prevent infinite loops. # if we receive a message with lifetime <= 0 we just drop it, # otherwise we decrement the lifetime and forward it to all # neighbors except the sender msg.lifetime -= 1 if msg.lifetime <= 0: return # forward messages if they aren't meant for us if (msg.recipient_id != self.my_id) and msg.recipient_id != "BROADCAST": print("Forwarding message...") self.forward(msg) return if msg.payload == b"ADVERTISE": if not msg.sender_id in self.neighbours: print(f"Neighbour discovered: {msg.sender_id}") self.neighbours.add(msg.sender_id) # tell the sender we heard them by sending a ping back self.send(b"Ping!", msg.sender_id) self.forward(msg) return asyncio.create_task(self.message_queue.put(msg)) def irq(self, events): #print(f"LORA EVENT: {events}") if events & SX1262.RX_DONE: msg, err = self.radio.recv() error = SX1262.STATUS[err] if error != "ERR_NONE": print(f"Error receiving message: {error}") return self.on_receive(LoRaMessage.decode(msg)) elif events & SX1262.TX_DONE: pass #print('TX done.')