[英]IoTAgent (JSON) over MQTT doesn't receive measurements from mosquitto
[英]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 代理。
但是,IotAgent 不會處理 MQTT 消息。 當我嘗試從 Context Broker 檢索數據時,我可以看到這一點,如步驟 5 所示。
使用調試器:
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 文件的 IoT 代理部分包含IOTA_MQTT_HOST=mosquitto
環境變量,該變量正在偵聽同一網絡中名為mosquitto
的 MQTT Broker。
要通過 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 compose 文件,我能夠成功運行我的測試。 部署命令:
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: ~
這些是成功運行的步驟:
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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.