在linux下使用c语言实现MQTT通信(二.编程实现)

本文介绍了在Linux系统中,使用C语言和Paho MQTT库进行MQTT通信的实现过程。包括下载Paho MQTT C库,理解不同动态库的用途,以及发布端和订阅端的程序实现,最终成功实现了数据的发布和订阅,并将接收到的数据存入数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我是用的secureCRT登录的树莓派,要实现MQTT通信,就需要用到许多关于MQTT的函数,这里我用的是Paho.c库,所以首先下载库:
在git下下载paho C库git clone https://github.com/eclipse/paho.mqtt.c.git

cd paho.mqtt.c
make//编译
sudo make install//安装

执行make之后我们在build/output/下能看到以下动态库:

iot@Public_RPi:~/caiwentao/team_lihaojie/caiwentao/mqtt/mqtt_publish/paho.mqtt/build/output $ ls
libpaho-mqtt3a.so      libpaho-mqtt3as.so      libpaho-mqtt3c.so      libpaho-mqtt3cs.so      paho_c_version
libpaho-mqtt3a.so.1    libpaho-mqtt3as.so.1    libpaho-mqtt3c.so.1    libpaho-mqtt3cs.so.1    samples
libpaho-mqtt3a.so.1.0  libpaho-mqtt3as.so.1.0  libpaho-mqtt3c.so.1.0  libpaho-mqtt3cs.so.1.0  test

在这里说一下这里面的各个动态库的作用:
paho-mqtt3a : 一般实际开发中就是使用这个,a表示的是异步消息推送(asynchronous)。
paho-mqtt3as : as表示的是 异步+加密(asynchronous+OpenSSL)。
paho-mqtt3c : c 表示的应该是同步(Synchronize),一般性能较差,是发送+等待模式。
paho-mqtt3cs : cs表示的是同步+加密(asynchronous+OpenSSL)。
一丶发布端程序

get_temperature.c  get_temperature.h  get_time.c  get_time.h  opt_init.c  opt_init.h  publish.c  set_signal.c  set_signal.h

下面解释一下这些c代码:
1丶get_temperature.c就是发布端发布的消息,其内容是实验室树莓派获取的实时温度,其代码如下:

#include<stdio.h>
#include<unistd.h>
#include<dirent.h>
#include<string.h>
#include<sys/types.h>
#include<errno.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include"get_temperature.h"
int  get_temperature(float *temper)
{
   
        char    path[128]="/sys/bus/w1/devices/";
        char    path_s[32];
        char    buf[128];
        char    *ptr;
        DIR     *dirp;
        int     a=0;
        int     fd; 
        struct dirent *direntp;
        if((dirp=opendir(path))==NULL)
        {
      
                printf("Open %s failure:%s\n",path,strerror(errno));
                return -1; 
        }   
        while((direntp=readdir(dirp))!=NULL)
        {
      
                if(strstr(direntp->d_name,"28-"))
                {
      
                        strcpy(path_s,direntp->d_name);
                        a=1;
                }   
        }   
        if(a==0)
        {
      
                printf("Can not find ds18b20 in %s\n",path);
                return -1; 
        }   
        strncat(path,path_s,sizeof(path));
        strncat(path,"/w1_slave",sizeof(path));
        if((fd=open(path,O_RDONLY))<0)
        {
      
                printf("Open %s failure:%s\n",path,strerror(errno));
                return -1;
        }
        if(read(fd,buf,sizeof(buf))<0)
        {
   
                printf("read data from %s failure:%s\n",path,strerror(errno));
                return -1;
        }
        ptr=strstr(buf,"t=");

        ptr+=2;

        if(!ptr)
        {
   
                printf("ERROR:%s\n",strerror(errno));
                return 1000;
        }
        *temper=atof(ptr)/1000;
        return 0;
}

2丶get_time.c:获取当前时间,网上有很多,我的示例代码如下:

1 #include<time.h>
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include"get_time.h"
  5 int get_time(char time_s[24])
  6 {
   
  7         time_t   timec;
  8         char *str;
  9         time(&timec);
 10         str=ctime(&timec);
 11         char *chr=strchr(str,'\n');
 12         *chr='\0';
 13         strcpy(time_s,str);
 14         return 0;
 15 
 16 }

3丶opt_init.c:参数初始化函数。示例代码如下:

  1 #include<stdio.h>
  2 #include<getopt.h>
  3 #include<unistd.h>
  4 #include<string.h>
  5 #include<stdlib.h>
  6 #include"opt_init.h"
  7 void usage(char *arg)
  8 {
   
  9         printf("%s usage:\n",arg);
 10         puts("-p (--port)get the port");
 11         puts("-d (--daemon)run back");
 12         puts("-a (-address)the publish address");
 13         puts("-h (--help)get the help message");
 14         puts("-i (--id) get the publish id");
 15         puts("-t (--topic)the publish topic");
 16         return ;
 17 }
 18 
 19 int opt_init(int *port,char name[],char sub_id[],char topic[],int argc,char **argv)
 20 {
   
 21         struct option opts[]=
 22
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值