def read_lora_message(): global last_heartbeat print("Calling receiveLoRaMessage to receive message...") msg0 = lora_interface.receiveLoRaMessage(receive_timeout) msg = msg0 # Copy the message safely. status = lora_interface.getLoRaStatus() print("Msg: " + msg + ", Status: " + str(status)) write_packet() gateway_snr = lora_interface.getLoRaSNRValue() gateway_rssi = lora_interface.getLoRaRSSIValue() gateway_rssi_packet = lora_interface.getLoRaRSSIpacketValue() # If no message available, try again. if len(msg) == 0: # Send a heartbeat message if it was last sent 10 mins ago. if (datetime.datetime.now() - last_heartbeat).total_seconds() < 10 * 60: return None last_heartbeat = datetime.datetime.now() device_state = { "message": "heartbeat", "address": gateway_address, "gateway": gateway_address, "gateway_snr": gateway_snr, "gateway_rssi": gateway_rssi, "gateway_rssi_packet": gateway_rssi_packet } return device_state device_address = lora_interface.getLoRaSender() recipient_address = lora_interface.getLoRaRecipient() packet_number = lora_interface.getLoRaPacketNumber() device_state = { "address": device_address, "gateway": recipient_address, "packet_number": packet_number, "gateway_snr": gateway_snr, "gateway_rssi": gateway_rssi, "gateway_rssi_packet": gateway_rssi_packet } try: # Msg contains an array of sensor data. Convert to dictionary. msg_split = msg.split("|") col = 0 for value in msg_split: key = fields[col] col = col + 1 value = value.strip() if value == "": continue # Allow "||" to denote skipping of fields. # If field is integer or decimal, convert accordingly. if key != "timestamp" and value.isdecimal(): if '.' in value: value = float(value) else: value = int(value) device_state[key] = value except Exception as e: # In case of parse errors e.g. due to Low Data Rate Optimization, record the error and continue. device_state["error"] = str(e) return device_state
receive_timeout = 10000 loop_delay = 10 device_address = 2 # My own address. gateway_address = 1 # Address of gateway. print("Calling setupLoRa...") status = lora_interface.setupLoRa(device_address, transmission_mode, transmission_channel, transmission_power) print("Status: " + str(status)) # Loop forever. while True: try: # Wait a while to receive a message. print("Calling receiveLoRaMessage to receive message...") msg = lora_interface.receiveLoRaMessage(receive_timeout) status = lora_interface.getLoRaStatus() print("Msg: " + msg + ", Status: " + str(status)) # Read the LoRa counters. status = lora_interface.getLoRaStatus() setup_done = lora_interface.getLoRaSetupDone() send_count = lora_interface.getLoRaSendCount() receive_count = lora_interface.getLoRaReceiveCount() snr = lora_interface.getLoRaSNRValue() rssi = lora_interface.getLoRaRSSIValue() rssi_packet = lora_interface.getLoRaRSSIpacketValue() timestamp = datetime.datetime.now().isoformat() msg = str(device_address) + "|" + \ str(gateway_address) + "|" + \
import lora_interface print("Calling setupLoRa...") status = lora_interface.setupLoRa() print(status) print("Calling sendLoRaMessage...") status = lora_interface.sendLoRaMessage(1, "test message") print(status) print("Calling receiveLoRaMessage...") msg = lora_interface.receiveLoRaMessage() print(msg) print("Calling getLoRaStatus...") status = lora_interface.getLoRaStatus() print(status)