diff --git a/lora_receiver.py b/lora_receiver.py new file mode 100644 index 0000000..4da88bc --- /dev/null +++ b/lora_receiver.py @@ -0,0 +1,66 @@ +from SX127x import SX127x +from machine import Pin, SPI +from time import sleep + +def receive(lora, interrupt_pin=None, lcd_connected=True): + print("Starting LoRa Receiver") + print(f"IRQ Flags: {lora.get_irq_flags()}\n") + + if interrupt_pin: + print(f"An interrupt pin has been configured (GPIO {interrupt_pin})") + int_pin = Pin(interrupt_pin, Pin.OUT) + + if lcd_connected: + from PCF8574 import I2C_LCD + from machine import I2C + _i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) + lcd = I2C_LCD(_i2c, 0x27, 2, 16) + + lcd.move_to(0,0) + lcd.putstr(f"LoRa! RX................") + + while True: + if interrupt_pin: + if int_pin.value() == 1: + print("Interrupt pin pulled high!") + break + if lora.received_packet(): + payload = lora.read_payload() + lora.blink_led() + print(f"Received: {payload}") + print(f"RSSI: {lora.packet_rssi()}; SNR: {lora.packet_snr()}\n") + + if lcd_connected: + lcd.move_to(0,1) + lcd.putstr(f"{str(payload).lstrip('b\'').rstrip('\'')}{" ".join("" for i in range(17-len(payload)))}") + lcd.move_to(0,0) + lcd.putstr("LoRa! RX") + + +device_spi = SPI(baudrate = 10000000, + polarity = 0, phase = 0, bits = 8, firstbit = SPI.MSB, id=0, + sck = Pin(2, Pin.OUT, Pin.PULL_DOWN), + mosi = Pin(3, Pin.OUT, Pin.PULL_UP), + miso = Pin(4, Pin.IN, Pin.PULL_UP)) + + +parameters = { + 'frequency': 433E6, + 'tx_power_level': 10, + 'signal_bandwidth': 125E3, + 'spreading_factor': 7, + 'coding_rate': 5, + 'preamble_length': 8, + 'implicit_header': False, + 'sync_word': 0x12, + 'enable_CRC': False, + 'invert_IQ': False, + } + +lora = SX127x(device_spi, pins={"dio_0": 6, "ss": 5, "led": 26}, parameters=parameters) + +def run(): + receive(lora, 7, lcd_connected=True) + +if __name__ == '__main__': + run() \ No newline at end of file diff --git a/lora_sender.py b/lora_sender.py new file mode 100644 index 0000000..a370fae --- /dev/null +++ b/lora_sender.py @@ -0,0 +1,70 @@ +from SX127x import SX127x +from machine import Pin, SPI +from time import sleep + + +def send(lora, interrupt_pin=None, lcd_connected=True): + counter = 0 + print("Starting LoRa Sender") + + if interrupt_pin: + print(f"An interrupt pin has been configured (GPIO {interrupt_pin})") + int_pin = Pin(interrupt_pin, Pin.OUT) + + if lcd_connected: + from PCF8574 import I2C_LCD + from machine import I2C + _i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) + lcd = I2C_LCD(_i2c, 0x27, 2, 16) + + lcd.move_to(0,0) + lcd.putstr(f"LoRa! TX................") + + while True: + payload = f"Hello ({counter})" + lora.println(payload) + lora.blink_led() + print(f"Sent b'{payload}' RSSI: {lora.packet_rssi()}dB") + if interrupt_pin: + if int_pin.value() == 1: + print("Interrupt pin pulled high!") + break + if lcd_connected: + lcd.move_to(0,1) + lcd.putstr(f"{str(payload).lstrip('b\'').rstrip('\'')}{" ".join("" for i in range(17-len(payload)))}") + lcd.move_to(0,0) + lcd.putstr("LoRa! TX") + + counter += 1 + sleep(5) + + +device_spi = SPI(baudrate = 10000000, + polarity = 0, phase = 0, bits = 8, firstbit = SPI.MSB, id=0, + sck = Pin(2, Pin.OUT, Pin.PULL_DOWN), + mosi = Pin(3, Pin.OUT, Pin.PULL_UP), + miso = Pin(4, Pin.IN, Pin.PULL_UP)) + + +parameters = { + 'frequency': 433E6, + 'tx_power_level': 10, + 'signal_bandwidth': 125E3, + 'spreading_factor': 7, + 'coding_rate': 5, + 'preamble_length': 8, + 'implicit_header': False, + 'sync_word': 0x12, + 'enable_CRC': False, + 'invert_IQ': False, + } + +lora = SX127x(device_spi, pins={"dio_0": 6, "ss": 5, "led": 27}, parameters=parameters) + + +# for modularity +def run(): + send(lora, 7, lcd_connected=True) + +if __name__ == '__main__': + run() \ No newline at end of file