一.连接阿里云服务器测试
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