samples/scenarized/door_mqtt_sample.c is the MQTT-based logic code for the door device.samples/scenarized/aircond_shadow_sample.c is the MQTT-based logic code for the air conditioner device.CMakeLists.txt to ensure that the following options exist:set(BUILD_TYPE "release")set(COMPILE_TOOLS "gcc")set(PLATFORM "linux")set(FEATURE_MQTT_COMM_ENABLED ON)set(FEATURE_MQTT_DEVICE_SHADOW ON)set(FEATURE_AUTH_MODE "KEY")set(FEATURE_AUTH_WITH_NOTLS OFF)set(FEATURE_DEBUG_DEV_INFO_USED OFF)
./cmake_build.sh
aircond_shadow_sample and door_mqtt_sample are in the output/release/bin folder.airConditioner1 device created above in the JSON file aircond_device_info.json."auth_mode":"KEY","productId":"GYT9V6D4AF","deviceName":"airConditioner1","key_deviceinfo":{"deviceSecret":"vXeds12qazsGsMyf5SMfs6OA6y"}
door1 device in another JSON file door_device_info.json."auth_mode":"KEY","productId":"S3EUVBRJLB","deviceName":"door1","key_deviceinfo":{"deviceSecret":"i92E3QMNmxi5hvIxUHjO8gTdg"}
aircond_shadow_sample, _register_subscribe_topics implements the subscription to the /{productID}/{deviceName}/control topic and registers the corresponding callback handler. After receiving a message from this topic, the callback determines whether the message content is "come_home" or "leave_home" and instructs airConditioner to turn on or off accordingly. _simulate_room_temperature simply simulates the changes in indoor temperature and energy consumption of airConditioner. You can also implement other custom logic.close:./output/release/bin/aircond_shadow_sample -c ./device_info.jsonINF|2019-09-16 23:25:17|device.c|iot_device_info_set(67): SDK_Ver: 3.1.0, Product_ID: GYT9V6D4AF, Device_Name: airConditioner1INF|2019-09-16 23:25:19|mqtt_client.c|IOT_MQTT_Construct(125): mqtt connect with id: Nh9Vc successDBG|2019-09-16 23:25:19|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(138): topicName=$shadow/operation/result/GYT9V6D4AF/airConditioner1|packet_id=56171DBG|2019-09-16 23:25:19|shadow_client.c|_shadow_event_handler(63): shadow subscribe success, packet-id=56171INF|2019-09-16 23:25:19|aircond_shadow_sample.c|event_handler(96): subscribe success, packet-id=56171INF|2019-09-16 23:25:19|shadow_client.c|IOT_Shadow_Construct(172): Sync device data successfullyINF|2019-09-16 23:25:19|aircond_shadow_sample.c|main(256): Cloud Device Construct SuccessDBG|2019-09-16 23:25:19|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(138): topicName=GYT9V6D4AF/airConditioner1/control|packet_id=56172DBG|2019-09-16 23:25:19|shadow_client.c|_shadow_event_handler(63): shadow subscribe success, packet-id=56172INF|2019-09-16 23:25:19|aircond_shadow_sample.c|event_handler(96): subscribe success, packet-id=56172INF|2019-09-16 23:25:19|aircond_shadow_sample.c|main(291): airConditioner state: closeINF|2019-09-16 23:25:19|aircond_shadow_sample.c|main(292): currentTemperature: 32.000000, energyConsumption: 0.000000
-t airConditioner1 -a come_home, you can see that the demo sends a JSON message {"action": "come_home", "targetDevice": "airConditioner1"} to the /{productID}/{deviceName}/event topic, which notifies the target device airConditioner1 of the homecoming event../output/release/bin/door_mqtt_sample -c ./output/release/bin/device_info.json -t airConditioner1 -a come_homeINF|2019-09-16 23:29:11|device.c|iot_device_info_set(67): SDK_Ver: 3.1.0, Product_ID: S3EUVBRJLB, Device_Name: door1INF|2019-09-16 23:29:11|mqtt_client.c|IOT_MQTT_Construct(125): mqtt connect with id: d89Wh successINF|2019-09-16 23:29:11|door_mqtt_sample.c|main(229): Cloud Device Construct SuccessDBG|2019-09-16 23:29:11|mqtt_client_publish.c|qcloud_iot_mqtt_publish(329): publish topic seq=46683|topicName=S3EUVBRJLB/door1/event|payload={"action": "come_home", "targetDevice": "airConditioner1"}INF|2019-09-16 23:29:11|door_mqtt_sample.c|main(246): Wait for publish ackINF|2019-09-16 23:29:11|door_mqtt_sample.c|event_handler(81): publish success, packet-id=46683
aircond_shadow_sample. You can see that the homecoming message sent by door1 and forwarded by the cloud has been received, the state has changed to open, and the indoor temperature currentTemperature (adjusted to the configured default temperature) and the energy consumption energyConsumption have changed dynamically.INF|2019-09-16 23:29:11|aircond_shadow_sample.c|main(291): airConditioner state: closeINF|2019-09-16 23:29:11|aircond_shadow_sample.c|main(292): currentTemperature: 32.000000, energyConsumption: 0.000000INF|2019-09-16 23:29:12|aircond_shadow_sample.c|on_message_callback(140): Receive Message With topicName:GYT9V6D4AF/airConditioner1/control, payload:{"action":"come_home","targetDevice":"airConditioner1"}INF|2019-09-16 23:29:12|aircond_shadow_sample.c|main(291): airConditioner state: openINF|2019-09-16 23:29:12|aircond_shadow_sample.c|main(292): currentTemperature: 31.000000, energyConsumption: 1.000000
/*** 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";
enterRoom(). If it is empty, perform initialization and initiate connect(); otherwise, determine whether the connection is valid, and if so, publish the event topic. come_home or leave_home) and targetDeviceName (name of the device to relay to) parameters you specified when running the program. You can organize the message content and topic on your own to execute your own message publishing logic.Airconditioner constructor and initiate connect().control topic.com.qcloud.iot I/com.qcloud.iot.mqtt.TXMqttConnection: Start connecting to ssl://connect.iot.qcloud.com:8883com.qcloud.iot I/com.qcloud.iot.mqtt.TXMqttConnection: onSuccess!com.qcloud.iot I/IoTEntryActivity: connected to ssl://connect.iot.qcloud.com:8883com.qcloud.iot I/com.qcloud.iot.mqtt.TXMqttConnection: Starting subscribe topic: ******/airConditioner1/controlcom.qcloud.iot I/IoTEntryActivity: onSubscribeCompleted, subscribe success
control topic. The corresponding message is:"{\\"action\\": \\"come_home\\", \\"targetDevice\\": \\"airConditioner1\\"}"
com.qcloud.iot I/com.qcloud.iot.mqtt.TXMqttConnection: Start connecting to ssl://connect.iot.qcloud.com:8883com.qcloud.iot I/com.qcloud.iot.mqtt.TXMqttConnection: onSuccess!com.qcloud.iot I/IoTEntryActivity: connected to ssl://connect.iot.qcloud.com:8883
com.qcloud.iot I/com.qcloud.iot.mqtt.TXMqttConnection: Starting publish topic: ******/door1/event Message: {"action": "come_home", "targetDevice": "airConditioner1"}
com.qcloud.iot I/com.qcloud.iot.mqtt.TXMqttConnection: Received topic: ******/airConditioner1/control, message: {"action":"come_home","targetDevice":"airConditioner1"}com.qcloud.iot D/IoTEntryActivity: receive command: open airconditioner, count: 1
control topic. The corresponding message is:"{\\"action\\": \\"leave_home\\", \\"targetDevice\\": \\"airConditioner1\\"}"
com.qcloud.iot I/com.qcloud.iot.mqtt.TXMqttConnection: Starting publish topic: ******/door1/event Message: {"action": "leave_home", "targetDevice": "airConditioner1"}
com.qcloud.iot I/com.qcloud.iot.mqtt.TXMqttConnection: Received topic: ******/airConditioner1/control, message: {"action":"leave_home","targetDevice":"airConditioner1"}com.qcloud.iot D/IoTEntryActivity: receive command: close airconditioner, count: 2
Feedback