1.2.3 快速体验Link C SDK
更新时间:2019-03-01 17:34:43
简介
这个例子中我们将使用Hacklab完成Link C SDK快速上手的例子。
阿里云IoT提供了将设备接入云端需要的Linkkit C SDK,以便于非AliOS-Things设备能够快速接入阿里云。关于LinkKit SDK的介绍,请参考使用Link Kit SDK接入。
为了帮助开发者快速使用Linkkit C SDK,Hacklab Web IDE已经提供了Linkkit C SDK中快速体验和基于Make的交叉编译示例需要的开发环境和开发工具,使用Hacklab无需任何安装配置工作,就能让你快速体验使用Linkkit C SDK快速上云。
准备工作
一个阿里云账号,新用户可以在阿里云免费注册。
一台装有浏览器的开发PC,运行本例子不需要额外的硬件设备,推荐使用Chrome浏览器。
例子:设备快速上云
接下来让我们使用Hacklab编译运行体验基础版例子。该程序在Linux环境中编译出一个通过MQTT协议连接云端的可执行程序,让开发者了解如何使用Link C SDK连接到阿里云物联网平台。首先登陆Hacklab Web IDE,进入workspace,打开对应的terminal.
编译运行基础版例程
打开例子代码,填写设备三元组到例程中
可以使用IDE命令行中的wget
命令下载需要的SDK版本,如下面的命令下载Link C SDK 2.3.0并解压到workspace中:
hacklab@iot:~/workspace$ wget https://github.com/aliyun/iotkit-embedded/archive/v2.3.0.zip
hacklab@iot:~/workspace$ unzip v2.3.0.zip
解压后会看到对应的Link C SDK文件,从IDE左侧的文件树上打开文件iotkit-embedded-2.3.0/examples/mqtt/mqtt-example.c
,如下图所示:
用上一步中自己的设备三元组替换掉代码中测试三元组信息:
#if defined(SUPPORT_ITLS)
...
...
#else
#if defined(ON_DAILY)
...
...
#else
#define PRODUCT_KEY "a1ExpAkj9Hi"
#define DEVICE_NAME "Example1"
#define DEVICE_SECRET "cNzcn2Lbqzh4UiXKLwW77hxI9GFmcRgb"
#endif
#endif
编译基础版例程
在IDE terminal中输入以下命令编译出设备端的可执行程序,编译成功完成后, 生成的样例程序在当前路径的 output/release/bin 目录下:
$ cd iotkit-embedded-2.3.0
$ make distclean
$ make
编译完成后,输出output/release/bin/mqtt-example
:
运行基础版例程
$ cd iotkit-embedded-2.3.0
$ ./output/release/bin/mqtt-example
[inf] iotx_device_info_init(39): device_info created successfully!
[dbg] iotx_device_info_set(49): start to set device info!
[dbg] iotx_device_info_set(63): device_info set successfully!
[inf] guider_print_dev_guider_info(279): ....................................................
[inf] guider_print_dev_guider_info(280): ProductKey : a1ExpAkj9Hi
[inf] guider_print_dev_guider_info(281): DeviceName : Example1
[inf] guider_print_dev_guider_info(282): DeviceID : a1ExpAkj9Hi.Example1
[inf] guider_print_dev_guider_info(284): ....................................................
[inf] guider_print_dev_guider_info(285): PartnerID Buf : ,partner_id=example.demo.partner-id
[inf] guider_print_dev_guider_info(286): ModuleID Buf : ,module_id=example.demo.module-id
[inf] guider_print_dev_guider_info(287): Guider URL :
[inf] guider_print_dev_guider_info(289): Guider SecMode : 2 (TLS + Direct)
[inf] guider_print_dev_guider_info(291): Guider Timestamp : 2524608000000
[inf] guider_print_dev_guider_info(292): ....................................................
[inf] guider_print_dev_guider_info(298): ....................................................
[inf] guider_print_conn_info(256): -----------------------------------------
[inf] guider_print_conn_info(257): Host : a1ExpAkj9Hi.iot-as-mqtt.cn-shanghai.aliyuncs.com
[inf] guider_print_conn_info(258): Port : 1883
[inf] guider_print_conn_info(261): ClientID : a1ExpAkj9Hi.Example1|securemode=2,timestamp=2524608000000,signmethod=hmacsha1 ...
[inf] guider_print_conn_info(263): TLS PubKey : 0x437636 ('-----BEGIN CERTI ...')
[inf] guider_print_conn_info(266): -----------------------------------------
[inf] IOT_MQTT_Construct(3005): CONFIG_MQTT_SUBTOPIC_MAXNUM : 65535
[dbg] IOT_MQTT_Construct(3007): sizeof(iotx_mc_client_t) = 1573144!
[inf] iotx_mc_init(2098): MQTT init success!
[inf] _ssl_client_init(142): Loading the CA root certificate ...
[inf] _ssl_client_init(149): ok (0 skipped)
[inf] _TLSConnectNetwork(315): Connecting to /a1ExpAkj9Hi.iot-as-mqtt.cn-shanghai.aliyuncs.com/1883...
[inf] mbedtls_net_connect_timeout(257): setsockopt SO_SNDTIMEO timeout: 10s
[inf] mbedtls_net_connect_timeout(260): connecting IP_ADDRESS: 106.15.100.2
[inf] _TLSConnectNetwork(328): ok
[inf] _TLSConnectNetwork(333): . Setting up the SSL/TLS structure...
[inf] _TLSConnectNetwork(343): ok
[inf] _TLSConnectNetwork(382): Performing the SSL/TLS handshake...
[inf] _TLSConnectNetwork(390): ok
[inf] _TLSConnectNetwork(394): . Verifying peer X.509 certificate..
[inf] _real_confirm(90): certificate verification result: 0x00
[wrn] MQTTConnect(204): NOT USING pre-malloced buf 0xc4a010, malloc per packet
[dbg] MQTTConnect(204): ALLOC: curr buf = 0xc56890, curr buf_size = 320, required payload_len = 256
[dbg] MQTTConnect(224): FREED: curr buf = (nil), curr buf_size = 0
[inf] iotx_mc_connect(2449): mqtt connect success!
...
...
mqtt_client|309 :: packet-id=7, publish topic msg={"attr_name":"temperature", "attr_value":"1"}
[dbg] iotx_mc_cycle(1591): PUBACK
event_handle|132 :: publish success, packet-id=7
[dbg] iotx_mc_cycle(1608): PUBLISH
[dbg] iotx_mc_handle_recv_PUBLISH(1412): Packet Ident : 00035641
[dbg] iotx_mc_handle_recv_PUBLISH(1413): Topic Length : 26
[dbg] iotx_mc_handle_recv_PUBLISH(1417): Topic Name : /a1ExpAkj9Hi/Example1/data
[dbg] iotx_mc_handle_recv_PUBLISH(1420): Payload Len/Room : 45 / 992
[dbg] iotx_mc_handle_recv_PUBLISH(1421): Receive Buflen : 1024
[dbg] iotx_mc_handle_recv_PUBLISH(1432): delivering msg ...
[dbg] iotx_mc_deliver_message(1170): topic be matched
_demo_message_arrive|166 :: ----
_demo_message_arrive|167 :: packetId: 35641
_demo_message_arrive|171 :: Topic: '/a1ExpAkj9Hi/Example1/data' (Length: 26)
_demo_message_arrive|175 :: Payload: '{"attr_name":"temperature", "attr_value":"1"}' (Length: 45)
_demo_message_arrive|176 :: ----
...
...
main|361 :: out of sample!