百度IOT Hub (12) Paho C Client Async Subscribe Operation without SSL

1. 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"

#if !defined(WIN32)
#include <unistd.h>
#else
#include <windows.h>
#endif

#if defined(_WRS_KERNEL)
#include <OsWrapper.h>
#endif

#define ADDRESS     "tcp://sdmn79a.mqtt.iot.gz.baidubce.com:1883"
#define CLIENTID    "MyMonitor_Sub_Any"
 /// MUST Be Subcribed by the different Clients Belonged in the Same Shadow
#define TOPIC       "$baidu/iot/shadow/MyMonitor/update/accepted" 
//#define PAYLOAD     "Hello World!"
#define USER "sdmn79a/MyMonitor"
#define PWD  "xxxxxxxxxxx"
#define QOS         1
#define TIMEOUT     10000L

volatile MQTTAsync_token deliveredtoken;

int disc_finished = 0;
int subscribed = 0;
int finished = 0;

void connlost(void *context, char *cause)
{
    MQTTAsync client = (MQTTAsync)context;
    MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
    int rc;

    printf("\n[connlost] : Connection lost\n");
    if (cause)
        printf("     cause: %s\n", cause);

    printf("[connlost] : Reconnecting\n");
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;
    if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
    {
        printf("[connlost] : Failed to start connect, return code %d\n", rc);
        finished = 1;
    }
}


int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
{
    int i;
    char* payloadptr;

    printf("[msgarrvd] : Message arrived\n");
    printf("     topic: %s\n", topicName);
    printf("   message: ");

    payloadptr = message->payload;
    for(i=0; i<message->payloadlen; i++)
    {
        putchar(*payloadptr++);
    }
    putchar('\n');
    MQTTAsync_freeMessage(&message);
    MQTTAsync_free(topicName);
    return 1;
}


void onDisconnect(void* context, MQTTAsync_successData* response)
{
    printf("[onDisconnect] : Successful disconnection\n");
    disc_finished = 1;
}


void onSubscribe(void* context, MQTTAsync_successData* response)
{
    printf("[onSubscribe] : Subscribe succeeded\n");
    subscribed = 1;
}

void onSubscribeFailure(void* context, MQTTAsync_failureData* response)
{
    printf("[onSubscribeFailure] : Subscribe failed, rc %d\n", response ? response->code : 0);
    finished = 1;
}


void onConnectFailure(void* context, MQTTAsync_failureData* response)
{
    printf("[onConnectFailure] : Connect failed, rc %d\n", response ? response->code : 0);
    finished = 1;
}


void onConnect(void* context, MQTTAsync_successData* response)
{
    MQTTAsync client = (MQTTAsync)context;
    MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
    int rc;

    printf("[onConnect] : Successful connection\n");

    printf("[onConnect] : Subscribing to topic %s\nfor client %s using QoS%d\n\n"
           "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
    opts.onSuccess = onSubscribe;
    opts.onFailure = onSubscribeFailure;
    opts.context = client;

    deliveredtoken = 0;

    if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS)
    {
        printf("[onConnect] : Failed to start subscribe, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
}


int main(int argc, char* argv[])
{
    MQTTAsync client;
    MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
    MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;
    int rc;
    int ch;

    MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);

    MQTTAsync_setCallbacks(client, client, connlost, msgarrvd, NULL);

    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;
    conn_opts.onSuccess = onConnect;
    conn_opts.onFailure = onConnectFailure;
    conn_opts.context = client;
    conn_opts.username = USER;
    conn_opts.password = PWD;
    conn_opts.MQTTVersion = MQTTVERSION_3_1_1;

    if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
    {
        printf("[main] : Failed to start connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }

    while    (!subscribed)
        #if defined(WIN32)
            Sleep(100);
        #else
            usleep(10000L);
        #endif

    if (finished)
        goto exit;

    do 
    {
        ch = getchar();
    } while (ch!='Q' && ch != 'q');

    disc_opts.onSuccess = onDisconnect;
    if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS)
    {
        printf("[main] : Failed to start disconnect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
     while    (!disc_finished)
        #if defined(WIN32)
            Sleep(100);
        #else
            usleep(10000L);
        #endif

exit:
    MQTTAsync_destroy(&client);
     return rc;
}
2. 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值