mqtt C语言版本,MQTT客户端编程--C语言

环境准备php

①安装paho c库html

解压

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

cd paho.mqtt.c-1.3.0服务器

编译session

# make多线程

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

0871a4369d832cb525a48edfa2e6f881.png

# make installeclipse

库将被安装到/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语言版本,MQTT客户端编程--C语言_第1张图片

你可能感兴趣的:(mqtt,C语言版本)