Alibaba Cloud IoT Platform provides a device SDK to establish communication between a device and the platform. This topic describes how to use the fota_posix_demo.c sample program to simulate a device connection and an OTA update.
Important notes
The steps in this topic are performed as a regular user. If an operation requires administrative permissions, prefix the command with sudo.
Prerequisites
Create a product and a device, and obtain the device certificate (ProductKey, DeviceName, and DeviceSecret). For more information, see Create a product and Create a device.
Create a device named SDevice1.
Prepare the development environment
-
The device-side C SDK runs on Linux. Compile it in a 64-bit Ubuntu 16.04 environment.
-
Required software:
make 4.1 or later, gcc 5.4.0 or later.
Install command:
sudo apt-get install -y build-essential make gcc
Procedure
-
Obtain the C device SDK: C SDK 4.x.
-
Unzip the C device SDK package and modify the device connection information in the
/LinkSDK/demos/fota_posix_demo.cfile.Use the device information for SDevice1.
char *product_key = "g18***"; char *device_name = "SDevice1"; char *device_secret = "cefbebf00***"; ... ... char *url = "iot-***.mqtt.iothub.aliyuncs.com";Parameter
Example
Description
url
iot-***.mqtt.iothub.aliyuncs.comThe device connection endpoint.
-
New public instances: On the Instance Details page, click View Development Configurations. The endpoint appears on the Development Configurations panel.
-
Old public instances: The endpoint format is
${YourProductKey}.iot-as-mqtt.${YourRegionId}.aliyuncs.com.
product_key
g18***The device certificate information. For more information, see Obtain a device certificate.
This sample uses the unique-certificate-per-device authentication method.
device_name
SDevice1device_secret
cefbebf00***The
fota_posix_demo.cfile provides sample code for an OTA update. The device reports its version as1.0.0before the update. In a production environment, you must obtain the actual version number from the device configuration area and implement logic to report it. For more information, see Description of the sample code.cur_version = "1.0.0"; res = aiot_ota_report_version(ota_handle, cur_version); if (res < STATE_SUCCESS) { printf("aiot_ota_report_version failed: -0x%04X\r\n", -res); } -
-
On the Linux virtual machine, install the required software.
sudo apt-get install -y build-essential make gcc -
Upload the modified
LinkSDKpackage to your Linux development environment. -
In the SDK root directory
/LinkSDK, run themakecommand to build the sample program.make clean makeThe sample program,
fota-posix-demo, is generated in the./outputdirectory. -
Run the sample program:
./output/fota-posix-demo -
View the device runtime logs and status.
-
The following log shows the device connection information and the reported version number.
:/opt/LinkSDK# ./output/fota-posix-demo [1630993401.977][LK-0313] MQTT user calls aiot_mqtt_connect api, connect [1630993401.977][LK-0317] SDevice1&g18xxx [1630993401.977][LK-0318] 348xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxC core_sysdep_network_establish host iot-xxx.mqtt.iothub.aliyuncs.com port 443, type 0 establish tcp connection with server(host='iot-xxx.mqtt.iothub.aliyuncs.com', port=[443]) success to establish tcp, fd=3 local port: 32776 [1630993401.988][LK-1000] establish mbedtls connection with server(host='iot-xxx mqtt.iothub.aliyuncs.com', port=[443]) [1630993401.999][LK-1000] success to establish mbedtls connection, (cost 45247 bytes in total, max used 47983 bytes) [1630993401.999][LK-0319] xxx.SDevice1|timestamp=2524608000000,_ss=1,_v=sdk-c-4.1.0,securemode=2,signmethod=hmacsha256,ext=3,_conn-tls_11| [1630993402.066][LK-0313] MQTT connect success in 86 ms AIOT_MQTTEVT_CONNECT [1630993402.066][LK-0309] pub: /ota/device/inform/g18xxx/SDevice1 [LK-030A] > 7B 22 69 64 22 3A 31 2C 20 22 70 61 72 61 6D 73 {"id":1, "params [LK-030A] > 22 3A 7B 22 76 65 72 73 69 6F 6E 22 3A 22 31 2E ":{"version":"1. [LK-030A] > 30 2E 30 22 7D 7D 0.0"}} -
Return to the IoT Platform console for your instance. In the left-side navigation pane, navigate to . Find the target device and check its status. A status of Online indicates that the device is connected to IoT Platform.
-
Next steps
Push an OTA update package to a device: IoT Platform sends an update task to the device. The online device receives the update information and performs the OTA update.