LoRa-Training/lora_sender.py

83 lines
2.7 KiB
Python

"""
LoRa-Training / led_sender - Send a simple lora message frequently
Copyright (C) 2024 Benjamin Burkhardt
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
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()