Update content

Triggering IoT Hub events via the API


Device information, such as events or errors, are reported on your Hub under the default topic prefix $SCW/events. For more information on Hub Events, you can read the IoT Hub events documentation.

In the following example we will trigger an event by trying to connect without security on a device where security is required.

First, we connect to previously created dev1 device as events consumer (we want only error events)

# In one terminal
mosquitto_sub \
-h $(jq -r '.endpoint' hub.json) -p 1883 \
-i $(jq -r '.device.id' dev1.json) \
-t '$SCW/events/error/#'

Then we create a secured-only device and try to connect it in insecure mode:

curl -sS -H "X-Auth-Token: $SCW_SECRET_KEY" -d '{
"hub_id": "'$(jq -r '.id' hub.json)'",
"name": "my_secured_device",
"allow_insecure": false
}' $IOT_API/devices > secure-dev.json

jq < secure-dev.json
# In another terminal
mosquitto_pub \
-h $(jq -r '.endpoint' hub.json) -p 1883 \
-i $(jq -r '.device.id' secure-dev.json) \
-t mytopic/mysubtopic \
-m 'This should fail'

dev1 will receive following message, on topic $SCW/events/device/<secure-dev-id>/error:

"severity": "error",
"object-type": "device",
"object-id": "dev3-id",
"msg":"connection refused because this device requires mutual TLS authentication",
"packet":"CONNECT: dup: false qos: 0 retain: false rLength: 48"