1.1.2. Using Paho MQTT Java Client 15
Total Page:16
File Type:pdf, Size:1020Kb
Alibaba Cloud Alibaba Cloud 物联物网联平网台 平台 BeBset Psrta Pctricaecstices Document Version: 20200922 Document Version: 20200922 物联网平台 Best Practices · Legal disclaimer Legal disclaimer Alibaba Cloud reminds you to carefully read and fully understand the terms and conditions of this legal disclaimer before you read or use this document. If you have read or used this document, it shall be deemed as your total acceptance of this legal disclaimer. 1. You shall download and obtain this document from the Alibaba Cloud website or other Alibaba Cloud-authorized channels, and use this document for your own legal business activities only. The content of this document is considered confidential information of Alibaba Cloud. You shall strictly abide by the confidentiality obligations. No part of this document shall be disclosed or provided to any third party for use without the prior written consent of Alibaba Cloud. 2. No part of this document shall be excerpted, translated, reproduced, transmitted, or disseminated by any organization, company or individual in any form or by any means without the prior written consent of Alibaba Cloud. 3. The content of this document may be changed because of product version upgrade, adjustment, or other reasons. Alibaba Cloud reserves the right to modify the content of this document without notice and an updated version of this document will be released through Alibaba Cloud- authorized channels from time to time. You should pay attention to the version changes of this document as they occur and download and obtain the most up-to-date version of this document from Alibaba Cloud-authorized channels. 4. This document serves only as a reference guide for your use of Alibaba Cloud products and services. Alibaba Cloud provides this document based on the "status quo", "being defective", and "existing functions" of its products and services. Alibaba Cloud makes every effort to provide relevant operational guidance based on existing technologies. However, Alibaba Cloud hereby makes a clear statement that it in no way guarantees the accuracy, integrity, applicability, and reliability of the content of this document, either explicitly or implicitly. Alibaba Cloud shall not take legal responsibility for any errors or lost profits incurred by any organization, company, or individual arising from download, use, or trust in this document. Alibaba Cloud shall not, under any circumstances, take responsibility for any indirect, consequential, punitive, contingent, special, or punitive damages, including lost profits arising from the use or trust in this document (even if Alibaba Cloud has been notified of the possibility of such a loss). 5. By law, all the contents in Alibaba Cloud documents, including but not limited to pictures, architecture design, page layout, and text description, are intellectual property of Alibaba Cloud and/or its affiliates. This intellectual property includes, but is not limited to, trademark rights, patent rights, copyrights, and trade secrets. No part of this document shall be used, modified, reproduced, publicly transmitted, changed, disseminated, distributed, or published without the prior written consent of Alibaba Cloud and/or its affiliates. The names owned by Alibaba Cloud shall not be used, published, or reproduced for marketing, advertising, promotion, or other purposes without the prior written consent of Alibaba Cloud. The names owned by Alibaba Cloud include, but are not limited to, "Alibaba Cloud", "Aliyun", "HiChina", and other brands of Alibaba Cloud and/or its affiliates, which appear separately or in combination, as well as the auxiliary signs and patterns of the preceding brands, or anything similar to the company names, trade names, trademarks, product or service names, domain names, patterns, logos, marks, signs, or special descriptions that third parties identify as Alibaba Cloud and/or its affiliates. 6. Please directly contact Alibaba Cloud for any errors of this document. > Document Version:20200922 I 物联网平台 Best Practices · Document conventions Document conventions Style Description Example A danger notice indicates a situation Danger: Danger that will cause major system changes, Resetting will result in the loss of faults, physical injuries, and other user configuration data. adverse results. Warning: A warning notice indicates a situation Warning that may cause major system changes, Restarting will cause business faults, physical injuries, and other interruption. About 10 minutes are adverse results. required to restart an instance. A caution notice indicates warning Notice: Notice information, supplementary If the weight is set to 0, the server instructions, and other content that no longer receives new requests. the user must understand. Note: A note indicates supplemental Note instructions, best practices, tips, and You can use Ctrl + A to select all other content. files. Closing angle brackets are used to Click Settings> Network> Set network > indicate a multi-level menu cascade. type. Bold formatting is used for buttons , Bold menus, page names, and other UI Click OK. elements. Run the cd /d C:/window command to Courier font Courier font is used for commands enter the Windows system folder. Italic formatting is used for bae log list --instanceid Italic parameters and variables. Instance_ID This format is used for an optional [] or [a|b] value, where only one item can be ipconfig [-all|-t] selected. This format is used for a required {} or {a|b} value, where only one item can be switch {active|stand} selected. > Document Version:20200922 I 物联网平台 Best Practices · Table of Contents Table of Contents 1.Device access 06 1.1. Access IoT Platform by using Paho 06 1.1.1. Using Paho MQTT C client 06 1.1.2. Using Paho MQTT Java client 15 1.1.3. Using Paho MQTT Go client 19 1.1.4. Using Paho MQTT C# client 23 1.1.5. Using Paho MQTT Android client 28 1.1.6. Troubleshooting 35 1.2. Access IoT Platform by using CoAP 36 1.3. Access IoT Platform by using HTTP 46 1.4. Dynamic registration based on unique-certificate-per-prod…uct auth53entication by using MQTT 1.5. Dynamic registration based on unique-certificate-per-prod…uct auth60entication by using HTTPS 1.6. Access IoT Platform by using MQTT.fx 66 1.7. Connect Android Things to IoT Platform 74 1.8. Connect an RTOS device to IoT Platform by using a TCP …commun80ication module 1.8.1. Overview 80 1.8.2. Create a product and add a device 80 1.8.3. Build a development environment for devices 81 1.8.4. Develop for a device 87 1.9. Connect a bare-metal device to IoT Platform by using T…CP 94 1.9.1. Overview 94 1.9.2. Create a product and add a device 95 1.9.3. Build a development environment for devices 95 1.9.4. Provision a device 100 1.10. Connect a serial device to IoT Platform by using a DTU …device 106 1.10.1. Overview 106 > Document Version:20200922 I Best Practices · Table of Contents 物联网平台 1.10.2. Configure a product and a device 107 1.10.3. Configure a DTU device 113 1.10.4. Test data communication 114 1.11. Connect environmental sensors to IoT Platform by using… HTTPS1 15 1.12. Connect a Linux device to IoT Platform 121 2.Communications 125 2.1. Use custom topics for communication 125 2.2. Control Raspberry Pi servers from IoT Platform 137 2.3. TSL model communication 151 2.4. Communication between devices 165 2.4.1. M2M communication 165 2.4.2. Use the rules engine to establish M2M communicatio…n 165 2.4.3. Use topic-based message routing to establish M2M co…mmun1i6c9ation 2.5. Create a server-side subscription of the MNS type 170 2.6. Parse pass-through data from devices 175 3.Device management 181 3.1. Set desired property values to control the light bulb stat…us 181 3.2. Call a synchronous service 197 3.3. Connect a sub-device to IoT Platform 209 4.Data forwarding 210 4.1. Integrate with a weather service 210 4.2. Upload temperature and humidity data to DingTalk chat…bots 217 II > Document Version:20200922 物联网平台 Best Practices · Device access 1.Device access 1.1. Access IoT Platform by using Paho 1.1.1. Using Paho MQTT C client This topic describes how to use the open-source Paho MQTT C/C++ client to access Alibaba Cloud IoT Platform for message exchanges. IoT Internet of Things Paho.MQTT. C Prerequisites Products and devices are created in IoT Platform. For more information, see Create a product and Create a device. Prepare the development environment In this example, Ubuntu 16.04-LTS is used for building the development environment. Run the following command to build the development environment. sudo apt-get update sudo apt-get install build-essential git sed cmake Download the Paho MQTT library for C Run the following command to clone the C language version of the Paho MQTT library. git clone [email protected]:eclipse/paho.mqtt.embedded-c.git Note The demo example uses the master branch. The commit id is 29ab2aa29c5e47794284376d7f8386cfd54c3eed . The Paho MQTT for C project provides three sub-projects: MQTTPacket: Provides serialization and deserialization of MQTT data packets and some additional helper functions. MQTTClient: Encapsulates the high-level C++ client program generated by MQTTPacket. MQTTClient-C: Encapsulates the high-level C client program generated by MQTTPacket. MQTTClient-C includes: > Document Version:20200922 6 Best Practices · Device access 物联网平台 ├── CMakeLists.txt ├── samples │ ├── CMakeLists.txt │ ├── FreeRTOS │ └── linux ├── src │ ├── CMakeLists.txt │ ├── FreeRTOS │ ├── MQTTClient.c │ ├── MQTTClient.h │ ├── cc3200 │ └── linux └── test ├── CMakeLists.txt └── test1.c The samples directory provides sample code for FreeRTOS and Linux systems. The src directory provides the code for MQTTClient and network drivers for porting to FreeRTOS, CC3200, and Linux. For more information about the Paho MQTT APIs, see MQTTClient.h.