Skip to main content

Connect an MQTT device

Get the template

Check template files at examples/mqttDeviceShifu.

Modify configuration

In examples/mqttDeviceShifu/mqtt_deploy/mqtt_edgedevice.yaml:

...
spec:
sku: "testMQTT" # change this value
connection: Ethernet
address: 192.168.62.231:1883 # change this value
protocol: MQTT
protocolSettings:
MQTTSetting:
MQTTTopic: /test/test # change this value

In examples/mqttDeviceShifu/mqtt_deploy/mqtt_deviceshifu_configmap.yaml:

...
data:
driverProperties: |
driverSku: testMQTT
driverImage:
instructions: |
instructions: # A command can subscribe to a topic, if you need to subscribe to multiple topics, just add a command
get_topicmsg1: # change this value
protocolPropertyList:
MQTTTopic: "/test/test1" # change this value
get_topicmsg2:
protocolPropertyList:
MQTTTopic: "/test/test2"
... # You can continue to configure commands and corresponding topics according to your own needs, just continue to add according to this format

Deploy deviceShifu

Run this command:

kubectl apply -f examples/mqttDeviceShifu/mqtt_deploy

Get the latest MQTT message from device

In your cluster:

curl deviceshifu-mqtt/get_topicmsg1

Where get_topicmsg1 is the embedded query string.

Return from MQTT deviceShifu:

{"mqtt_message":"test2333","mqtt_receive_timestamp":"2022-04-29 08:57:49.9492744 +0000 UTC m=+75.407609501"}

Where mqtt_message is the latest data string from device, mqtt_receive_timestamp is the timestamp when the message was received.

Publish MQTT messages through deviceShifu

In your cluster:

curl -X POST -d 'test_publish' deviceshifu-mqtt/get_topicmsg1

Where get_topicmsg1 is the embedded query or publish string.

After publishing, query MQTT deviceShifu again, The returned contents are as follows:

{"mqtt_message":"test_publish","mqtt_receive_timestamp":"2022-04-29 08:57:59.7397692 +0000 UTC m=+75.407609501"}