def on_message(client, userdata, msg): # Convert the JSON payload to a Python dictionary. # The payload is in binary format so we need to decode as UTF-8. payload2 = json.loads(msg.payload.decode("utf-8")) print("Received message, topic: " + msg.topic + ", payload:\n" + json.dumps(payload2, indent=4, separators=(',', ': '))) # If there is a desired state in this message, then we send to the LoRa device. if payload2.get("state") is not None and payload2["state"].get("desired") is not None: # Get the desired state and convert to JSON. desired_state = payload2["state"]["desired"] msg = json.dumps(desired_state) # Send to LoRa device. lora_address = convert_mqtt_topic_to_lora_address(msg.topic) status = lora_interface.sendLoRaMessage(lora_address, msg) # TODO: Check status.
def on_message(client, userdata, msg): # Convert the JSON payload to a Python dictionary. # The payload is in binary format so we need to decode as UTF-8. payload2 = json.loads(msg.payload.decode("utf-8")) print("Received message, topic: " + msg.topic + ", payload:\n" + json.dumps(payload2, indent=4, separators=(',', ': '))) # If there is a desired state in this message, then we send to the LoRa device. if payload2.get("state") is not None and payload2["state"].get( "desired") is not None: # Get the desired state and convert to JSON. desired_state = payload2["state"]["desired"] msg = json.dumps(desired_state) # Send to LoRa device. lora_address = convert_mqtt_topic_to_lora_address(msg.topic) status = lora_interface.sendLoRaMessage(lora_address, msg) # TODO: Check status.
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) + "|" + \ str(status) + "|" + \ str(setup_done) + "|" + \ str(send_count) + "|" + \ str(receive_count) + "|" + \ str(snr) + "|" + \ str(rssi) + "|" + \ str(rssi_packet) + "|" + \ str(timestamp) print("Calling sendLoRaMessage to send device state to LoRa gateway " + str(gateway_address) + "...\n" + msg) status = lora_interface.sendLoRaMessage(gateway_address, msg) print("Status: " + str(status)) # Wait 10 seconds before sending the next message. time.sleep(loop_delay) except KeyboardInterrupt: # Stop the program when we press Ctrl-C. break except Exception as e: # For all other errors, we wait a while and resume. print("Exception: " + str(e)) time.sleep(10) continue
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)