telefonicaid / fiware-orion

Context Broker and CEF building block for context data management, providing NGSI interfaces.
https://fiware-orion.rtfd.io/
GNU Affero General Public License v3.0
212 stars 265 forks source link

connecting Orion with MQTT #4180

Closed SrdjanZec closed 2 years ago

SrdjanZec commented 2 years ago

Hi GitHub Community, I am trying to connect raspberry pi with FIWARE Orion context broker system using MQTT protocol. I am using python3 mqtt_publisher.py codein order to publish the mqtt JSON data, I am connecting to the mosquitto docker container. The server (my laptop) has 4 docker containers so far, Mosquitto (in this case is used as MQTT broker), IOT-JSON-Agent (gets data from MQTT broker and pass it to Orion Context Broker), Orion Context Broker (uses that data and stores it in MongoDB). I have some issues, so i will try to present them here if someone can help me. `#MQTT PUBLISHER import json import random import time import paho.mqtt.client as mqtt

THE_BROKER = " " # my laptops ip address THE_TOPIC = "/12345/SensTemp/attrs" #MQTT topic, important CLIENT_ID =""

The callback for when the client receives a CONNACK response from the server

def on_connect(client, userdata, flags, rc): print("Connected to ", client._host, "port: ",client._port) print("Flags: ", flags, "returned code: ", rc)

The callback for when a message is published

def on_publish(client, userdata, mid): print("Yespub: msg published (mid={})".format(mid))

client = mqtt.Client(client_id=CLIENT_ID, clean_session=True, userdata=None, protocol=mqtt.MQTTv311, transport= "tcp")

client.on_connect = on_connect client.on_publish = on_publish client.connect(THE_BROKER, port=1883, keepalive=60) #The port of the mosquitto docker container

client.loop_start()

while True: s = {} s['the_temperature'] = random.randint(0,100) j = json.dumps(s)

msg_to_be_sent = j print(j) client.publish(THE_TOPIC, payload=msg_to_be_sent, qos = 1, retain = False) time.sleep(15)

client.loop_stop()

And here is the docker-compose.yml file: version: "3.3" services:

      mongo-db:
        image: mongo:3.6
        hostname: mongo-db
        container_name: db-mongo
        expose:
         - "27017"
        ports:
         - "27017:27017"
        networks:
         - default
        command: --bind_ip_all --smallfiles

      orion:
         image: fiware/orion
         ports:
             - "1026:1026"
         networks:
             - default
         depends_on:
             - mongo-db
         command: -dbhost mongo-db -logLevel DEBUG
         healthcheck:
               test: ["CMD", "curl", "-f", "http://0.0.0.0:1026/version"]
               interval: 1m
               timeout: 10s
               retries: 3

      mqtt:
        image: eclipse-mosquitto:1.6.14
        container_name: mqtt
        ports:
          - "1883:1883"
          - "9001:9001"
        expose:
          - "1883"
          - "9001"
        volumes:
          - /opt/mosquitto/var/run:/var/run
        networks:
          - default

      agent:
        image: fiware/iotagent-ul:latest
        hostname: agent
        container_name: fiware-agent
        depends_on:
         - mongo-db
         - mqtt
        networks:
         - default
        expose:
         - "4041"
         - "7896"
        ports:
         - "4041:4041"
         - "7896:7896"
        environment:
         - IOTA_CB_HOST=orion
         - IOTA_CB_PORT=1026
         - IOTA_NORTH_PORT=4041
         - IOTA_REGISTRY_TYPE=mongodb
         - IOTA_LOG_LEVEL=DEBUG
         - IOTA_TIMESTAMP=true
         - IOTA_CB_NGSI_VERSION=v2
         - IOTA_AUTOCAST=true
         - IOTA_MONGO_HOST=mongo-db
         - IOTA_MONGO_PORT=27017
         - IOTA_MONGO_DB=iotagentul
         - IOTA_PROVIDER_URL=http://localhost:4041
         - IOTA_MQTT_HOST=mqtt
         - IOTA_MQTT_PORT=1883
         - IOTA_DEFAULT_TRANSPORT=MQTT
         - IOTA_DEFAULT_RESOURCE= `

After some REST commands, in this case so far i have created of a service Group with these commands: `curl -iX POST \

'http://localhost:4041/iot/services' \

-H 'content-type: application/json' \

-H 'fiware-service: myClassRoom' \

-H 'fiware-servicepath: /' \

-d'{

"services": [
  {
    "apikey":         "12345",

    "cbroker":       "http://localhost:1026",

    "entity_type": "Device",

    "resource":      "/iot/d"
  }
]

}' Also I have created a Device in the IOT-Agent like this:

curl -iX POST \'http://localhost:4041/iot/devices' \  -H 'content-type: application/json' \  -H 'fiware-service: myClassRoom' \  -H 'fiware-servicepath: /' \  -d '{    "devices": [      {        "device_id":      "SensTemp",        "entity_name": "urn:ngsi-ld:temperature-sensor:001",        "entity_type":   "Device",        "protocol":       "PDI-IoTA-UltraLight",        "transport":      "MQTT",        "timezone":      "Europe/Madrid",        "attributes": [          { "object_id": "temperature", "name": "temperature", "type": "Integer" }        ]      }    ]  }'

` The server responded with HTTP 201 Response, but i think the agent is not processing data the right way, can someone give me a pointer on how can i register the device in context broker? Maybe i need to do that as well?

SrdjanZec commented 2 years ago

Hi all, I am able to connect to the agent, but it gives me a Parse error parsing incoming message[%] forcing to hex string, what does it mean? Also lvl=FATAL Ignoring:Type Error parsedMessage.reduce is not a function. What does it mean?

SrdjanZec commented 2 years ago

Solved! I was using the wrong IOT Agent.