All Products
Search
Document Center

Quick Start

Last Updated: Jun 29, 2020

This chapter describes how to send and receive messages via MQTT topics, and how to implement a device by Device-Modelling programming method on Ubuntu.

Prepare the development environment

Install the local development environment

Install Ubuntu16.04

The compiling environment for this quick experience is Ubuntu16.04 on a 64-bit host. This environment has not been verified on other versions of Linux. We recommend that you install the release version consistent with Alibaba to avoid compatibility problems.

If you are using a Windows operating system, we recommend that you install the VMWare Virtualbox. Download Link: https://www.virtualbox.org/wiki/Downloads

Then, install the 64-bit Desktop version of Ubuntu 16.04.x LTS. Download Link: http://releases.ubuntu.com/16.04

Install the prerequisite software

The development and compiling environment of this SDK uses the following software: make-4.1, git-2.7.4, gcc-5.4.0, gcov-5.4.0, lcov-1.12, bash-4.3.48, tar-1.28, mingw-5.3.1

You can use the following command to install the necessary software:

  1. $ sudo apt-get install -y build-essential make git gcc

Develop through the Hacklab Web IDE

If you do not plan to install Ubuntu on your PC or laptop, you can also develop for the devices directly through the Hacklab Web IDE environment provided by the Alibaba Cloud IoT Platform. Hacklab Web IDE is a Linux development environment on the cloud, which already has all the necessary software installed. If you have an Alibaba Cloud account, you can log on to the Hacklab Web IDE and start development through a browser. Click here to log on to the Hacklab Web IDE.

Access the device using MQTT topics

Create products and devices

Use your Alibaba Cloud account to log on to the Alibaba Cloud IoT Platform to create products. Because you are implementing the product functions directly through MQTT topics, be sure to select “Basic version” when creating the product.

After creating a product, you need to create a specific device. Alibaba Cloud IoT Platform will generate identity information for the device.

If you are not familiar with product creation on the cloud, please learn how to create products and devices on Alibaba Cloud IoT Platform.

Implement product functions

Understand the root directory structure of the SDK

After the Link Kit SDK has been downloaded, the top-level directory structure is as follows:

  1. $ ls
  2. build-rules Config.in examples LICENSE make.settings project.mk sdk-c.mk win_board_conf.mk CMakeLists.txt Config.linkkit include makefile prebuilt README.txt src win.makefile

Modify Device Identification

To get a product’s ProductKey & Product Secret by clicking the ‘copy’ buttons indicated on the picture below:

To get a device’s DeviceName & DeviceSecret by clicking the ‘copy’ buttons indicated on the picture below:

basic-dev

Open the file ./examples/mqtt/mqtt-example.c, edit the following code snippet, to replace the values of PRODUCT_KEY/PRODUCT_SECRET/DEVICE_NAME/DEVICE_SECRET:

  1. #include "iot_import.h"
  2. #include "iot_export.h"
  3. #include "app_entry.h"
  4.  
  5. #define PRODUCT_KEY "a1MZxOdcBnO"
  6. #define PRODUCT_SECRET "h4I4dneEFp7EImTv"
  7. #define DEVICE_NAME "test_01"
  8. #define DEVICE_SECRET "t9GmMf2jb3LgWfXBaZD2r3aJrfVWBv56"

Initialize the device and establish a connection

