最近有关注MQTT协议,发现各大厂家都有自己的物联网云平台,网上搜索查找到阿里云物联网平台支持设备接入,并提供SDK用户可以通过快速的体验C-SDK感受如何将设备连接到阿里云物联网平台, 并如何将设备的数据发送到平台/以及如何从物联网平台接收数据SDK提供了一系列的编程文档来描述如何使用SDK提供的软件功能请访问此处进行了解
查看官方文档发现编译存在问题,特分享自己的遇到的问题
本文开发环境是自己搭建的ubuntu64位虚拟机,开发环境不在这里做过多描述,阿里的官方文档中说到自己没有环境的话可以到Hacklab Web IDE上开发,这里没弄懂这个Web IDE环境,可以以后自己研究下
Linux vm 4.4.0-31-generic #50~14.04.1-Ubuntu SMP Wed Jul 13 01:06:37 UTC 2016 i686 i686 i686 GNU/Linux
安装必要软件命令
$ sudo apt-get install -y build-essential make git gcc gawk
sdk运行之前需要在物联网云平台创建产品和设备,可以查看下面的链接查看
如何创建支持物模型的产品
通过常见产品->创建设备的步骤来预先在物联网平台创建设备信息,会生成设备标识码
如上图进入设备管理页面,查看设备信息,对应的ProductKey,DeviceName
,DeviceSecret是艺要填入到SDK的代码中的,不然标示不匹配,再在云平台上传查询设备会失败
官方文档里面说明需要关闭宏FEATURE_DEVICE_MODEL_ENABLED,但是关闭宏之后编译失败。我编译时候没有关闭宏,修改了wrappers/os/ubuntu/HAL_OS_linux.c,里面的ProductKey,DeviceName,DeviceSecret
#ifdef DYNAMIC_REGISTER
....
#else
#ifdef DEVICE_MODEL_ENABLED
char _product_key[IOTX_PRODUCT_KEY_LEN + 1] = "xxxxxxx";
char _product_secret[IOTX_PRODUCT_SECRET_LEN + 1] = "fSAF0hle6xL0oRWd";
char _device_name[IOTX_DEVICE_NAME_LEN + 1] = "xxxxxxxxxxx";
char _device_secret[IOTX_DEVICE_SECRET_LEN + 1] = "xxxxxxxxxxxxxxxxx";
#else
修改完成之后执行make命令输出如下打印
SELECTED CONFIGURATION:
....
. [certs]
. [config.bat]
. [external_libs]
. [extract.bat]
. [extract.sh]
. [LICENSE]
. [makefile]
. [make.settings]
. [model.json]
. [output]
. [README.md]
. [src]
. [tools]
| RATE | OBJ NAME | BYTES/TOTAL | MODULE NAME |
|-------|-------------------------------------|----------------|----------------------------|
| 100% | dev_sign_mqtt.o | 1775/1775 | src/dev_sign |
|-------|-------------------------------------|----------------|----------------------------|
| 27.3% | infra_cjson.o | 3584/13113 | src/infra |
| 18.7% | infra_sha256.o | 2459/13113 | src/infra |
| 13.1% | infra_compat.o | 1730/13113 | src/infra |
| 12.3% | infra_report.o | 1613/13113 | src/infra |
| 8.76% | infra_string.o | 1150/13113 | src/infra |
| 8.40% | infra_prt_nwk_payload.o | 1102/13113 | src/infra |
| 3.88% | infra_net.o | 509/13113 | src/infra |
| 3.53% | infra_timer.o | 464/13113 | src/infra |
| 2.78% | infra_defs.o | 365/13113 | src/infra |
| 1.04% | infra_log.o | 137/13113 | src/infra |
|-------|-------------------------------------|----------------|----------------------------|
| 18.7% | dm_manager.o | 7397/39446 | src/dev_model |
| 16.8% | dm_message.o | 6643/39446 | src/dev_model |
| 13.7% | impl_linkkit.o | 5411/39446 | src/dev_model |
| 9.38% | dm_msg_process.o | 3702/39446 | src/dev_model |
| 8.17% | dm_utils.o | 3223/39446 | src/dev_model |
| 7.95% | dm_api.o | 3139/39446 | src/dev_model |
| 5.21% | dm_client.o | 2057/39446 | src/dev_model |
| 4.84% | dm_log_report.o | 1912/39446 | src/dev_model |
| 4.27% | iotx_cm_mqtt.o | 1685/39446 | src/dev_model |
| 3.95% | iotx_cm.o | 1562/39446 | src/dev_model |
| 3.03% | dm_message_cache.o | 1199/39446 | src/dev_model |
| 1.78% | dm_ipc.o | 705/39446 | src/dev_model |
| 1.53% | dm_client_adapter.o | 606/39446 | src/dev_model |
| 0.51% | dm_opt.o | 205/39446 | src/dev_model |
|-------|-------------------------------------|----------------|----------------------------|
| 62.0% | iotx_mqtt_client.o | 11580/18648 | src/mqtt |
| 13.9% | mqtt_api.o | 2605/18648 | src/mqtt |
| 6.86% | MQTTPacket.o | 1281/18648 | src/mqtt |
| 6.29% | MQTTConnectClient.o | 1173/18648 | src/mqtt |
| 3.25% | MQTTSubscribeClient.o | 607/18648 | src/mqtt |
| 2.90% | MQTTSerializePublish.o | 542/18648 | src/mqtt |
| 2.44% | MQTTUnsubscribeClient.o | 456/18648 | src/mqtt |
| 2.16% | MQTTDeserializePublish.o | 404/18648 | src/mqtt |
|-------|-------------------------------------|----------------|----------------------------|
| 20.8% | ssl_tls.o | 29324/140365 | external_libs/mbedtls |
| 11.1% | bignum.o | 15655/140365 | external_libs/mbedtls |
| 8.55% | error.o | 12006/140365 | external_libs/mbedtls |
| 8.15% | x509_crt.o | 11447/140365 | external_libs/mbedtls |
| 7.68% | ssl_cli.o | 10781/140365 | external_libs/mbedtls |
| 7.31% | rsa.o | 10271/140365 | external_libs/mbedtls |
| 4.28% | sha1.o | 6010/140365 | external_libs/mbedtls |
| 3.81% | aes.o | 5353/140365 | external_libs/mbedtls |
| 3.58% | sha256.o | 5037/140365 | external_libs/mbedtls |
| 3.02% | oid.o | 4241/140365 | external_libs/mbedtls |
| 1.98% | x509.o | 2788/140365 | external_libs/mbedtls |
| 1.95% | cipher.o | 2739/140365 | external_libs/mbedtls |
| 1.87% | debug.o | 2629/140365 | external_libs/mbedtls |
| 1.59% | net_sockets.o | 2240/140365 | external_libs/mbedtls |
| 1.54% | pkparse.o | 2175/140365 | external_libs/mbedtls |
| 1.54% | md.o | 2174/140365 | external_libs/mbedtls |
| 1.44% | ctr_drbg.o | 2035/140365 | external_libs/mbedtls |
| 1.43% | ssl_ciphersuites.o | 2009/140365 | external_libs/mbedtls |
| 1.32% | asn1parse.o | 1855/140365 | external_libs/mbedtls |
| 1.31% | pk.o | 1843/140365 | external_libs/mbedtls |
| 0.84% | entropy.o | 1182/140365 | external_libs/mbedtls |
| 0.76% | ssl_cookie.o | 1077/140365 | external_libs/mbedtls |
| 0.76% | md_wrap.o | 1068/140365 | external_libs/mbedtls |
| 0.71% | base64.o | 1002/140365 | external_libs/mbedtls |
| 0.70% | pk_wrap.o | 992/140365 | external_libs/mbedtls |
| 0.63% | cipher_wrap.o | 893/140365 | external_libs/mbedtls |
| 0.43% | pem.o | 617/140365 | external_libs/mbedtls |
| 0.40% | timing.o | 570/140365 | external_libs/mbedtls |
| 0.19% | entropy_poll.o | 273/140365 | external_libs/mbedtls |
| 0.05% | platform.o | 79/140365 | external_libs/mbedtls |
|-------|-------------------------------------|----------------|----------------------------|
| 69.3% | HAL_OS_linux.o | 4884/7041 | wrappers |
| 30.6% | HAL_TCP_linux.o | 2157/7041 | wrappers |
| RATE | MODULE NAME | ROM | RAM | BSS | DATA |
|-------|-------------------------------------|-----------|-----------|------------|--------|
| 54.0% | src/dev_model | 39474 | 216 | 188 | 28 |
| 25.5% | src/mqtt | 18648 | 28 | 28 | 0 |
| 18.2% | src/infra | 13301 | 252 | 64 | 188 |
| 2.50% | src/dev_sign | 1831 | 56 | 0 | 56 |
|-------|-------------------------------------|-----------|-----------|------------|--------|
| 100% | - IN TOTAL - | 73254 | 552 | 280 | 272 |
在sdk的output/release/bin/下执行./mqtt-example
main|125 :: mqtt example
establish tcp connection with server(host='a1z6TfYBkwo.iot-as-mqtt.cn-shanghai.aliyuncs.com', port=[1883])
success to establish tcp, fd=3
> {
> "id": "0",
> "version": "1.0",
> "params": [
> {
> "attrKey": "SYS_LP_SDK_VERSION",
> "attrValue": "3.0.1",
> "domain": "SYSTEM"
> },
> {
> "attrKey": "SYS_SDK_LANGUAGE",
> "attrValue": "C",
> "domain": "SYSTEM"
> }
> ],
> "method": "thing.deviceinfo.update"
> }
> {
> "id": "1",
> "params": {
> "version": "app-1.0.0-20180101.1000"
> }
> }
> {
> "message": "hello!"
> }
example_event_handle|098 :: msg->event_type : 9
example_event_handle|098 :: msg->event_type : 3
example_event_handle|098 :: msg->event_type : 9
> {
> "message": "hello!"
> }
> {
> "message": "hello!"
> }