示例#1
0
import json
import platform
import lora_interface

#transmission_mode = 1 # Max range, slow data rate.
transmission_mode = 5 # Better reach, medium time on air. Test this mode because it doesn't mandate Low Data Rate Optimisation, which is not supported on Hope RF95.
transmission_channel = lora_interface.cvar.LORA_CH_10_868
transmission_power = "H"
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()
示例#2
0
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)
示例#3
0
def main():
    global isConnected
    # Create an MQTT client for connecting to AWS IoT via MQTT.
    client = mqtt.Client("lora_gateway")  # Client ID must be unique because AWS will disconnect any duplicates.
    client.on_connect = on_connect  # When connected, call on_connect.
    client.on_message = on_message  # When message received, call on_message.
    client.on_log = on_log  # When logging debug messages, call on_log.

    # Set the certificates and private key for connecting to AWS IoT.  TLS 1.2 is mandatory for AWS IoT and is supported
    # only in Python 3.4 and later, compiled with OpenSSL 1.0.1 and later.
    client.tls_set(awsCert, deviceCertificate, devicePrivateKey, ssl.CERT_REQUIRED, ssl.PROTOCOL_TLSv1_2)
    # Connect to AWS IoT server.  Use AWS command line "aws iot describe-endpoint" to get the address.
    print("Connecting to AWS IoT...")
    client.connect("A1P01IYM2DOZA0.iot.us-west-2.amazonaws.com", 8883, 60)
    # Start a background thread to process the MQTT network commands concurrently, including auto-reconnection.
    client.loop_start()

    # Setup the LoRa connection.  TODO: Check status
    print("Calling setupLoRa...")
    status = lora_interface.setupLoRa(gateway_address, transmission_mode, transmission_channel, transmission_power)
    print("Status: " + str(status))
    preamble_length = lora_interface.getLoRaPreambleLengthValue()
    print("Preamble Length: " + str(preamble_length))
    time.sleep(1)

    # Loop forever.
    while True:
        try:
            # If we are not connected yet to AWS IoT, wait 1 second and try again.
            if not isConnected:
                time.sleep(1)
                continue
            # Attempt to read 1 message from LoRa.
            device_state = read_lora_message()
            # If no message available, try again.
            if device_state is None:
                continue

            # Set the timestamp if not present.
            if device_state.get("timestamp") is None:
                device_state["timestamp"] = datetime.datetime.now().isoformat()
            # Send the reported device state to AWS IoT.
            payload = {
                "state": {
                    "reported": device_state
                }
            }
            print("Sending sensor data to AWS IoT...\n" +
                  json.dumps(payload, indent=4, separators=(',', ': ')))
            # Publish our sensor data to AWS IoT via the gateway topic and the LoRa topic.
            device_topic = convert_lora_address_to_mqtt_topic(device_state["address"])
            gateway_topic = convert_lora_address_to_mqtt_topic(gateway_address)
            client.publish(device_topic + "/shadow/update", json.dumps(payload))
            client.publish(gateway_topic + "/shadow/update", json.dumps(payload))
            print("Sent to AWS IoT")

        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
示例#4
0
def main():
    global isConnected
    # Create an MQTT client for connecting to AWS IoT via MQTT.
    client = mqtt.Client(
        "lora_gateway"
    )  # Client ID must be unique because AWS will disconnect any duplicates.
    client.on_connect = on_connect  # When connected, call on_connect.
    client.on_message = on_message  # When message received, call on_message.
    client.on_log = on_log  # When logging debug messages, call on_log.

    # Set the certificates and private key for connecting to AWS IoT.  TLS 1.2 is mandatory for AWS IoT and is supported
    # only in Python 3.4 and later, compiled with OpenSSL 1.0.1 and later.
    client.tls_set(awsCert, deviceCertificate, devicePrivateKey,
                   ssl.CERT_REQUIRED, ssl.PROTOCOL_TLSv1_2)
    # Connect to AWS IoT server.  Use AWS command line "aws iot describe-endpoint" to get the address.
    print("Connecting to AWS IoT...")
    client.connect("A1P01IYM2DOZA0.iot.us-west-2.amazonaws.com", 8883, 60)
    # Start a background thread to process the MQTT network commands concurrently, including auto-reconnection.
    client.loop_start()

    # Setup the LoRa connection.  TODO: Check status
    print("Calling setupLoRa...")
    status = lora_interface.setupLoRa(gateway_address, transmission_mode,
                                      transmission_channel, transmission_power)
    print("Status: " + str(status))
    preamble_length = lora_interface.getLoRaPreambleLengthValue()
    print("Preamble Length: " + str(preamble_length))
    time.sleep(1)

    # Loop forever.
    while True:
        try:
            # If we are not connected yet to AWS IoT, wait 1 second and try again.
            if not isConnected:
                time.sleep(1)
                continue
            # Attempt to read 1 message from LoRa.
            device_state = read_lora_message()
            # If no message available, try again.
            if device_state is None:
                continue

            # Set the timestamp if not present.
            if device_state.get("timestamp") is None:
                device_state["timestamp"] = datetime.datetime.now().isoformat()
            # Send the reported device state to AWS IoT.
            payload = {"state": {"reported": device_state}}
            print("Sending sensor data to AWS IoT...\n" +
                  json.dumps(payload, indent=4, separators=(',', ': ')))
            # Publish our sensor data to AWS IoT via the gateway topic and the LoRa topic.
            device_topic = convert_lora_address_to_mqtt_topic(
                device_state["address"])
            gateway_topic = convert_lora_address_to_mqtt_topic(gateway_address)
            client.publish(device_topic + "/shadow/update",
                           json.dumps(payload))
            client.publish(gateway_topic + "/shadow/update",
                           json.dumps(payload))
            print("Sent to AWS IoT")

        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