Connecting To The MQTT Broker Using The Open-source C Language ...

检测到您已登录华为云国际站账号,为了您更好的体验,建议您访问国际站服务网站 https://www.huaweicloud.com/intl/zh-cn

不再显示此消息

  • Intl-English International
    • English
    • Bahasa Indonesia
    • Español
    • Português
    • Türkçe
    • عربي
    • ไทย
    • 简体中文
    • 日本語
    中国站
    • 简体中文
    Europe
    • English
    • Deutsch
    • Español
    • Français
    • Nederlands
  • Huawei Cloud
    • Activities
    • Products
    • Solutions
    • Pricing
    • KooGallery
    • Partners
    • Developers
    • Support
    • About Us
    Hot
    • Free Packages
    • Elastic Cloud Server (ECS)
    • Cloud Backup and Recovery (CBR)
    • Host Security Service (HSS)
    • Cloud Container Engine (CCE)
    Show more results for “”
    • Contact Us
    • Documentation
    • Console
      • My Account
      • Billing & Costs
      • Service Tickets
      • Unread Messages
      • Console
      • Partner Center
      • Sign In Sign Up
    • Sign In
    • Sign Up
      • My Account Complete Sign Up
      • Billing & Costs
      • Service Tickets
      • Unread Messages
      • Console
      • Partner Center
      • Log Out
    Cancel Hot
    • Free Packages
    • Elastic Cloud Server (ECS)
    • Cloud Backup and Recovery (CBR)
    • Host Security Service (HSS)
    • Cloud Container Engine (CCE)
    Help Center/ Intelligent EdgeFabric/ Best Practices/ Connecting to the MQTT Broker Using the Open-source C Language Library Updated on 2024-09-19 GMT+08:00 View PDF Share
    • x.com
    • Facebook
    • LinkedIn
    • Copy link

    Copied.

    Connecting to the MQTT Broker Using the Open-source C Language Library

    Scenario

    MQTT is a message protocol in publish/subscribe mode. It works even on remote devices with poor hardware performance and in poor network conditions.

    This article describes how to connect and use IEF's built-in MQTT broker using an open-source C language library, Eclipse Paho C Client Library.

    Procedure

    1. Prepare a Linux computer and download the source code.

      git clone https://github.com/eclipse/paho.mqtt.c.git

      You can also download the ZIP package from https://github.com/eclipse/paho.mqtt.c and decompress it.

    2. Install the compilation dependency tool.

      For Ubuntu, run the following commands:

      apt-get install build-essential gcc make cmake cmake-gui cmake-curses-gui

      apt-get install fakeroot fakeroot devscripts dh-make lsb-release

      apt-get install libssl-dev

      apt-get install ninja-build

      For CentOS, run the following commands:

      yum install build-essential gcc make cmake cmake-gui cmake-curses-gui

      yum install fakeroot fakeroot devscripts dh-make lsb-release

      yum install openssl-devel

      yum install ninja-build

    3. Modify the sample code.

      Add the following line to the src/samples/ paho_cs_pub.c file of the source code.

      ssl_opts.enableServerCertAuth =0;

      Add the following line to the src/samples/paho_cs_sub.c file.

    4. Compile the sample program.

      mkdir /tmp/build.paho

      cd /tmp/build.paho

      cmake -GNinja -DPAHO_BUILD_STATIC=TRUE -DPAHO_BUILD_SHARED=FALSE -DPAHO_WITH_SSL=TRUE -DPAHO_BUILD_SAMPLES=TRUE {paho.mqtt.c directory}

      ninja package

      {paho.mqtt.c directory} is the directory where the source code paho.mqtt.c is stored, for example, /root/work/paho.mqtt.c.

    5. Go to the directory where the compiled sample program is located, and copy the generated binary files paho_cs_pub and paho_cs_sub to the edge node.

      cd /tmp/build.paho/src/samples/

    6. Download the edge node certificate.
      1. Log in to the IEF console, choose Managed Resources > Edge Nodes in the navigation pane, and click an edge node name on the displayed page to view the edge node details page. Click the Certificates tab, and click Add Certificate. Figure 1 Adding a certificate
      2. In the displayed dialog box, enter the certificate name and click OK.
      3. Copy the downloaded certificate to the edge node and decompress it.
    7. Run the sample program.

      For example, to publish a message to a specified topic, run the following commands to view the paho_cs_pub command instructions.

      The following is an example of publishing a message:

      ./paho_cs_pub -c ssl://127.0.0.1:8883 -q 0 -m "xxx" -t "aaa" --cert /root/mqtt_cert/xOEMIsYVpw_private_cert.crt --key /root/mqtt_cert/xOEMIsYVpw_private_cert.key

      Running this command will send a message whose content is xxx to the aaa topic in the built-in MQTT broker. 127.0.0.1:8883 is the address of the built-in MQTT broker of the edge node. /root/mqtt_cert/xOEMIsYVpw_private_cert.crt and /root/mqtt_cert/xOEMIsYVpw_private_cert.key are the edge node certificates.

    Previous topic: Connecting Edge Nodes to IEF Through Direct Connect or VPN

    Feedback

    Was this page helpful?

    Helpful Not helpful Provide feedback

    Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.

    The system is busy. Please try again later.

    Which of the following issues have you encountered?

    Content is inconsistent with the product UI Unclear descriptions Lack of examples or code Incorrect steps Can't find what I need Lack of best practices

    Feedback (optional)

    0/500

    Select at least one type of issue, and enter your comments or suggestions.

    Enter a maximum of 500 characters.

    Submit Cancel

    For any further questions, feel free to contact us through the chatbot.

    Chatbot Contact Sales After-Sales Self Service
    • Site Terms
    • Privacy Statement

    Explore Huawei Cloud

    Why Us Customer Stories Trust Center Legal Press Releases

    Featured Services

    Elastic Cloud Server (ECS) Elastic IP (EIP) RDS for MySQL Elastic Volume Service (EVS) MapReduce Service (MRS)

    Service and Support

    Documentation Contact Us Public Notices Support Plans Service Health Dashboard

    Account and Payment

    Top Up Invoices Billing Center My Account Payment Method

    Quick Links

    Huawei Corporate Huawei Enterprise Huawei Consumer Business Huawei Developers

    © 2025, Huawei Cloud Computing Technologies Co., Ltd. and/or its affiliates. All rights reserved.

    • Site Terms
    • Privacy Statement

    Từ khóa » Mqtt Github C