2021-07-11 C语言 mqtt 协议 样例

1/uuid.h

  centos 需要  uuid-devel libuuid-devel
  ubuntu  uuid-dev

2/arm交叉编译后的 so 文件
复制或者ln即可

   默认lib安装到 /usr/local/lib
   ln /usr/local/lib/  /usr/lib/

3/配置交叉编译环境

1/rv3389 openwrt官方源码已经支持,可以直接编译op的toolchain 后 export 临时变量即可
2/复制op目录编译好的so 库文件然后执行  ldconfig也可以
3/可能需要 sudo 修改文件权限

cmake 生成需要安装依赖
在这里插入图片描述

4/op的 driver 有rv1108/1126 clk 时钟驱动

半成品代码

//gcc -o mqtt struct_conf.c
//${CROSS_COMPILE}gcc -o cpuid gets.c
//this code for tested ok rv1126
//if you want to run in x86 change "serial" to cpu MHx 
#include<stdio.h>    
#include<stdlib.h> 
#include<string.h>    
 #include<unistd.h>  
#include<signal.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<termios.h>
#include <errno.h>
#include <fcntl.h>


#include <mosquitto.h>

bool session = true; 

  /* exact-width signed integer types */
typedef   signed           char int8_t;
typedef   signed short     int int16_t;
typedef   signed           int int32_t;
/* exact-width unsigned integer types */
typedef unsigned           char uint8_t;
typedef unsigned short     int uint16_t;
typedef unsigned           int uint32_t;

#define MQTT_TOPIC_SIZE     (128)		
#define MQTT_BUF_SIZE       (8 * 1024) 
#define MQTT_HOST "localhost"		
#define MQTT_PORT 1883 //平台mqtt 配置	
		
//用户名密码为空			
//#define MQTT_USER "test"				
//#define MQTT_PASS "test"			

typedef struct 
{
int iotstatus;
char *IOT_MQTT_Config_Opions_Host; //"localhost"
short IOT_MQTT_Config_Opions_Port; //1883
char *IOT_MQTT_Config_Opions_uuid; //
int IOT_MQTT_Config_Opions_IntervalTimes;
char IOT_MQTT_Config_Optins_username[64];	
char IOT_MQTT_Config_Optins_password[64];
}IOT_MQTT_Config_Options;

typedef struct
{
  char IOT_SUB_TOPIC[128];
 char IOT_PUB_TOPIC[128];
}IOT_MQTT_TOPIC;

//init login value
IOT_MQTT_Config_Options login = 
{
.iotstatus = 100,  
.IOT_MQTT_Config_Optins_username = {"testuser"},
.IOT_MQTT_Config_Optins_password = {"testpass"},
};

//init topic value
IOT_MQTT_TOPIC topic =
{
.IOT_SUB_TOPIC = {"SUB_TOPIC"},
.IOT_PUB_TOPIC = {"PUB_TOPIC"},
};


//登陆MQTT服务器发送验证
static int iot_mqtt_init()
{
return 0;
}

//直接登陆返回正常 
//mosquitto_sub -h mqtestproject.seater.cn -p 13306 -d -t "test" -k 10
int main(int argc, const char * argv[])    
{   
struct mosquitto *mosq = NULL;
char buff[MSG_MAX_SIZE];

//libmosquitto 库初始化
mosquitto_lib_init();
//char user[64] = {0};
  // char pass[64] = "password";
//char req_topic[64] = "test_topic";

//sprintf(user, "%s", login.IOT_MQTT_Config_Optins_username);
//sprintf(login.IOT_MQTT_Config_Optins_username, "%s", pass);
//printf("mqtt test user %s--pass %s!!\n", user, pass);

//user sprintf for json char for test
  // sprintf(topic.IOT_SUB_TOPIC, "%s", req_topic);
  // printf("test topic :%s\n", topic.IOT_SUB_TOPIC);

//创建mosquitto客户端

//   mosq = mosquitto_new(NULL, session, NULL);
mosq = mosquitto_new(NULL, session, NULL);	
if(!mosq)
{
    printf("create client failed..\n");
    mosquitto_lib_cleanup();
    return 1;
}

//获取发布的topic

//打印topic字符到变量

//执行发布操作


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值