百度IOT Hub (十) Paho C Client Publish Operation without SSL

  1. #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "MQTTClient.h"

    #define ADDRESS     "tcp://sdmn79a.mqtt.iot.gz.baidubce.com:1883"
    #define CLIENTID    "MyMonitor_Pub"
    #define TOPIC       "$baidu/iot/general/abc"
    #define PAYLOAD     "Hello World!"
    #define QOS         1
    #define TIMEOUT     10000L

    int main(int argc, char* argv[])
    {
        MQTTClient client;
        MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;

        MQTTClient_message pubmsg = MQTTClient_message_initializer; //==> MQTTClient_publishMessage
        MQTTClient_deliveryToken token; // ==> MQTTClient_publishMessage
        int rc;

        // 1. Create MQTT Client
        MQTTClient_create(&client, ADDRESS, CLIENTID,
            MQTTCLIENT_PERSISTENCE_NONE, NULL);

        conn_opts.keepAliveInterval = 20;
        conn_opts.cleansession = 1;
        conn_opts.username = "sdmn79a/MyMonitor_Pub";
        conn_opts.password = "xxxxxxx";
        conn_opts.MQTTVersion = MQTTVERSION_3_1_1;

        // 2. Establish MQTT Connection
        if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
        {
            printf("Failed to connect, return code %d\n", rc);
            exit(EXIT_FAILURE);
        }

        // 3. Send Topic with Payload
        pubmsg.payload = PAYLOAD;
        pubmsg.payloadlen = (int)strlen(PAYLOAD);
        pubmsg.qos = QOS;
        pubmsg.retained = 0;

        MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
        // MQTTClient_publish(c, test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained, &token);

        printf("Waiting for up to %d seconds for publication of %s\n"
            "on topic %s for client with ClientID: %s\n",
            (int)(TIMEOUT / 1000), PAYLOAD, TOPIC, CLIENTID);

        // 4. Wait For Completion
        rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
        printf("Message with delivery token %d delivered\n", token);

        MQTTClient_disconnect(client, 10000);
        MQTTClient_destroy(&client);

        return rc;
    }
     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值