OpenMTC/apps/mqttConnector
2019-01-11 18:01:19 +01:00
..
bin changes starting with python3 explicit 2018-10-14 17:03:08 +02:00
docker changes starting with python3 explicit 2018-10-14 17:03:08 +02:00
etc fixes mqtt docker start procedure 2018-06-18 14:51:34 +02:00
src/mqttconnector Merge branch 'master' into feature-python3-port 2019-01-11 18:01:19 +01:00
config.json fixes mqtt docker start procedure 2018-06-18 14:51:34 +02:00
MANIFEST.in add new mqtt connector app 2018-06-12 14:32:51 +02:00
mqtt_connector_diagram.png add new mqtt connector app 2018-06-12 14:32:51 +02:00
mqtt_connector_ocb_example.png add new mqtt connector app 2018-06-12 14:32:51 +02:00
README.md mqtt connector doc: minor enhancements 2018-06-12 16:08:00 +02:00
setup-mqttconnector.py changes starting with python3 explicit 2018-10-14 17:03:08 +02:00
utils.py simple python2 -> python3 conversions 2019-01-11 17:46:03 +01:00

Introduction

MQTTConnector is able to connect a MQTT-Broker with OpenMTC. The subscribes to specific topics on the broker in order to forward all published data to OpenMTC.

The MQTTConnector will subscribe itself on the MQTT-Broker (e.g. MeshBlu) to a specific first topics level. Published data is expected to contain at least two more topic levels, with one representing the location and one the device. This information will later be used to create an entity within the OpenMTC resource tree. If data is published, the MQTTConnector app will be notified via the subscription and will forward the data to an instance of OpenMTC (gateway or backend).

Getting started

In order to get an overview of the possible paramters of the app:

./apps/mqtt-connector -v

The most relevant paramters are the following:

EP (Endpoint): URL of the OpenMTC endpoint to use (backend or gateway) Example: "http://localhost:18000**

BROKER_EP (MQTT Endpoint): URL of the MQTT-Broker Example: "http://localhost:1883**

TOPIC_PRE: definition of the first topic level in order to filter for relevant topics Example: "OfficeBuilding**

TOPIC_INDEX_LOCATION: topic level representing the location Example: 1

TOPIC_INDEX_DEVICE: topic level representing the device Example: 2

BROKER_USER: username to connect to the MQTT Broker

BROKER_USER_PW: password to connect to the MQTT Broker

If used to together with the OpenMTC OrionContextBroker app, it is also possible to configure a specific fiware service.

Required Data Format

In order to work data needs to be published to the MQTT-Broker in an onem2m specific format. This will look like the following:

{
    "m2m:cin": {
        "con": "B64_ENCODED_SENML_PAYLOAD"
    }
}

where the B64_ENCODED_SENML_PAYLOAD could look like this:

decoded:

[
    {
        "bn": "23",
        "v": 27,
        "u": "Celsius",
        "t": 1527757260000,
        "n": "Temperatur"
    }
]

Example

In this example setup, we are using the MQTTConnector to forward data of a temperature sensor connected to the Mosquitto MQTT-Broker via OpenMTC to an instance of the Fiware Orion Context Broker. The following docker-compose configuration shows the setup in more detail.

version: "2"

services:
    backend:
        image: openmtc/backend-amd64
        ports:
            - "18000:18000"
        environment:
            - ONEM2M_NOTIFICATION_DISABLED=false
            - ONEM2M_HTTP_TRANSPORT_DISABLED=false
            - ONEM2M_HTTP_TRANSPORT_SSL_ENABLED=false
            - ONEM2M_HTTP_TRANSPORT_REQUIRE_CERT=false

    mosquitto:
        image: eclipse-mosquitto
        ports:
            - "1883:1883"

    mqtt-ipe:
        image: openmtc/mqtt-connector-app-amd64
        environment:
            - EP=http://backend:18000
            - BROKER_EP=mosquitto:1883
            - TOPIC_PRE=OfficeBuilding
            - TOPIC_INDEX_LOCATION=1
            - TOPIC_INDEX_DEVICE=2
        links:
            - mosquitto
            - backend

    orioncontextbroker-app:
        image: openmtc/orion-context-broker-app-amd64
        container_name: orioncontextbroker-app
        links:
            - backend
            - orion
        ports:
            - "8086:8086"
            - "8080:8080"
        environment:
            - EP=http://backend:18000
            - ORION_HOST=http://orion:1026
            - ORION_API=v2
            - ACCUMULATE_ADDRESS=http://orioncontextbroker-app:8080
            - LABELS=["openmtc:sensor_data"]

    mongo:
        image: mongo:3.4
        command: --nojournal

    orion:
        image: fiware/orion
        ports:
            - "1026:1026"
        command: -dbhost mongo -logLevel debug
        links:
            - mongo

The following will be published to topic OfficeBuilding/MeetingRoom/temperature:

{
    "m2m:cin": {
        "con": "W3tibjogMjMsIHY6IDI3LCB1OiBDZWxzaXVzLCB0OiAxNTI3NzU3MjYwMDAwLCBuOiBUZW1wZXJhdHVyfV0K"
    }
}

The string is in "con" is the base64 representation of:

[
    {
        "bn": "23",
        "v": 27,
        "u": "Celsius",
        "t": 1527757260000,
        "n": "temperature"
    }
]

If the data is successfully published to the MQTT-Broker it will be forwarded to the OCB. Therefore we are able to request the data from the OCB.

curl localhost:1026/v2/entities/MeetingRoom-temperature/attrs -H -s
{
  "temperature": {
    "type": "Float",
    "value": 27,
    "metadata": {
      "bn": {
        "type": "String",
        "value": 23
      },
      "timestamp": {
        "type": "String",
        "value": 152775726000
      },
      "unit": {
        "type": "String",
        "value": "Celsius"
      }
    }
  }
}