[MQTT]mosquitto库上报温度到阿里云平台

本文介绍了如何利用mqtt.fx连接阿里云服务器,通过mosquitto库将温度数据以cJSON格式打包,然后上传到阿里云平台。文章详细讲述了配置文件的解析、cJSON数据打包以及流程设计,并提供了代码实现测试,每10秒发布一次温度数据。
摘要由CSDN通过智能技术生成

一.连接阿里云服务器测试

mqtt.fx测试与阿里云平台的连通。

可以参考博客 mqtt.fx实现与阿里云的通信
主要是记录一下mqtt连接参数和设备证书,mqtt.fx连接上云平台。并使用publish和subcribe进行订阅和发布具体实现参考上面博客。
在这里插入图片描述
在这里插入图片描述
记录下在云平台上的配置信息

客户ID
clientId
gv9vUe6QzX6.MQTT|securemode=2,signmethod=hmacsha256,timestamp=2524608000000|

用户名
username
MQTT&gv9vUe6QzX6

密码
passwd
34bde8763e13a25d2448e8594dc8a69650e05cf70c1fb8fa731ffeaedef95

mqtt的host
mqttHostUrl
iot-06z00gyvi020tfv.mqtt.iothub.aliyuncs.com

端口号
port
1883

ProductKey
gv9vUe6QzX6

DeviceName
MQTT

DeviceSecret
c6dd3dfb77d3f64b73475c7ee0880a74

发布主题
/sys/gv9vUe6QzX6/${
   deviceName}/thing/event/property/post   

订阅主题
/sys/gv9vUe6QzX6/${
   deviceName}/thing/service/property/set   

二.iniparser库介绍

为了实现代码复用性,我们把不同云平台的配置文件单独写成一个.ini配置文件,这样要解析配置文件中的信息就需要用到iniparser库函数。可以参考博客:iniparser库的安装和使用

三.CJSON库介绍

由于阿里云对消息的格式比较严格,需要打包成cJSON的格式才能正确获得数据,所以我们使用mosquito的APImosquitto库和mqtt介绍连接上阿里云后需要把数据打包成cJSON的格式再上传。这里需要用到cJSON库简介打包数据。

四.需要使用到的文件

首先是需要使用的到连接阿里云平台的配置信息.ini文件

[mqtt_server_addr]
host        =iot-06z00gyvi020tfv.mqtt.iothub.aliyuncs.com
port        =1883

[user_passwd]
username    =MQTT&gv9vUe6QzX6
passwd      =34bde8763e13a25d2448e8594dc8a69650e05cf70c1fb8fa731ffeaedef95

[client_id]
id          =gv9vUe6QzX6.MQTT|securemode=2,signmethod=hmacsha256,timestamp=2524608000000|

[sub_topic]
topic       =/sys/gv9vUe6QzX6/MQTT/thing/service/property/set

[pub_topic]
topic       =/sys/gv9vUe6QzX6/MQTT/thing/event/property/post

[ali_json]
method      =thing.service.property.set
id          =1759550978
identifier  =CurrentTemperature
version     =1.0.0"

[KEEP_ALIVE]
alive       =60

[ali_Qos]
Qos         =0

由于需要解析配置文件我们自己写一个关于解析配置文件的头文件和c文件

/********************************************************************************
 *      Copyright:  (C) 2022 hubeiwuhan
 *                  All rights reserved.
 *
 *       Filename:  mosq_conf.h
 *    Description:  This head file 
 *
 *        Version:  1.0.0(23/02/22)
 *         Author:  yanp <2405204881@qq.com>
 *      ChangeLog:  1, Release initial version on "23/02/22 14:25:16"
 *                 
 ********************************************************************************/
#ifndef MQTT_CONF_H
#define MQTT_CONF_H

#define  BUF_SIZE 512
/设置默认参数,假如没有传入参数就是用配置文件内的默认参数/
#define DEFAULT_CLIENTID    "gv9vUe6QzX6.MQTT|securemode=2,signmethod=hmacsha256,timestamp=2524608000000|"

#define DEFAULT_USERNAME    "MQTT&gv9vUe6QzX6"
#define DEFAULT_PASSWD      "34bde8763e13a25d2448e8594dc8a69650e05cf70c1fb8fa731ffeaedd22ef95"

#define DEFAULT_HOSTNAME    "iot-06z00gyvi020tfv.mqtt.iothub.aliyuncs.com"
#define DEFAULT_PORT        1883

#define DEFAULT_SUBTOPIC    "/sys/gv9vUe6QzX6/MQTT/thing/service/property/set"
#define DEFAULT_PUBTOPIC    "/sys/gv9vUe6QzX6/MQTT/thing/event/property/post"

#define DEFAULT_QOS         0

#define DEFAULT_METHOD      "thing.service.property.set"
#define DEFAULT_JSONID      "1759550978"
#define DEFAULT_IDENTIFIER  "CurrentTemperature"
#define DEFAULT_VERSION     "1.0.0.0"

#define KEEP_ALIVE          60

enum{
   
    SUB,
    PUB
};

typedef struct data_st_mqtt
{
   
    char    hostname[BUF_SIZE] ;
    int     port ;
    char    username[BUF_SIZE] ;
    char    passwd[BUF_SIZE] ;
    char    clientid[BUF_SIZE] ;
    char    topic[BUF_SIZE] ;
    int     Qos;

    char    method[BUF_SIZE] ;
    char    jsonid[BUF_SIZE] ;
    char    identifier[BUF_SIZE] ;
    char    version[BUF_SIZE] ;
}st_mqtt;

int gain_mqtt_conf(char *ini_path,st_mqtt *mqtt,int type);/获取配置参数的函数/
/*********************************************************************************
 *      Copyright:  (C) 2022 hubeiwuhan
 *                  All rights reserved.
 *
 *       Filename:  mqtt_conf.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(24/02/22)
 *         Author:  yanp <2405204881@qq.com>
 *      ChangeLog:  1, Release initial version on "24/02/22 13:45:05"
 *                 
 ********************************************************************************/
#include 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值