IotAgent UltraLight over MQTT 不处理来自 MQTT 代理的值

IotAgent UltraLight over MQTT is not processing values from MQTT broker

我正在学习以下教程: https://github.com/FIWARE/tutorials.IoT-over-MQTT

完全按照教程中的步骤 1 到 3 进行操作(工作正常),在步骤 4 中,我使用 mosquitto_pub 将消息发布到 MQTT 代理。

但是,MQTT 消息不会被 IotAgent 处理。当我在步骤 5 中尝试从 Context Broker 检索数据时,我可以看到这一点。

使用调试器:

docker exec -it [IOTAGENT CONTAINER] pm2 monit

发布 MQTT 消息时也没有任何反应。我试过以下消息格式:

mosquitto_pub -t "/4jggokgpepnvsb2uv4s40d59ov/motion001/attrs" -m "c|1" 
mosquitto_pub -t /4jggokgpepnvsb2uv4s40d59ov/motion001/attrs -m '{"c":1}'

Mosquitto 经纪人 运行 可以订阅。 我缺少参数吗? IotAgent 未按预期工作的原因可能是什么? 几个月前我已经成功测试了设置,但不幸的是测试参数不再可用。

这很可能是一个 docker 网络问题,在本教程中所有组件(包括 MQTT Broker)都已放置在同一个 docker 网络中,因此可以相互通信.我的猜测是您当前的设置不是这种情况。

Docker Compose File 的 IoT 代理部分包括 IOTA_MQTT_HOST=mosquitto 环境变量,它正在侦听同一网络中名为 mosquitto 的 MQTT 代理。

要通过 MQTT 发送测量,您需要从同一网络中调用 MQTT 发布者。

docker run -it --rm --name mqtt-publisher --network \
  fiware_default efrecon/mqtt-client pub -h mosquitto -m "c|1" \
  -t "/4jggokgpepnvsb2uv4s40d59ov/motion001/attrs"

或者,对于实际安装,您必须更改 IOTA_MQTT_HOST 值以引用 MQTT Broker 的实际位置。

使用 https://github.com/FIWARE/tutorials.IoT-over-MQTT/blob/master/docker-compose.yml 编辑的 docker 撰写文件,我能够 运行 我的测试成功。部署命令:

docker stack deploy -c docker-compose.yml fiware

docker-compose.yml:

version: "3.5"
services:
  orion:
    image: fiware/orion
    hostname: orion
    container_name: fiware-orion
    depends_on:
      - mongo-db
    networks:
      - default
    ports:
      - "1026:1026"
    command: -dbhost mongo-db -logLevel DEBUG
    healthcheck:
      test: curl --fail -s http://orion:1026/version || exit 1

  iot-agent:
    image: fiware/iotagent-ul
    hostname: iot-agent
    container_name: fiware-iot-agent
    depends_on:
      - mongo-db
      - mosquitto
    networks:
      - default
    expose:
      - "4061"
      - "7896"
    ports:
      - "4061:4061"
      - "7896:7896"
    environment:
      - IOTA_CB_HOST=orion # name of the context broker to update context
      - IOTA_CB_PORT=1026 # port the context broker listens on to update context
      - IOTA_NORTH_PORT=4061
      - IOTA_REGISTRY_TYPE=mongodb #Whether to hold IoT device info in memory or in a database
      - IOTA_LOG_LEVEL=DEBUG # The log level of the IoT Agent
      - IOTA_TIMESTAMP=true # Supply timestamp information with each measurement
      - IOTA_CB_NGSI_VERSION=v2 # use NGSIv2 when sending updates for active attributes
      - IOTA_AUTOCAST=true # Ensure Ultralight number values are read as numbers not strings
      - IOTA_MONGO_HOST=mongo-db # The host name of MongoDB
      - IOTA_MONGO_PORT=27017 # The port mongoDB is listening on
      - IOTA_MONGO_DB=iotagentul # The name of the database used in mongoDB
      - IOTA_MQTT_HOST=mosquitto # The host name of the MQTT Broker
      - IOTA_MQTT_PORT=1883 # The port the MQTT Broker is listening on to receive topics
      - IOTA_DEFAULT_RESOURCE= # Default is blank. I'm using MQTT so I don't need a resource
      - IOTA_PROVIDER_URL=http://iot-agent:4061
    healthcheck:
      test: curl --fail -s http://iot-agent:4061/iot/about || exit 1

  mongo-db:
    image: mongo:3.6
    hostname: mongo-db
    container_name: db-mongo
    expose:
      - "27017"
    ports:
      - "27017:27017" # localhost:27017
    networks:
      - default
    command: --bind_ip_all --smallfiles
    volumes:
      - mongo-db:/data

  mosquitto:
    image: eclipse-mosquitto
    hostname: mosquitto
    container_name: mosquitto
    expose:
      - "1883"
      - "9001"
    ports:
      - "1883:1883"
      - "9001:9001"
    volumes:
      - ./mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf
    networks:
      - default
networks:
  default:
    ipam:
      config:
        - subnet: 172.18.1.0/24

volumes:
  mongo-db: ~

这些是成功的 运行ning 步骤:

0) 健康检查

curl http://localhost:4061/iot/about

1) 创建服务组

curl -iX POST \
  'http://localhost:4061/iot/services' \
  -H 'Content-Type: application/json' \
  -H 'fiware-service: testservice' \
  -H 'fiware-servicepath: /iot_ul' \
  -d '{
 "services": [
   {
     "apikey":      "hallo",
     "cbroker":     "http://orion:1026",
     "entity_type": "multiSensor",
     "resource":    "/iot/d"
   }
 ]
}'

2) 创建传感器

curl -X POST \
  'http://localhost:4061/iot/devices' \
  -H "Content-Type: application/json" \
  -H "fiware-service: testservice" \
  -H "fiware-servicepath: /iot_ul" \
  -d '{
  "devices": [
    {
      "device_id": "sensor01",
      "entity_name": "LivingRoomSensor1",
      "entity_type": "multiSensor",
      "protocol":    "PDI-IoTA-UltraLight",
      "transport":   "MQTT",
      "attributes": [
        { "object_id": "t", "name": "Temperature", "type": "Integer" },
        { "object_id": "l", "name": "Luminosity", "type": "Integer" }
      ]
    }
  ]
}' 

3) 通过 curl 更新值

curl --location --request POST 'http://localhost:7896/iot/d?i=sensor01&k=hallo' \
--header 'Content-Type: text/plain' \
--header 'fiware-service: testservice' \
--header 'fiware-servicepath: /iot_ul' \
--data-raw 't|1|l|3'

4) 检查值是否更新

curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "Fiware-Service: testservice" -H "Fiware-ServicePath: /iot_ul" -d '{
    "entities": [
        {
            "isPattern": "false",
            "id": "LivingRoomSensor1",
            "type": "multiSensor"
        }
    ]
}' 'http://localhost:1026/v1/queryContext'

5) 通过 mqtt 更新值(也适用于其他机器 - 添加参数 -h "hostaddress")

mosquitto_pub  -m "t|2|l|5" -t "/hallo/sensor01/attrs"