If you need to set a device target temperature and report device status information in a smart home scenario (this is not a real product but only used to demonstrate IoT Hub's capabilities), you can follow the steps in this document.
The management backend updates the configuration and registration parameters of the device shadow and associates the corresponding callback functions through the TencentCloud APIs provided by IoT Hub to update the configuration locally.
For the implementation of relevant TencentCloud APIs for device shadow, please download iotcloud_RestAPI_python.zip. You need to configure your profile according to the RESTful API description. You can customize the features by modifying the parameters in airConditionerCtrl.py
in the RestAPI
folder.
sample/scenarized/aircond_shadow_sample_v2.c
. It adds the following logic to sample/scenarized/aircond_shadow_sample.c
:IOT_Shadow_Register_Property
to bind the shadow's configuration class attribute and callback function. When the shadow has a configuration change of this attribute, the underlying layer of the SDK will perform the corresponding callback. The temperatureDesire
field in the shadow is registered here, which means that when the application sets the target temperature for the device shadow, the local configuration can be corrected by the callback function to adjust the desired temperature. You can also implement custom configuration-based attribute listening and callback binding.rc = _register_config_shadow_property();
make
in the root directory of the SDK, compile, and get the aircond_shadow_sample_v2
executable program../aircond_shadow_sample_v2
in the ./output/release/bin
directory. Please note that if MQTT asymmetric encryption is used, the root certificate, device certificate, and device key files should be placed in the parent directory of ./../certs
../door_mqtt_sample come_home airConditioner1
in the ./output/release/bin
directory to turn on airConditioner
.INF|2018-01-11 20:52:50|aircond_shadow_sample_v2.c|main(377): Cloud Device Construct Success
INF|2018-01-11 20:52:50|aircond_shadow_sample_v2.c|main(389): Cloud Device Register Delta Success
In the output log, it can be seen that the on_temperature_actuate_callback
function has been called, indicating that the delta
topic sent by the shadow has been received, and the operation modify desire temperature to: 10.000000
has been performed for updating the locally set temperature.
INF|2018-01-11 21:04:31|aircond_shadow_sample_v2.c|on_temperature_actuate_callback(181): actuate callback jsonString=10},"desired":{"temperatureDesire":10},"reported":{"energyConsumption":0.0}},"timestamp":1515675847609,"version":5},"result":0,"timestamp":1515675871,"type":"get"}|dataLen=2
INF|2018-01-11 21:04:31|aircond_shadow_sample_v2.c|on_temperature_actuate_callback(184): modify desire temperature to: 10.000000
INF|2018-01-11 21:04:31|aircond_shadow_sample_v2.c|on_request_handler(123): Method=GET|Ack=ACK_ACCEPTED
INF|2018-01-11 21:04:31|aircond_shadow_sample_v2.c|on_request_handler(124): received jsonString={"clientToken":"EJSKHKIS1M-0","payload":{"metadata":{"delta":{"temperatureDesire":{"timestamp":1515675847609}},"desired":{"temperatureDesire":{"timestamp":1515675847609}},"reported":{"energyConsumption":{"timestamp":1515674881485}}},"state":{"delta":{"temperatureDesire":10},"desired":{"temperatureDesire":10},"reported":{"energyConsumption":0.0}},"timestamp":1515675847609,"version":5},"result":0,"timestamp":1515675871,"type":"get"}
In the above output log of airConditioner1
, it can be seen that the configuration operation has taken effect and airConditioner
has adjusted the locally set temperature.
ShadowSample.java is the device shadow class with the following main features:
connect()
API of TXShadowConnect
.disconnect()
API of TXShadowConnection
.registerProperty()
API of TXShadowConnection
.get()
API of TXShadowConnection
.update()
API of TXShadowConnection
.Before running the application, please enter the PRODUCT_ID, DEVICE_NAME, DEVICE_CERT_NAME, and DEVICE_KEY_NAME obtained in the previous steps for product and device creation and place the device certificate and device private key files in the assets directory:
/**
* Product ID
*/
private static final String PRODUCT_ID = "YOUR_PRODUCT_ID";
/**
* Device name
*/
protected static final String DEVICE_NAME = "YOUR_DEVICE_NAME";
/**
* Key
*/
private static final String SECRET_KEY = "YOUR_DEVICE_PSK";
/**
* Device certificate name
*/
private static final String DEVICE_CERT_NAME = "YOUR_DEVICE_NAME_cert.crt";
/**
* Device private key file name
*/
private static final String DEVICE_KEY_NAME = "YOUR_DEVICE_NAME_private.key";
Call the RESTful API UpdateDeviceShadow to simulate the home appliance management backend and publish the target temperature configuration.
The RESTful API request parameter is: deviceName=airConditioner1, state={"desired" : {"temperatureDesire": 10}}, productName=AirConditioner
, which adjusts the control temperature to 10°C.
The device reports its own status data to the device shadow, and the home appliance management backend directly gets data from the device shadow through the RESTful API.
As an example, the energy consumption status is reported to the device shadow through the call of IOT_Shadow_Update
by the following function in the SDK code sample/scenarized/aircond_shadow_sample_v2.c
. Then, the corresponding callback function is registered to handle the response of the device shadow. You can customize the reported attributes here.
_do_report_energy_consumption(...)
...
IOT_Shadow_Update(...)
./aircond_shadow_sample_v2
. Please note that if MQTT asymmetric encryption is used, the root certificate, device certificate, and device key files should be placed in the parent directory of ./../certs
../door_mqtt_sample come_home/leave_home airConditioner1
. door1
will communicate with airConditioner1
, and then airConditioner1
will be instructed to turn on through the rule engine. The reported changes in energy consumption and indoor temperature can be observed in the log, and the shadow data is obtained again through the RESTful API (as detailed in step 2):It can be seen that after airConditioner1
is turned on, the air conditioner energy consumption is dynamically reported to the shadow, and the data can be successfully queried and obtained through the RESTful API.
Please see the instructions in Directions for SDK for Android - Program implementation.
Please see the instructions in Directions for SDK for Android - Program compilation and execution.
Call the RESTful API GetDeviceShadow to get the status data of the shadow, which is used by the application to display the device's energy consumption.
The RESTful API request parameter is: deviceName=airConditioner1, productName=AirConditioner
.
Was this page helpful?