예제 #1
0
def main():
    mqtt_client = MqttLocalClient(
                                  client_id          = MQTT_CLIENT_ID,
                                  host               = IIoT.MQTT_HOST,
                                  port               = IIoT.MQTT_PORT,
                                  subscription_paths = SUBSCRIBED_MQTT_CHANNELS
                                 )
    mqtt_client.start()

    losant_client = LosantClient(
                                  my_device_id         = MY_DEVICE_ID,
                                  my_app_access_key    = MY_APP_ACCESS_KEY,
                                  my_app_access_secret = MY_APP_ACCESS_SECRET,
                                )
    losant_client.set_callback(command_from_losant_callback)
    losant_client.name = 'Losant Thread'
    losant_client.start()

    # Get the data from message_queue
    while True:
        # Waiting for a new message in the queue
        message = mqtt_client.message_queue.get()

        #Get the message topic and its payload
        topic        = message.topic
        payload      = message.payload
        json_payload = json.loads(payload)
        input_name   = json_payload['data']['name']
        input_value  = json_payload['data']['value']

        # Perform actions
        if input_name == 'panelVoltage':
            losant_client.sendDeviceState( "my_first_numeric_value", input_value)
예제 #2
0
def main():
    mqtt_client = MqttLocalClient(client_id="telemetry-adafruitio",
                                  host=IIoT.MQTT_HOST,
                                  port=IIoT.MQTT_PORT,
                                  subscription_paths=[
                                      IIoT.MqttChannels.telemetry,
                                  ])
    mqtt_client.start()

    aio_client = AdafruitIoClient(my_key=ADAFRUIT_IO_KEY,
                                  my_username=ADAFRUIT_IO_USERNAME)
    aio_client.name = 'Adafruitio Thread'
    aio_client.subscribe(SUBSCRIBED_TOPIC)
    aio_client.on_message = on_message_received_from_telemetry
    aio_client.start()

    # Create a new feed in the cloud telemetry MAX 10 !!
    # You can create them also on the adafruit portal
    """
    for feedName in sensors:
        try:
            print(feedName)
            feed     = Feed(name = feedName )
            response = aio_client.create_feed(feed)
        except Exception as e:
            print( e )
    """

    # Get the data from message_queue
    while True:
        # Waiting for a new message in the queue
        message = mqtt_client.message_queue.get()

        #Get the message topic and its payload
        topic = message.topic
        payload = message.payload
        json_payload = json.loads(payload)
        input_name = json_payload['data']['name']
        input_value = json_payload['data']['value']

        # Perform actions
        try:
            if input_name == 'panelVoltage++':
                aio_client.send_data(input_name.lower(), input_value)
        except Exception as e:
            print(e)
예제 #3
0
def main():
    mqtt_client = MqttLocalClient(client_id=MQTT_CLIENT_ID,
                                  host=IIoT.MQTT_HOST,
                                  port=IIoT.MQTT_PORT,
                                  subscription_paths=[
                                      SUBSCRIBED_MQTT_CHANNELS,
                                  ])
    mqtt_client.start()

    # Get the data from message_queue
    while True:
        # Waiting for a new message in the queue
        message = mqtt_client.message_queue.get()

        #Get the message topic and its payload
        topic = message.topic
        payload = message.payload
        json_payload = json.loads(payload)
        input_name = json_payload['data']['name']
        input_value = json_payload['data']['value']

        # Perform actions
        output_value = data_manipulation(input_value)

        # Publish data
        message = packOutputMessage(output_value)
        mqtt_client.publish(IIoT.MqttChannels.persist, json.dumps(message))
예제 #4
0
def main():
    mqtt_client = MqttLocalClient(client_id="database-mongodb",
                                  host=IIoT.MQTT_HOST,
                                  port=IIoT.MQTT_PORT,
                                  subscription_paths=SUBSCRIBED_MQTT_CHANNELS)
    mqtt_client.start()

    # initialize database
    db = database_init()

    ## initialize db collections
    sensors_collection = db.sensors_collection
    events_collection = db.events_collection
    data_collection = db.data_collection

    # Get the data from message_queue
    while True:
        # Waiting for a new message in the queue
        message = mqtt_client.message_queue.get()

        #Get the message topic and its payload
        topic = message.topic
        payload = message.payload
        json_payload = json.loads(payload)
        input_name = json_payload['data']['name']
        input_value = json_payload['data']['value']

        print(message)

        # Perform actions
        obj = {input_name: input_value, 'date': datetime.datetime.now()}

        # depending on the input_name te value is written in a different caollection0
        if input_name in sensors:
            add_data(sensors_collection, obj)

        elif input_name in events:
            add_data(events_collection, obj)
예제 #5
0
def main():
    mqtt_client = MqttLocalClient(
        client_id=MQTT_CLIENT_ID,
        host=IIoT.MQTT_HOST,
        port=IIoT.MQTT_PORT,
        subscription_paths=SUBSCRIBED_MQTT_CHANNELS,
    )
    mqtt_client.start()

    conn = database_init()

    sensors_data = dict()
    last_data_arrived = None
    # Get the data from message_queue
    while True:
        # Waiting for a new message in the queue
        message = mqtt_client.message_queue.get()

        topic = message.topic
        payload = message.payload
        json_payload = json.loads(payload)
        input_name = json_payload['data']['name']
        input_value = json_payload['data']['value']

        now = time.time()

        # Perform actions
        if input_name in sensors:
            sensors_data[input_name] = input_value
            add_sensor_data(conn, input_name, input_value)

        if input_name in sensors and last_data_arrived is not None:
            if now - last_data_arrived > 1:
                add_sensors_data(conn, sensors_data)
                #:print(sensors_data)
        else:
            add_event_data(conn, input_value)
예제 #6
0
def main():
    mqtt_client = MqttLocalClient(
                                  client_id          = MQTT_CLIENT_ID,
                                  host               = IIoT.MQTT_HOST,
                                  port               = IIoT.MQTT_PORT,
                                  subscription_paths = SUBSCRIBED_MQTT_CHANNELS
                                 )
    mqtt_client.start()

    # initialize ounter and mean var
    i = 0
    mean = 0

    while True:
        # Waiting for a new message in the queue
        message = mqtt_client.message_queue.get()

        #Get the message topic and its payload
        topic   = message.topic
        payload = message.payload
        json_payload = json.loads(payload)

        if json_payload['data']['name'] == VARIABLE_NAME:
            # perform calculation
            mean = mean + float(json_payload['data']['value'])
            i = i + 1

            if i == MEAN_SAMPLES:
                # the variable name containing mean is made by
                # original_var_name _ number_of_samples_ (seconds) _mean
                output_name = VARIABLE_NAME + "_" + str(MEAN_SAMPLES) + "_sec_mean"

                # compute the mean
                output_value = mean / MEAN_SAMPLES

                # reset counter and mean var
                i = 0
                mean = 0

                # pack the json publish message
                message = IIoT.packOutputMessage(output_name ,output_value)

                # publish the message to the mqtt broker
                mqtt_client.publish(OUTPUT_CHANNEL, json.dumps(message))
예제 #7
0
def mqtt_connection():
    global mqtt_client
    mqtt_client = MqttLocalClient('sensor-opc_ua', 'localhost', 1883)
    mqtt_client.run()