#!/usr/bin/python # -*- coding:utf-8 -*- import smbus import time from pca9534d import PCA9534D address = 0x78 # Address for LoRa module busNumber = 1 # I2C bus number class expBoard: def __init__(self, addr, port): self.addr = addr self.bus = smbus.SMBus(port) def write_data(self, value): self.bus.write_byte(self.addr, value) def write_block_data(self, byte, array): self.bus.write_i2c_block_data(self.addr, byte, array) def read_nbytes_data(self, data, n): return self.bus.read_i2c_block_data(self.addr, data, n) def send_command(i2c_bus, command): """ Send a command to the LoRa module via I2C. The command is split into bytes and sent via I2C block write. """ command_bytes = [ord(char) for char in command] # Convert string to list of ASCII values command_bytes.append(0x0D) # Add carriage return '\r' command_bytes.append(0x0A) # Add newline '\n' i2c_bus.write_block_data(0x00, command_bytes) # Write to register 0x00 print(f"Sent command: {command}") def wait_for_lora_packet(i2c_bus, expected_length=16): """ Wait for and read the incoming LoRa packet. It reads a specified number of bytes and returns the result. """ print("Waiting for LoRa packet...") time.sleep(1) # Adjust delay as necessary rx = i2c_bus.read_nbytes_data(0x00, expected_length) # Read from register 0x00 # Convert byte list to string packet = ''.join([chr(n) for n in rx]) print(f"Received packet: {packet}") return packet def main(): # Initialize PCA9534D (for reset control) pca9534d = PCA9534D(i2c_bus=1, device_address=0x20) pca9534d.set_rst_high() # Set reset pin to HIGH # Initialize the I2C communication with the LoRa module i2c_bus = expBoard(address, busNumber) # Reset the LoRa module i2c_bus.write_data(123) time.sleep(0.7) # Wait for module reset # Set baud rate or any other initial configuration as needed i2c_bus.write_data(17) # Example setting baud rate to 57600 time.sleep(0.1) send_command(i2c_bus, "radio set mod lora") time.sleep(0.7) # Wait for command to process rx = i2c_bus.read_nbytes_data(0x00, len("ok")+2) print("Llegint: " + ''.join([chr(byte) for byte in rx])) send_command(i2c_bus, "radio set freq 868100000") time.sleep(0.7) # Wait for command to process rx = i2c_bus.read_nbytes_data(0x00, len("ok")+2) print("Llegint: " + ''.join([chr(byte) for byte in rx])) send_command(i2c_bus, "radio set pwr 14") time.sleep(0.7) # Wait for command to process rx = i2c_bus.read_nbytes_data(0x00, len("ok")+2) print("Llegint: " + ''.join([chr(byte) for byte in rx])) send_command(i2c_bus, "radio set sf sf12") time.sleep(0.7) # Wait for command to process rx = i2c_bus.read_nbytes_data(0x00, len("ok")+2) print("Llegint: " + ''.join([chr(byte) for byte in rx])) send_command(i2c_bus, "radio set afcbw 125") time.sleep(0.7) # Wait for command to process rx = i2c_bus.read_nbytes_data(0x00, len("ok")+2) print("Llegint: " + ''.join([chr(byte) for byte in rx])) send_command(i2c_bus, "radio set rxbw 250") time.sleep(0.7) # Wait for command to process rx = i2c_bus.read_nbytes_data(0x00, len("ok")+2) print("Llegint: " + ''.join([chr(byte) for byte in rx])) send_command(i2c_bus, "radio set prlen 8") time.sleep(0.7) # Wait for command to process rx = i2c_bus.read_nbytes_data(0x00, len("ok")+2) print("Llegint: " + ''.join([chr(byte) for byte in rx])) send_command(i2c_bus, "radio set cr 4/8") time.sleep(0.7) # Wait for command to process rx = i2c_bus.read_nbytes_data(0x00, len("ok")+2) print("Llegint: " + ''.join([chr(byte) for byte in rx])) send_command(i2c_bus, "radio set sync 12") time.sleep(0.7) # Wait for command to process rx = i2c_bus.read_nbytes_data(0x00, len("ok")+2) print("Llegint: " + ''.join([chr(byte) for byte in rx])) send_command(i2c_bus, "radio set bw 250") time.sleep(0.7) # Wait for command to process rx = i2c_bus.read_nbytes_data(0x00, len("ok")+2) print("Llegint: " + ''.join([chr(byte) for byte in rx])) send_command(i2c_bus, "radio set wdt 0") time.sleep(0.7) rx = i2c_bus.read_nbytes_data(0x00, len("ok")+2) print("Llegint: " + ''.join([chr(byte) for byte in rx])) send_command(i2c_bus, "radio rxstop") time.sleep(0.7) rx = i2c_bus.read_nbytes_data(0x00, len("ok")+2) print("Llegint: " + ''.join([chr(byte) for byte in rx])) # Send "mac pause" command send_command(i2c_bus, "mac pause") time.sleep(0.7) # Wait for command to process rx = i2c_bus.read_nbytes_data(0x00, len("4294967245")+2) print("Llegint: " + ''.join([chr(byte) for byte in rx])) # Send "radio tx AABB" command to send send_command(i2c_bus, "radio tx AABB") time.sleep(0.7) rx = i2c_bus.read_nbytes_data(0x00, len("ok")+2) print("Llegint: " + ''.join([chr(byte) for byte in rx])) time.sleep(0.7) # Wait and read LoRa packet rx = i2c_bus.read_nbytes_data(0x00, len("radio_tx_ok") + 2) print("Rebut: " + ''.join([chr(byte) for byte in rx])) if __name__ == "__main__": main()