Skip to content

Commit fcfbdbd

Browse files
committed
added instructions on how to send data using mqtt and use the IoT to
send commands to the device
1 parent 983fa14 commit fcfbdbd

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

IoTSonnenUploader/DigitalTwin/Instructions.txt

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ oci iot digital-twin-adapter create --iot-domain-id $IOT_DOMAIN_OCID --digital-t
212212
export DIGITAL_TWIN_ADAPTER_OCID=`oci iot digital-twin-adapter list --iot-domain-id $IOT_DOMAIN_OCID --display-name $DIGITAL_TWIN_ADAPTER_NAME | jq -r '.data.items[0].id'`
213213

214214

215-
Finally we create a didital twin instance using the mapping based adaptor
215+
Finally we create a digital twin instance using the mapping based adaptor
216216
oci iot digital-twin-instance create --iot-domain-id $IOT_DOMAIN_OCID --auth-id $DEVICE_VAULT_SECRET_OCID --display-name $DEVICE_ID --digital-twin-adapter-id $DIGITAL_TWIN_ADAPTER_OCID --wait-for-state ACTIVE
217217

218218
# get the OCID of the digital twin
@@ -273,3 +273,39 @@ curl -i -X POST -u "$DEVICE_EXTERNAL_KEY:$DEVICE_SECRET" https://$IOT_DOMAIN_HOS
273273
check that this has been uploaded
274274
select * from snapshot_data where content_path in ('SoftwareVersion', 'TimeOfUseSchedule', 'ConfigurationTimestamp') order by TIME_OBSERVED desc
275275

276+
We have now sucesfully uploaded data using http(s) in unstructured, default structured and mapped structured format, but what it we want to send commands back to the device and have it change it's behaviour
277+
278+
First let's get the mqtt connection setup (you may want to test this using mqttx or similar)
279+
In the config file enable the mqtt configuration and status uploaders
280+
mqtt.configurationupload.enabled: true
281+
mqtt.statusupload.enabled: true
282+
set the port, broker host, broker username and broker password
283+
mqtt.broker.protocol: ssl
284+
mqtt.broker.host: the **DEVICE (NOT DATA)) hostname - $IOT_DOMAIN_HOST
285+
mqtt.broker.port: 8883
286+
mqtt.client.user-name: $DEVICE_EXTERNAL_KEY
287+
mqtt.client.password: $DEVICE_SECRET
288+
289+
disable http uploads
290+
iotservicehttps.configurationupload.enabled: false
291+
iotservicehttps.statusupload.enabled: false
292+
293+
Now just run the simulator again, the iot services automatically runs the mqtt server, and the adaprot sees the mqtt topic in the same way as the http path, so the iot servcie behaviour is the same
294+
you can se the data updating
295+
oci iot digital-twin-instance get-content --digital-twin-instance-id $DIGITAL_TWIN_INSTANCE_OCID | jq -r '.data'
296+
297+
298+
but what if we want to send a command to the IOT service to pass on to the device, for example changing it's configuration? In the data you'll have seen the telemetry value CommandDemoPlaceholder with the value "CommandTestPlaceholder" let's use MQTT to change that.
299+
300+
Before you do this you **MUST** ensure that the client is running as it sets up subscription to the mqtt server, without that then the mqtt topics used to send the command and receive the do not exist and the request will be rejected by the IOT Service.
301+
302+
While it's running look at the output and check that the CommandDemoPlaceholder is set to the default, do this using the output of the client OR the SQL interface or by getting the content of the instance :
303+
oci iot digital-twin-instance get-content --digital-twin-instance-id $DIGITAL_TWIN_INSTANCE_OCID | jq -r '.data'
304+
305+
Let's upload the command to the IOT service
306+
oci iot digital-twin-instance invoke-raw-json-command --digital-twin-instance-id $DIGITAL_TWIN_INSTANCE_OCID --request-endpoint house/sonnencommand/tims --request-data '{"commandIdentifier": "SET_PLACEHOLDER", "data":"I am another placeholder"}' --request-data-content-type application/json --request-duration PT10M --response-duration PT10M --response-endpoint house/sonnencommandresponse/tims
307+
308+
you will see the client receive the command and process it and send the response, you can also check the database contents using SQL developer :
309+
select * from RAW_COMMAND_DATA order by time_created desc
310+
311+
note the command will (briefly) show "SENT" in the status before switching to "COMPLETED"

0 commit comments

Comments
 (0)