The following code snippet briefly describes the initialization and connection establishment process of the device:

  1. int mqtt_client(void)
  2. {
  3. ...
  4. ...
  5.  
  6.  
  7. /* Enter the device identity information before attempting to establish an MQTT connection to the server*/
  8.  
  9. if (0 ! = IOT_SetupConnInfo(__product_key, __device_name, __device_secret, (void **)&pconn_info)) {
  10. EXAMPLE_TRACE("AUTH request failed!") ;
  11. rc = -1;
  12. goto do_exit;
  13. }
  14.  
  15. /* Initialize MQTT parameter */
  16. ...
  17. ...

Attempt to establish an MQTT connection to the server

  1. pclient = IOT_MQTT_Construct(&mqtt_params);
  2. if (NULL == pclient) {
  3. EXAMPLE_TRACE("MQTT construct failed");
  4. rc = -1;
  5. goto do_exit;
  6. }

Report data to the cloud

The following topics are defined in the example file:

  1. #define TOPIC_UPDATE "/"PRODUCT_KEY"/"DEVICE_NAME"/update"
  2. #define TOPIC_ERROR "/"PRODUCT_KEY"/"DEVICE_NAME"/update/error"
  3. #define TOPIC_GET "/"PRODUCT_KEY"/"DEVICE_NAME"/get"
  4. #define TOPIC_DATA "/"PRODUCT_KEY"/"DEVICE_NAME"/data"
  5.  
  6. /* topic format definitions*/
  7. #define TOPIC_UPDATE_FMT "/%s/%s/update"
  8. #define TOPIC_ERROR_FMT "/%s/%s/update/error"
  9. #define TOPIC_GET_FMT "/%s/%s/get"
  10. #define TOPIC_DATA_FMT "/%s/%s/data"

Alibaba Cloud IoT Platform will generate following 3 topics by default for a “Basic Edition” product:

The following code snippets show how to send data to the topic ‘UPDATE’:

  1. /* Initialize topic information */
  2. memset(&topic_msg, 0x0, sizeof(iotx_mqtt_topic_info_t));
  3. strcpy(msg_pub, "update: hello! start!") ;
  4. topic_msg.qos = IOTX_MQTT_QOS1;
  5. topic_msg.retain = 0;
  6. topic_msg.dup = 0;
  7. topic_msg.payload = (void *)msg_pub;
  8. topic_msg.payload_len = strlen(msg_pub);
  9. rc = IOT_MQTT_Publish(pclient, TOPIC_UPDATE, &topic_msg);
  10. if (rc < 0) {
  11. IOT_MQTT_Destroy(&pclient);
  12. EXAMPLE_TRACE("error occur when publish");
  13. rc = -1;
  14. goto do_exit;
  15. }
  16. EXAMPLE_TRACE("\n publish message: \n topic: %s\n payload: \%s\n rc = %d", TOPIC_UPDATE, topic_msg.payload, rc);

Subscribe and process data from the cloud

The example program subscribes to the topic TOPIC_DATA, and specifies the handler when receiving data from the topic:

  1. rc = IOT_MQTT_Subscribe(pclient, TOPIC_DATA, IOTX_MQTT_QOS1, _demo_message_arrive, NULL);
  2. if (rc < 0) {
  3. IOT_MQTT_Destroy(&pclient);
  4. EXAMPLE_TRACE("IOT_MQTT_Subscribe() failed, rc = %d", rc);
  5. rc = -1;
  6. goto do_exit;
  7. }
  8. IOT_MQTT_Yield(pclient, 200);
  9. HAL_SleepMs(2000);
  10. ...
  11. ...

In function _demo_message_arrive(), it simply prints the data out:

  1. static void _demo_message_arrive(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
  2. {
  3. iotx_mqtt_topic_info_pt ptopic_info = (iotx_mqtt_topic_info_pt) msg->msg;
  4.  
  5. /* print topic name and topic message */
  6. EXAMPLE_TRACE("----");
  7. EXAMPLE_TRACE("packetId: %d", ptopic_info->packet_id);
  8. EXAMPLE_TRACE("Topic: '%.*s' (Length: %d)",
  9. ptopic_info->topic_len,
  10. ptopic_info->ptopic,
  11. ptopic_info->topic_len);
  12. EXAMPLE_TRACE("Payload: '%.*s' (Length: %d)",
  13. ptopic_info->payload_len,
  14. ptopic_info->payload,
  15. ptopic_info->payload_len);
  16. EXAMPLE_TRACE("----");
  17. }

The following code snippet attempts to send data to this topic:

  1. do {
  2. cnt++;
  3. msg_len = snprintf(msg_pub, sizeof(msg_pub), "{\"attr_name\":\"temperature\", \"attr_value\":\"%d\"}", cnt);
  4. ...
  5. ...
  6. rc = IOT_MQTT_Publish(pclient, TOPIC_DATA, &topic_msg);
  7. if (rc < 0) {
  8. EXAMPLE_TRACE("error occur when publish");
  9. rc = -1;
  10. break;
  11. }
  12. EXAMPLE_TRACE("packet-id=%u, publish topic msg=%s", (uint32_t)rc, msg_pub);

Because the example subscribes to the topic TOPIC_DATA, so it will receive the data back after it sent the data to IoT platform.

Compile the sample program

Run the following command in the top-level directory of the SDK:

  1. make distclean
  2. make

After the compilation is successfully completed, the example program is generated in the output/release/bin directory of the current path:

  1. $ tree output/release
  2. output/release/
  3. +-- bin
  4. ...
  5. ...
  6. | +-- mqtt-example
  7. ...
  8. ...

Observe data

Run the following command:

  1. $ ./output/release/bin/mqtt-example

You can find the specified product in the console of Alibaba Cloud IoT Platform, and view the messages reported from the device in Log Service. You can click here to learn how to view the data reported from the device on the cloud.

You can also see the data from the cloud printed by the sample program in the Linux console:

  1. _demo_message_arrive|166 :: ----
  2. _demo_message_arrive|167 :: packetId: 35641
  3. _demo_message_arrive|171 :: Topic: '/a1ExpAkj9Hi/Example1/data' (Length: 26)
  4. _demo_message_arrive|175 :: Payload: '{"attr_name":"temperature", "attr_value":"1"}' (Length: 45)
  5. _demo_message_arrive|176 :: ----

Devlopping Products by Device Modelling

Create products and devices

You can create products on Alibaba Cloud IoT Platform and multiple services provided on it. The help links for creating products on Alibaba Cloud IoT Platform are as follows:

If a product needs to be created on the Smart Life IoT Platform (lifestyle-related services on Alibaba Cloud IoT Platform), you can log on to the Life IoT Platform to create the product.

In this example, you need to create a product that supports funtions:

  • property LightSwitch
    • Identifier: LightSwitch
    • Data type: enum
    • Values: 0 and 1
    • Read/Write type: read and write
  • Service Custom
    • Identifier: Custom
    • Call method: Asynchronous
    • Input parameter: {Identifier: transparency, Data type: int32, Value range: 0 ~ 100}
    • Output parameter: {Identifier: Contrastratio, Data type: int32, Value range: 0 ~ 100}
  • Event
    • Identifier: Error
    • Event type: Information
    • Output parameter: {Identifier: Errorcode, Data type: enum, Value range: 0, 1}

After production definition, IoT platform generates a TSL description file in JSON format for this product.

Implement product functions

Enter the device trituple into the routine

Replace the certificate (ProductKey/ProductSecret/DeviceName/DeviceSecret) in ./examples/linkkit/linkkit_example_solo.c with those of the device generated on the IoT platform.

  1. #define PRODUCT_KEY "a1csED27mp7"
  2. #define PRODUCT_SECRET "VuULINCrtTAzCSzp"
  3. #define DEVICE_NAME "AdvExample1"
  4. #define DEVICE_SECRET "3xGoxtWRscxPAoMxnJnjocZbNfmCTRi0"

Compile and run a program

Run the following command in the top-level directory of the SDK:

  1. $ make distclean
  2. $ make

After the compilation is successfully completed, the example program for the Pro-Edition product is generated in the output/release/bin directory of the current path, and its name is linkkit-example-solo.

Run the following command in the top-level directory of the SDK:

  1. $ ./output/release/bin/linkkit-example-solo

Observe data

The example program periodically reports the values of the LightSwitch property to the cloud, you can check the property on the IoT platform.

Report properties

This example program invokes function user_post_property to report properties. The sample cycles through reporting the payloads of various situations. Please be noted that this function tries to send several invalid propertis to the IoT platform, the purpose is to let developers be familiar with the erro code returned by IoT platfrom, you can remove them when implementing your own logic:

  1. void user_post_property(void)
  2. {
  3. static int example_index = 0;
  4. int res = 0;
  5. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  6. char *property_payload = "NULL";
  7. if (example_index == 0) {

Typical attribute reporting

  1. /* Typical Example */
  2. property_payload = "{\"LightSwitch\":1}";
  3. example_index++;
  4. }else if(...) {
  5. ...
  6. ...
  7. }
  8. res = IOT_Linkkit_Report(user_example_ctx->master_devid, ITM_MSG_POST_PROPERTY,
  9. (unsigned char *)property_payload, strlen(property_payload));
  10. EXAMPLE_TRACE("Post Property Message ID: %d", res);
  11. }

The logs printed during typical attribute reporting are as follows:

  1. [inf] dm_msg_request(218): DM Send Message, URI: /sys/a1csED27mp7/AdvExample1/thing/event/property/post, Payload: {"id":"1","version":"1.0","params":{"LightSwitch":1},"method":"thing.event.property.post"}
  2. [dbg] MQTTPublish(319): ALLOC: (160) / [224] @ 0x1da41a0
  3. [inf] MQTTPublish(378): Upstream Topic: '/sys/a1csED27mp7/AdvExample1/thing/event/property/post'
  4. [inf] MQTTPublish(379): Upstream Payload:

The following message is sent to the cloud:

  1. > {
  2. > "id": "1",
  3. > "version": "1.0",
  4. > "params": {
  5. > "LightSwitch": 1
  6. > },
  7. > "method": "thing.event.property.post"
  8. > }
  9. [inf] dm_client_publish(106): Publish Result: 0
  10. [dbg] alcs_observe_notify(105): payload:{"id":"1","version":"1.0","params":{"LightSwitch":1},"method":"thing.event.property.post"}
  11. [dbg] CoAPResourceByPath_get(176): Found the resource: /sys/a1csED27mp7/AdvExample1/thing/event/property/post
  12. [inf] dm_server_send(76): Send Observe Notify Result 0
  13. [dbg] dm_msg_cache_insert(79): dmc list size: 0
  14. user_post_property. 431: Post Property Message ID: 1
  15. [dbg] iotx_mc_cycle(1774): PUBLISH
  16. [inf] iotx_mc_handle_recv_PUBLISH(1549): Downstream Topic: '/sys/a1csED27mp7/AdvExample1/thing/event/property/post_reply'
  17. [inf] iotx_mc_handle_recv_PUBLISH(1550): Downstream Payload:

The following response is received from the cloud:

  1. < {
  2. < "code": 200,
  3. < "data": {
  4. < },
  5. < "id": "1",
  6. < "message": "success",
  7. < "method": "thing.event.property.post",
  8. < "version": "1.0"
  9. < }
  10. [dbg] iotx_mc_handle_recv_PUBLISH(1555): Packet Ident : 00000000
  11. [dbg] iotx_mc_handle_recv_PUBLISH(1556): Topic Length : 60
  12. [dbg] iotx_mc_handle_recv_PUBLISH(1560): Topic Name : /sys/a1csED27mp7/AdvExample1/thing/event/property/post_reply
  13. [dbg] iotx_mc_handle_recv_PUBLISH(1563): Payload Len/Room : 104 / 4935
  14. [dbg] iotx_mc_handle_recv_PUBLISH(1564): Receive Buflen : 5000
  15. [dbg] iotx_mc_handle_recv_PUBLISH(1575): delivering msg ...
  16. [dbg] iotx_mc_deliver_message(1291): topic be matched
  17. [inf] dm_msg_proc_thing_event_post_reply(258): Event Id: property
  18. [dbg] dm_msg_response_parse(167): Current Request Message ID: 1
  19. [dbg] dm_msg_response_parse(168): Current Request Message Code: 200
  20. [dbg] dm_msg_response_parse(169): Current Request Message Data: {}
  21. [dbg] dm_msg_response_parse(174): Current Request Message Desc: success
  22. [dbg] dm_ipc_msg_insert(87): dm msg list size: 0, max size: 50
  23. [dbg] dm_msg_cache_remove(142): Remove Message ID: 1
  24. [inf] _iotx_linkkit_event_callback(219): Receive Message Type: 30
  25. [inf] _iotx_linkkit_event_callback(221): Receive Message: {"id":1,"code":200,"devid":0,"payload":{}}
  26. [dbg] _iotx_linkkit_event_callback(476): Current Id: 1
  27. [dbg] _iotx_linkkit_event_callback(477): Current Code: 200
  28. [dbg] _iotx_linkkit_event_callback(478): Current Devid: 0

The log of the callback function:

  1. user_report_reply_event_handler. 300: Message Post Reply Received, Devid: 0, Message ID: 1, Code: 200, Reply: {}

Process a property-setting request

When an property-setting request is received in the exmaple code, the following callback function will be called:

  1. static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
  2. {
  3. int res = 0;
  4. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  5. EXAMPLE_TRACE("Property Set Received, Devid: %d, Request: %s", devid, request);

In the function, it just sends the value of the property back to the IoT platform, which will update the device’s propery record:

  1. res = IOT_Linkkit_Report(user_example_ctx->master_devid, ITM_MSG_POST_PROPERTY,
  2. (unsigned char *)request, request_len);
  3. EXAMPLE_TRACE("Post Property Message ID: %d", res);
  4. return 0;
  5. }

You can see the value set from the IoT flatform in the device logs:

  1. [dbg] iotx_mc_cycle(1774): PUBLISH
  2. [inf] iotx_mc_handle_recv_PUBLISH(1549): Downstream Topic: '/sys/a1csED27mp7/AdvExample1/thing/service/property/set'
  3. [inf] iotx_mc_handle_recv_PUBLISH(1550): Downstream Payload:

The property-setting request received from the cloud is as follows:

  1. < {
  2. < "method": "thing.service.property.set",
  3. < "id": "161430786",
  4. < "params": {
  5. < "LightSwitch": 1
  6. < },
  7. < "version": "1.0.0"
  8. < }
  9. [dbg] iotx_mc_handle_recv_PUBLISH(1555): Packet Ident : 00000000
  10. [dbg] iotx_mc_handle_recv_PUBLISH(1556): Topic Length : 55
  11. [dbg] iotx_mc_handle_recv_PUBLISH(1560): Topic Name : /sys/a1csED27mp7/AdvExample1/thing/service/property/set
  12. [dbg] iotx_mc_handle_recv_PUBLISH(1563): Payload Len/Room : 113 / 4940
  13. [dbg] iotx_mc_handle_recv_PUBLISH(1564): Receive Buflen : 5000
  14. [dbg] iotx_mc_handle_recv_PUBLISH(1575): delivering msg ...
  15. [dbg] iotx_mc_deliver_message(1291): topic be matched
  16. [inf] dm_msg_proc_thing_service_property_set(93): thing/service/property/set
  17. [dbg] dm_msg_request_parse(142): Current Request Message ID: 161430786
  18. [dbg] dm_msg_request_parse(143): Current Request Message Version: 1.0.0
  19. [dbg] dm_msg_request_parse(144): Current Request Message Method: thing.service.property.set
  20. [dbg] dm_msg_request_parse(145): Current Request Message Params: {"LightSwitch":1}
  21. [dbg] dm_ipc_msg_insert(87): dm msg list size: 0, max size: 50
  22. [inf] dm_msg_response(274): Send URI: /sys/a1csED27mp7/AdvExample1/thing/service/property/set_reply, Payload: {"id":"161430786","code":200,"data":{}}
  23. [dbg] MQTTPublish(319): ALLOC: (100) / [164] @ 0x154f1f0
  24. [inf] MQTTPublish(378): Upstream Topic: '/sys/a1csED27mp7/AdvExample1/thing/service/property/set_reply'
  25. [inf] MQTTPublish(379): Upstream Payload:

The following log is printed when the user_property_set_event_handler sample function receives the attribute-setting request:

  1. user_property_set_event_handler. 160: Property Set Received, Devid: 0, Request: {"LightSwitch":1}

Note: When an actual product receives a property settings, developers should parse the property and process it accordingly instead of simply sending the value back to the IoT platform.

Report an event

Function user_post_event() in the example program is to send events to IoT platform, again, it generates several invalid events and sends them to the IoT platform, you need to remove them and just send events to IoT platform only when an event is triggered:

  1. void user_post_event(void)
  2. {
  3. static int example_index = 0;
  4. int res = 0;
  5. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  6. char *event_id = "Error";
  7. char *event_payload = "NULL";
  8. if (example_index == 0) {
  9. ...
  10. //Report the event to the cloud
  11. res = IOT_Linkkit_TriggerEvent(user_example_ctx->master_devid, event_id, strlen(event_id),
  12. event_payload, strlen(event_payload));
  13. EXAMPLE_TRACE("Post Event Message ID: %d", res);
  14. }

The event reported to the cloud is as follows:

  1. > {
  2. > "id": "1",
  3. > "version": "1.0",
  4. > "params": {
  5. > "ErrorCode": 0
  6. > },
  7. > "method": "thing.event.Error.post"
  8. > }
  9. [inf] dm_client_publish(106): Publish Result: 0
  10. [dbg] alcs_observe_notify(105): payload:{"id":"1","version":"1.0","params":{"ErrorCode":0},"method":"thing.event.Error.post"}
  11. [inf] dm_server_send(76): Send Observe Notify Result 0
  12. [dbg] dm_msg_cache_insert(79): dmc list size: 0
  13. user_post_event. 470: Post Event Message ID: 1
  14. [dbg] iotx_mc_cycle(1774): PUBLISH
  15. [inf] iotx_mc_handle_recv_PUBLISH(1549): Downstream Topic: '/sys/a1csED27mp7/AdvExample1/thing/event/Error/post_reply'
  16. [inf] iotx_mc_handle_recv_PUBLISH(1550): Downstream Payload:

The response received from the cloud is as follows:

  1. < {
  2. < "code": 200,
  3. < "data": {
  4. < },
  5. < "id": "1",
  6. < "message": "success",
  7. < "method": "thing.event.Error.post",
  8. < "version": "1.0"
  9. < }
  10. [dbg] iotx_mc_handle_recv_PUBLISH(1555): Packet Ident : 00000000
  11. [dbg] iotx_mc_handle_recv_PUBLISH(1556): Topic Length : 57
  12. [dbg] iotx_mc_handle_recv_PUBLISH(1560): Topic Name : /sys/a1csED27mp7/AdvExample1/thing/event/Error/post_reply
  13. [dbg] iotx_mc_handle_recv_PUBLISH(1563): Payload Len/Room : 101 / 4938
  14. [dbg] iotx_mc_handle_recv_PUBLISH(1564): Receive Buflen : 5000
  15. [dbg] iotx_mc_handle_recv_PUBLISH(1575): delivering msg ...
  16. [dbg] iotx_mc_deliver_message(1291): topic be matched
  17. [inf] dm_msg_proc_thing_event_post_reply(258): Event Id: Error
  18. [dbg] dm_msg_response_parse(167): Current Request Message ID: 1
  19. [dbg] dm_msg_response_parse(168): Current Request Message Code: 200
  20. [dbg] dm_msg_response_parse(169): Current Request Message Data: {}
  21. [dbg] dm_msg_response_parse(174): Current Request Message Desc: success
  22. [dbg] dm_ipc_msg_insert(87): dm msg list size: 0, max size: 50
  23. [dbg] dm_msg_cache_remove(142): Remove Message ID: 1
  24. [inf] _iotx_linkkit_event_callback(219): Receive Message Type: 31
  25. [inf] _iotx_linkkit_event_callback(221): Receive Message: {"id":1,"code":200,"devid":0,"eventid":"Error","payload":"success"}
  26. [dbg] _iotx_linkkit_event_callback(513): Current Id: 1
  27. [dbg] _iotx_linkkit_event_callback(514): Current Code: 200
  28. [dbg] _iotx_linkkit_event_callback(515): Current Devid: 0
  29. [dbg] _iotx_linkkit_event_callback(516): Current EventID: Error
  30. [dbg] _iotx_linkkit_event_callback(517): Current Message: success

The following log is printed for the user’s callback function user_trigger_event_reply_event_handler:

  1. user_trigger_event_reply_event_handler. 310: Trigger Event Reply Received, Devid: 0, Message ID: 1, Code: 200, EventID: Error, Message: success

Service calling

In the example program, when a service-call-request is received, the following callback function will be invoked:

  1. static int user_service_request_event_handler(const int devid, const char *serviceid, const int serviceid_len,
  2. const char *request, const int request_len,
  3. char **response, int *response_len)
  4. {
  5. int contrastratio = 0, to_cloud = 0;
  6. cJSON *root = NULL, *item_transparency = NULL, *item_from_cloud = NULL;
  7. EXAMPLE_TRACE("Service Request Received, Devid: %d, Service ID: %.*s, Payload: %s", devid, serviceid_len,
  8. serviceid,
  9. request);
  10. /* Parse Root */
  11. root = cJSON_Parse(request);
  12. if (root == NULL || ! cJSON_IsObject(root)) {
  13. EXAMPLE_TRACE("JSON Parse Error");
  14. return -1;
  15. }

You need to process the received service with the Service ID of Custom, assign the value of the service input parameter +1 to the output parameter, and return it to the cloud.

  1. if (strlen("Custom") == serviceid_len && memcmp("Custom", serviceid, serviceid_len) == 0) {
  2. ...
  3. contrastratio = item_transparency->valueint + 1;
  4. ...
  5. HAL_Snprintf(*response, *response_len, response_fmt, contrastratio);
  6. *response_len = strlen(*response);
  7. } else if (strlen("SyncService") == serviceid_len && memcmp("SyncService", serviceid, serviceid_len) == 0) {
  8. ...
  9. ...
  10. }
  11. cJSON_Delete(root);
  12. return 0;
  13. }

At this point, you can see the following log on the device side:

  1. [dbg] iotx_mc_cycle(1774): PUBLISH
  2. [inf] iotx_mc_handle_recv_PUBLISH(1549): Downstream Topic: '/sys/a1csED27mp7/AdvExample1/thing/service/Custom'
  3. [inf] iotx_mc_handle_recv_PUBLISH(1550): Downstream Payload:

The following service-calling request is received from the cloud, with the input parameter transparency:

  1. < {
  2. < "method": "thing.service.Custom",
  3. < "id": "161445548",
  4. < "params": {
  5. < "transparency": 5
  6. < },
  7. < "version": "1.0.0"
  8. < }
  9. [dbg] iotx_mc_handle_recv_PUBLISH(1555): Packet Ident : 00000000
  10. [dbg] iotx_mc_handle_recv_PUBLISH(1556): Topic Length : 49
  11. [dbg] iotx_mc_handle_recv_PUBLISH(1560): Topic Name : /sys/a1csED27mp7/AdvExample1/thing/service/Custom
  12. [dbg] iotx_mc_handle_recv_PUBLISH(1563): Payload Len/Room : 96 / 4946
  13. [dbg] iotx_mc_handle_recv_PUBLISH(1564): Receive Buflen : 5000
  14. [dbg] iotx_mc_handle_recv_PUBLISH(1575): delivering msg ...
  15. [dbg] iotx_mc_deliver_message(1291): topic be matched
  16. [inf] dm_msg_proc_thing_service_request(224): Service Identifier: Custom
  17. [dbg] dm_msg_request_parse(142): Current Request Message ID: 161445548
  18. [dbg] dm_msg_request_parse(143): Current Request Message Version: 1.0.0
  19. [dbg] dm_msg_request_parse(144): Current Request Message Method: thing.service.Custom
  20. [dbg] dm_msg_request_parse(145): Current Request Message Params: {"transparency":5}
  21. [dbg] dm_ipc_msg_insert(87): dm msg list size: 0, max size: 50
  22. [inf] _iotx_linkkit_event_callback(219): Receive Message Type: 18
  23. [inf] _iotx_linkkit_event_callback(221): Receive Message: {"id":"161445548","devid":0,"serviceid":"Custom","payload":{"transparency":5}}
  24. [dbg] _iotx_linkkit_event_callback(300): Current Id: 161445548
  25. [dbg] _iotx_linkkit_event_callback(301): Current Devid: 0
  26. [dbg] _iotx_linkkit_event_callback(302): Current ServiceID: Custom
  27. [dbg] _iotx_linkkit_event_callback(303): Current Payload: {"transparency":5}

The user’s calling functionuser_service_request_event_handler is called:

  1. user_service_request_event_handler. 99: Service Request Received, Devid: 0, Service ID: Custom, Payload: {"transparency":5}
  2. user_service_request_event_handler. 116: transparency: 5
  3. [dbg] iotx_dm_send_service_response(280): Current Service Response Payload, Length: 19, Payload: {"Contrastratio":6}
  4. [dbg] dm_mgr_upstream_thing_service_response(1275): Current Service Name: thing/service/Custom_reply
  5. [inf] dm_msg_response(274): Send URI: /sys/a1csED27mp7/AdvExample1/thing/service/Custom_reply, Payload: {"id":"161445548","code":200,"data":{"Contrastratio":6}}
  6. [dbg] MQTTPublish(319): ALLOC: (111) / [175] @ 0x85a1a0
  7. [inf] MQTTPublish(378): Upstream Topic: '/sys/a1csED27mp7/AdvExample1/thing/service/Custom_reply'
  8. [inf] MQTTPublish(379): Upstream Payload:

In the callback function, add 1 to the value of transparency, assign the value to Contrastratio, and then report it to the cloud:

  1. > {
  2. > "id": "161445548",
  3. > "code": 200,
  4. > "data": {
  5. > "Contrastratio": 6
  6. > }
  7. > }
  8. [inf] dm_client_publish(106): Publish Result: 0
  9. [dbg] alcs_observe_notify(105): payload:{"id":"161445548","code":200,"data":{"Contrastratio":6}}
  10. [inf] dm_server_send(76): Send Observe Notify Result 0

That concludes the descriptions of services, attributes, and events in the advanced version of the single-item routine.