I am following the tutorial under: https://github.com/FIWARE/tutorials.IoT-over-MQTT
Steps 1 to 3 are followed exactly as in the tutorial (working fine), in Step 4 I am using mosquitto_pub to publish the message to the MQTT broker.
The MQTT message does not get processed by the IotAgent, however. I can see this when I try to retrieve the data from the Context Broker as in Step 5.
Using the debugger with:
docker exec -it [IOTAGENT CONTAINER] pm2 monit
also shows nothing happening when the MQTT message is published. I have tried the messaging formats below:
mosquitto_pub -t "/4jggokgpepnvsb2uv4s40d59ov/motion001/attrs" -m "c|1"
mosquitto_pub -t /4jggokgpepnvsb2uv4s40d59ov/motion001/attrs -m '{"c":1}'
The Mosquitto broker is running and can be subscribed to. Am I missing a parameter? What could be the reason that the IotAgent is not working as expected? I had tested the setup successfully some months ago, but unfortunately the testing parameters are not available anymore.
This is most likely a docker networking issue, within the tutorial all of the components (including the MQTT Broker) have been placed into the same docker network and hence can communicate with one another. My guess is that this is not the case for your current set up.
The IoT Agent section of the Docker Compose File includes the IOTA_MQTT_HOST=mosquitto
environment variable which is listening to the MQTT Broker called mosquitto
in the same network.
To send a measurement via MQTT, you will need to invoke the MQTT publisher from within the same network.
docker run -it --rm --name mqtt-publisher --network \
fiware_default efrecon/mqtt-client pub -h mosquitto -m "c|1" \
-t "/4jggokgpepnvsb2uv4s40d59ov/motion001/attrs"
Alternatively for a real installation you must alter the IOTA_MQTT_HOST
value to refer to the real location of the MQTT Broker.
With this edited docker compose file from https://github.com/FIWARE/tutorials.IoT-over-MQTT/blob/master/docker-compose.yml , I was able to run my tests successfully. Deployment command:
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: ~
These are the successfully running steps:
0) health check
curl http://localhost:4061/iot/about
1) create service group
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) create a sensor
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) update values via 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) check whether value was updated
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) update value over mqtt (works also from other machine - add parameters -h "hostaddress")
mosquitto_pub -m "t|2|l|5" -t "/hallo/sensor01/attrs"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.