MQTT客户端编程--C语言

 

环境准备

①安装paho c库

在https://github.com/eclipse/paho.mqtt.c/tree/v1.3.0下载V1.3.0版本

解压

tar zxvf paho.mqtt.c-1.3.0.tar.gz

cd paho.mqtt.c-1.3.0

编译

# make

在paho.mqtt.c-1.3.0/build/output下可以找到如下的输出文件:

# make install

库将被安装到/usr/local/lib中,需要把库的路径增加到/etc/ld.so.conf。然后执行ldconfig使配置生效

 

测试:

 

创建客户端的步骤:

        1.创建一个客户端对象; 
  2.设置连接MQTT服务器的选项; 
  3.如果多线程(异步模式)操作被使用则设置回调函数(详见 Asynchronous >vs synchronous client applications); 
  4.订阅客户端需要接收的任意话题; 
  5.重复以下操作直到结束: 
    a.发布客户端需要的任意信息; 
    b.处理所有接收到的信息; 
  6.断开客户端连接; 
  7.释放客户端使用的所有内存。

  我们使用Paho自带的示例程序。打开paho.mqtt.c-1.3.0/src/samples下的MQTTClient_subscribe .c文件。将以下的代码更改:

#define ADDRESS     "tcp://192.168.43.128:1883"
#define CLIENTID    "ExampleClientSub"
#define TOPIC       "MQTT Examples"
#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L

  如果你的MQTT服务器不允许匿名访问,则还需要添加姓名和密码:

  char *username= "user"; //用户名
  char *password = "test"; //密码
  • 并将用户名和密码写入连接选项中:
    conn_opts.username = username; //将用户名写入连接选项中
    conn_opts.password = password; //将密码写入连接选项中
  • 添加的代码具体位置详细查看代码,更改后的代码如下所示:
/*******************************************************************************
 * Copyright (c) 2012, 2017 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution. 
 *
 * The Eclipse Public License is available at 
 *   http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Ian Craggs - initial contribution
 *******************************************************************************/

#include 
#include 
#include 
#include "MQTTClient.h"

#define ADDRESS     "tcp://192.168.43.128:1883"
#define CLIENTID    "ExampleClientSub"
#define TOPIC       "MQTT Examples"
#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L

volatile MQTTClient_deliveryToken deliveredtoken;

void delivered(void *context, MQTTClient_deliveryToken dt)
{
    printf("Message with token value %d delivery confirmed\n", dt);
    deliveredtoken = dt;
}

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

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

    payloadptr = message->payload;
    for(i=0; ipayloadlen; i++)
    {
        putchar(*payloadptr++);
    }
    putchar('\n');
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}

void connlost(void *context, char *cause)
{
    printf("\nConnection lost\n");
    printf("     cause: %s\n", cause);
}

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

    MQTTClient_create(&client, ADDRESS, CLIENTID,
        MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;

    MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);

    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
    printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
           "Press Q to quit\n\n", TOPIC, CLIENTID, QOS);
    MQTTClient_subscribe(client, TOPIC, QOS);

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

    MQTTClient_unsubscribe(client, TOPIC);
    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);
    return rc;
}

更改完成之后回到paho.mqtt.c-1.3.0目录,执行make编译出bin文件

进入paho.mqtt.c-1.3.0/build/output/samples,看到重新编译生成的MQTTClient_scribe ,执行以下命令:

 ./MQTTClient_subscribe
  • 同时开启一个发布端发布消息,观察该应用是否能正常接收到消息
  • 输出以下结果:
  • MQTT客户端编程--C语言_第1张图片

 

  

你可能感兴趣的:(物联网)