MQTT 协议连接阿里云物联网服务器
一、阿里云账号注册
https://www.aliyun.com/?utm_content=se_1000301881
![](https://static001.geekbang.org/infoq/aa/aadc774d31218fa637d9bb8babdbead4.png)
![](https://static001.geekbang.org/infoq/c2/c252037507f97da4334c3e5e966fedf6.png)
![](https://static001.geekbang.org/infoq/92/9218bfc3b37adffe097b04d2f65f6139.png)
![](https://static001.geekbang.org/infoq/4c/4cea68c7485251da6b2d77ab080d903f.png)
![](https://static001.geekbang.org/infoq/b7/b707078b402b95d0de65990ce4edd403.png)
![](https://static001.geekbang.org/infoq/dc/dc2dc8f2e535138861e0170279eb8d03.png)
![](https://static001.geekbang.org/infoq/55/55579c515a69a31b9da679948abf4992.png)
![](https://static001.geekbang.org/infoq/33/3359ea1a1515c3e26943c689ddfa0634.png)
![](https://static001.geekbang.org/infoq/b2/b28b9e4ca9ef5130bcddc7c44c6dbd08.png)
![](https://static001.geekbang.org/infoq/c8/c893a839f51c56c0e083f6c71f320289.png)
![](https://static001.geekbang.org/infoq/99/996f35955d26143e44d1a0b53b284f46.png)
![](https://static001.geekbang.org/infoq/6a/6a62e51d7c3930f24f596045dcd1bc2c.png)
![](https://static001.geekbang.org/infoq/a7/a7b519efc9458ddd72b2f1e3e62c08a0.png)
二、阿里云 IOT 物联网平台介绍
![](https://static001.geekbang.org/infoq/08/0845a28973e545748c0c7cec745a5f31.png)
![](https://static001.geekbang.org/infoq/14/1454c95146c1aec43e0c655ad0bf1ea8.png)
![](https://static001.geekbang.org/infoq/d5/d5c8455493f960f409019ce00b0324ea.png)
https://edu.aliyun.com/course/explore/aliyun_iot?spm=a2c56.193971.0.0.705425c8wNdCII
![](https://static001.geekbang.org/infoq/8a/8a70565eeee20b8109b0db611a3451f4.png)
三、新建物联网产品与设备
https://edu.aliyun.com/course/explore/aliyun_iot?spm=a2c56.193971.0.0.7d2625c88z4kXP
![](https://static001.geekbang.org/infoq/a5/a5db67efdcca4d44d01c9ab7c570d594.png)
https://www.aliyun.com/product/iot-deviceconnect?spm=5176.10731491.1280361.402.w5ke4R
![](https://static001.geekbang.org/infoq/c4/c4b971b8b96ba175e6df495b0d6afe04.png)
![](https://static001.geekbang.org/infoq/fa/fa291ea0cea2fa13ccccdb118b67a3fd.png)
![](https://static001.geekbang.org/infoq/67/67ebed6da6cf8e072c3e9d8fe0a7faf0.png)
![](https://static001.geekbang.org/infoq/80/80924ee9a5904b28847bca96d2748e9a.png)
![](https://static001.geekbang.org/infoq/ed/ed4610be6ef78048ff7a078a4a5bf389.png)
![](https://static001.geekbang.org/infoq/e0/e04aef9abf4e15cd481aaabc9571366b.png)
![](https://static001.geekbang.org/infoq/b9/b9efa8c83fa85f37f21023826572fd7e.png)
![](https://static001.geekbang.org/infoq/e4/e493f0dc31b5b3a599f19571f1fa311f.png)
![](https://static001.geekbang.org/infoq/2a/2a529bb0e2e2e9a8c21e05c9d6738acc.png)
![](https://static001.geekbang.org/infoq/53/5306239168eafc80ac1efbe90904aad1.png)
![](https://static001.geekbang.org/infoq/7d/7dc771c42c2fd9b5c79c5bb4512413d7.png)
![](https://static001.geekbang.org/infoq/02/02a574207aa5ec1936fe97efa2931e93.png)
![](https://static001.geekbang.org/infoq/6c/6c3cbe307c4e7bc98b82d9bec0ac54d0.png)
![](https://static001.geekbang.org/infoq/f7/f74c93fca23595bca36bf0784f59f2fb.png)
https://iot.console.aliyun.com/studio
![](https://static001.geekbang.org/infoq/77/779efa870b462301bb03d54834ef4e1d.png)
![](https://static001.geekbang.org/infoq/dc/dc2d9735da35dd2432fd3f2e96d84545.png)
![](https://static001.geekbang.org/infoq/0e/0e91b0e87c4f685253a6e8bba26e3a5e.png)
![](https://static001.geekbang.org/infoq/4d/4d2fac2362cf54a993739afd7d0fa621.png)
![](https://static001.geekbang.org/infoq/ed/ed97d40d709888e73171bea6327d825a.png)
![](https://static001.geekbang.org/infoq/be/be0ec1f5232d43677604d1d46805ed86.png)
![](https://static001.geekbang.org/infoq/aa/aa7b4a07de44d260083da2d7994d28ea.png)
![](https://static001.geekbang.org/infoq/0e/0e40daea05444b09fb9cf72c0a6c0186.png)
![](https://static001.geekbang.org/infoq/9e/9ec4c844d9095c0aec25305586921cd4.png)
![](https://static001.geekbang.org/infoq/c1/c1038d5fdef3fd2b65287f2b28bc7a1a.png)
![](https://static001.geekbang.org/infoq/05/05dd977242a9323d7920d2ef311e58bd.png)
![](https://static001.geekbang.org/infoq/88/887042f2d84bf466944e21676fcce7fb.png)
![](https://static001.geekbang.org/infoq/68/687243dbe1a3ec3504bcd39f98fbb7ad.png)
![](https://static001.geekbang.org/infoq/56/56e15ebb3990a8bb6bcc9e743e961675.png)
![](https://static001.geekbang.org/infoq/85/85aff8a54ec4c18e8d5ab88e8c2945dc.png)
![](https://static001.geekbang.org/infoq/07/07a36311389fb7965496685dc0f13206.png)
移动 APP 界面设计:
![](https://static001.geekbang.org/infoq/51/519c9b46b21e0a283821d934499bd268.png)
![](https://static001.geekbang.org/infoq/6c/6cc303708159592be4f546ebf2c256d6.png)
![](https://static001.geekbang.org/infoq/98/987f317d9f5f61941512de64d5ed1665.png)
![](https://static001.geekbang.org/infoq/22/227478cee02ad3b33ccd70e12e312e77.png)
![](https://static001.geekbang.org/infoq/5a/5ae3053350c579179e3a997c8e9265e4.png)
我这里选择构建 Android 应用
![](https://static001.geekbang.org/infoq/37/374dc773ec823ffbf6cfb896e882a2a1.png)
![](https://static001.geekbang.org/infoq/09/09906e3151de7e196b145dc341c0bcb4.png)
![](https://static001.geekbang.org/infoq/ff/ff1e1db4ea5fde82f671777419d701c6.png)
![](https://static001.geekbang.org/infoq/e5/e576ff7887adb1b633dd8cd7af7b49cc.png)
![](https://static001.geekbang.org/infoq/ba/baa3587d2036c046d8a885a42dfd46e4.png)
![](https://static001.geekbang.org/infoq/19/19e13310b260771c3f1c9d8b69e3a67e.png)
![](https://static001.geekbang.org/infoq/6a/6a0e3a4becf7e8be99e83bf013dc32f2.png)
![](https://static001.geekbang.org/infoq/19/19b3c6e915ae79cea21ba88a1382b9a8.png)
![](https://static001.geekbang.org/infoq/4d/4d5267a325b6907ecded28947a8ba789.png)
![](https://static001.geekbang.org/infoq/49/49de0f286fda3b19b0477acced554002.png)
![](https://static001.geekbang.org/infoq/b0/b0a50db561867e62233307a5f7a03104.png)
![](https://static001.geekbang.org/infoq/a2/a2c8f59d5621be78f9632bddb72f8b63.png)
![](https://static001.geekbang.org/infoq/f3/f3ff323e7f9a2324618bad3e4faec579.png)
四、采用 Linux 系统模拟硬件接入云端
![](https://static001.geekbang.org/infoq/84/8498e8dee8c3df8e77daa4236a2f9ff3.png)
![](https://static001.geekbang.org/infoq/a6/a62458b8f92d280bc7e08d401e2acb69.png)
![](https://static001.geekbang.org/infoq/71/71cd0f0f5c46b8291f2417fe4985d19a.png)
https://iot.console.aliyun.com/devices
![](https://static001.geekbang.org/infoq/b5/b5a21a489e94015c7268ef7f20f48b02.png)
https://help.aliyun.com/document_detail/73708.html?spm=a2c4g.11186623.4.1.48f94c07KJBxYv
阿里云物联网平台提供设备端 SDK,设备使用 SDK 与平台建立通信。在这一步里,我们使用平台提供的样例程序 linkkit-example-solo 模拟设备开发,实现设备与物联网平台的通信。
背景信息本文使用 Linux 下的 C 语言 SDK,该 SDK 的编译环境推荐使用 64 位的 Ubuntu16.04。
SDK 的开发编译环境会用到以下软件: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
可以使用如下命令行安装:
操作步骤
登录 Linux 虚拟机。
下载 2.3.0 版本 C 语言 SDK。
使用 unzip 命令解压压缩包。
打开样例程序 linkkit_example_solo.c。
将设备证书信息修改为前一步中创建的设备证书,完成后保存退出。
如下所示,在此处填入 ProductKey、DeviceName 和 DeviceSecret,将创建设备时生成的证书进行身份认证并连接物联网平台。
![](https://static001.geekbang.org/infoq/ad/ad0e97c333eddc862653a743b6cd7a43.png)
在 SDK 顶层目录,执行 make 命令,完成样例程序的编译。
生成的样例程序 linkkit-example-solo 存放在 output/release/bin 目录下。
运行样例程序。在控制台观察到设备上线。此时,设备与物联网平台成功连接。
设备上线成功后,会自动向物联网平台上报消息。你可以通过查看日志,获取具体内容。
![](https://static001.geekbang.org/infoq/45/45c3467c894852b86cf6f333e6117817.png)
![](https://static001.geekbang.org/infoq/43/43244527bf37cc8aceaee997d8e970b8.png)
![](https://static001.geekbang.org/infoq/2c/2c12afeb235c2eff4845020a777edb4e.png)
![](https://static001.geekbang.org/infoq/af/af823e5cf8f05025ccc2d7296125bd1f.png)
详细教程: https://help.aliyun.com/document_detail/96624.html
![](https://static001.geekbang.org/infoq/64/648722c0c7820c17bf8541c64d1af7c3.png)
![](https://static001.geekbang.org/infoq/63/634642f26f1efc56d9adeca6ba2c7ddf.png)
![](https://static001.geekbang.org/infoq/cd/cd92ce7ff1e15a5022f5c4241ba7e106.png)
![](https://static001.geekbang.org/infoq/40/40035ed1dbebfa9ac2d1eed2842e3d87.png)
![](https://static001.geekbang.org/infoq/d6/d6e8c64d52c288c7085cd0ff11d199f0.png)
五、什么是 Topic ?
什么是 Topic?物联网平台中,服务端和设备端通过 Topic 来实现消息通信。Topic 是针对设备的概念,Topic 类是针对产品的概念。产品的 Topic 类会自动映射到产品下的所有设备中,生成用于消息通信的具体设备 Topic。
什么是 Topic 类?为了方便海量设备基于海量 Topic 进行通信,简化授权操作,物联网平台增加了产品 Topic 类的概念。Topic 类是一类 Topic 的集合。例如,高级版产品的自定义 Topic 类/${YourProductKey}/${YourDeviceName}/user/update 是具体 Topic/${YourProductKey}/device1/user/update 和/${YourProductKey}/device2/user/update 的集合。
你创建产品后,物联网平台会为该产品创建系统 Topic 类。你还可以根据业务需求,自定义 Topic 类。基础版产品和高级版产品均支持自定义 Topic 类。
在产品的 Topic 类列表页,创建自定义 Topic 类。
其他功能用到的 Topic 类,如固件升级等,请参考具体功能文档中的 Topic 相关章节进行创建。
你可以在产品详情页的 Topic 类列表页,查看该产品的所有 Topic 类。
在你创建设备后,产品 Topic 类会自动映射到设备上。你无需单独为每个设备授权 Topic。
![](https://static001.geekbang.org/infoq/1b/1bbf5664d5107e50ac99d117b254c9a5.png)
![](https://static001.geekbang.org/infoq/f7/f7458b317e7c54862304c10b2342180a.png)
六、发布产品
https://iot.console.aliyun.com/product/productDetail/a1liX1d3E0j
![](https://static001.geekbang.org/infoq/2e/2e45f73b24a54c469dc310e7bacf0c09.png)
![](https://static001.geekbang.org/infoq/ee/ee3e446628e1c8ddcc45f67a37bbd382.png)
![](https://static001.geekbang.org/infoq/ec/ecdeb9e41db07e28d28c38232117e591.png)
![](https://static001.geekbang.org/infoq/ea/ea7b36a14045c7e9f8d3d3b5ffcb5843.png)
![](https://static001.geekbang.org/infoq/cc/cce8e1dec3e5347613dcd8263abd46c4.png)
![](https://static001.geekbang.org/infoq/7b/7b8576d56cf09ab59e179a7ae784115f.png)
![](https://static001.geekbang.org/infoq/32/32096c95be10ea8efd9e7ac3cefc68f8.png)
七、自定义设备功能
![](https://static001.geekbang.org/infoq/55/55bec81b4c210f407d4444e43484de73.png)
![](https://static001.geekbang.org/infoq/94/94e74090b3035d833c122848eaaa02df.png)
![](https://static001.geekbang.org/infoq/5c/5ce1f7f29f9338ddce5c1c013732a624.png)
![](https://static001.geekbang.org/infoq/22/22ec1df4d392211d7874141199f5c4c1.png)
https://iot.console.aliyun.com/product/productDetail/a1liX1d3E0j
![](https://static001.geekbang.org/infoq/e3/e3121de18672d5d32909cd2c21f78485.png)
![](https://static001.geekbang.org/infoq/a1/a11487f63c13aa96f4071f3b29d0eb0e.png)
![](https://static001.geekbang.org/infoq/77/77fc39d3a1dc1166488e1481fbf07470.png)
![](https://static001.geekbang.org/infoq/1f/1ff05afc0de16617c0989be61b1aefd8.png)
![](https://static001.geekbang.org/infoq/09/092ea4fdea26b46241619b793679c25e.png)
![](https://static001.geekbang.org/infoq/0f/0fd5959eb59a4883e331554585ec7c19.png)
![](https://static001.geekbang.org/infoq/27/2744676cc424ba245b08e3f495c589c7.png)
![](https://static001.geekbang.org/infoq/d3/d3ba6fd81a14e5858f860dc95206111b.png)
配置好了之后保存,发布。
![](https://static001.geekbang.org/infoq/1a/1a94014ea74478388f0906fe5d80736d.png)
然后跳转到以下页面,预览界面。
![](https://static001.geekbang.org/infoq/a9/a9b144cd0c2a3d9a7e7f6fd8729caaba.png)
下面是预览的界面效果图:
![](https://static001.geekbang.org/infoq/82/824d3d5184662831095c8dabba07130e.png)
同时在新建一个窗口,开启在线调试功能:验证开关按钮是否生效。
![](https://static001.geekbang.org/infoq/3b/3b81a52c7197eccec42c77b9379f57aa.png)
![](https://static001.geekbang.org/infoq/ed/ed300052737a9bb7a76e37f1a2a43c5c.png)
![](https://static001.geekbang.org/infoq/aa/aa65180ea89e5b4f3d6dfe36fa37c5a2.png)
调试界面检测到云端下发的数据:
![](https://static001.geekbang.org/infoq/1b/1b7339b4999df6cf0df9258f880fff7b.png)
云端下发的数据里包含了开关的状态值:
![](https://static001.geekbang.org/infoq/fc/fce58acb03acba35d8b55577ac942412.png)
![](https://static001.geekbang.org/infoq/5f/5f5925217fc392cdc567c9c5bbf4fe93.png)
![](https://static001.geekbang.org/infoq/14/142cf8338b2239ebaaf42a3af231983a.png)
八、修改 SDK 模拟动态温度
![](https://static001.geekbang.org/infoq/12/1236c8ecad3bd781489393f82863ab22.png)
运行程序,上线设备 ------要保证 Linux 系统可以正常上网。
![](https://static001.geekbang.org/infoq/34/34e9b16faef650417d639882b1c8d74f.png)
![](https://static001.geekbang.org/infoq/a5/a5c848fdf8648a60fc482b7a34bda5a6.png)
九、SDK 的移植
https://help.aliyun.com/document_detail/97557.html?spm=a2c4g.11186623.6.552.5c7539adCky6ro
![](https://static001.geekbang.org/infoq/6d/6d81e3121d1f06602b25ddfdaa04eba6.png)
![](https://static001.geekbang.org/infoq/b9/b9e42d4b73ad081bd441a11f58193f94.png)
![](https://static001.geekbang.org/infoq/52/52afc712b28f6bcecde8af766b560989.png)
十、MCU 上集成 SDK
https://help.aliyun.com/document_detail/97921.html?spm=a2c4g.11186623.4.4.69432c22rWIem6
![](https://static001.geekbang.org/infoq/d0/d0384726cc3af647b9bdccc3159f59d1.png)
![](https://static001.geekbang.org/infoq/04/04e185910badcb39b0072b61da6a309d.png)
![](https://static001.geekbang.org/infoq/98/98daa976fd77b7f96a685e725ba7d056.png)
https://help.aliyun.com/document_detail/96623.html?spm=a2c4g.11186623.4.3.34953b10La7MGL
![](https://static001.geekbang.org/infoq/ce/ce95696f75959ef60505237c642b0882.png)
基于 STM32 的阿里云连接方案开发包,已经集成了 Ali 物联网套件 SDK。
EWARM IAR 开发环境。
串口工具 Tera Term。串口调试工具,显示程序运行信息,以及供用户配置 WiFi 用户名和密码。
基于 STM32 的阿里云物联网套件方案开发包已经集成了 Ali 物联网套件 SDK2.03 。
https://github.com/aliyun/iotkit-embedded/releases/tag/RELEASED_V2.03
在集成过程中,整个 SDK 目录文件结构保持不变,方便用户在将来自行替换更高版本的 Ali 物联网套件,如下图:
STM32 NUCLEO-L476RG
从 www.st.com 搜索关键字 NUCLEO-L476RG 可以得到此开发板的详细介绍。
板载 STM32 超低功耗芯片 STM32L476RG,拥有 1M 字节的 Flash 和 128K 字节的 SRAM。
板载 Arduino™ UnoV3 接口的扩展插座。我们通过该插座给 WiFi 模组供电。
板载 ST morpho 接口的扩展插座,可由此访问全部 STM32 管脚。我们将通过该插座连接 WiFi 模组的收、发 I/O。
版权声明: 本文为 InfoQ 作者【DS小龙哥】的原创文章。
原文链接:【http://xie.infoq.cn/article/3c0233855edcc9fa5eff4b485】。文章转载请联系作者。
评论