修改 发布主题 多主题合并为 planeState/macadd PS:之前把项目 用主题名字发送 现改为包含在 json字符串中发送

This commit is contained in:
szdot 2023-09-13 13:43:09 +08:00
parent b33bba3dd2
commit 77f01032c0

View File

@ -8,7 +8,7 @@ char* password = "fxmf0622"; //wifi密码
char* mqttServer = "szdot.top"; //mqtt地址
int mqttPort = 1883; //mqtt端口
char* mqttName = "admin"; //mqtt帐号
char* mqttPassword = "123456"; //mqtt密码
char* mqttPassword = "1234567"; //mqtt密码
uint8_t mavlinkSerial = 2; //飞控占用的串口号
uint8_t voiceSerial = 1; //声音模块占用串口
char* udpServerIP = "192.168.3.92"; //云台相机ip
@ -19,12 +19,13 @@ FoodCube fc(ssid, password, mqttServer, mqttPort, mqttName, mqttPassword, mavlin
//0:飞行航点任务 1设置飞机状态 2:获取飞机状态 3:设置飞机初始状态 4:油门通道1 5:油门通道2 6:油门通道3 7:油门通道4 8:钩子控制 9:云台相机控制
String topicSub[] = { "questAss", "setPlaneState", "getPlaneState", "resetState", "chan1", "chan2", "chan3", "chan4", "hookConteroller", "cameraController" }; //订阅主题
int topicSubCount = sizeof(topicSub) / sizeof(topicSub[0]); //订阅主题总数
/*有更新主动发送 主题*/
/* 发布 主题 ps:以下是登记发布json内容的组成元素 */
//登记 json成员名字
//0:心跳信息 1:电压信息 2:电流信息 3:电池电量 4:高度信息 5:对地速度 6:卫星数量 7:纬度 8:经度 9:定位状态 10:飞机状态 11:网速测试 12:飞机模式 13:重量 14:钩子状态 15:飞机海拔高度
String topicPub[] = { "heartBeat", "voltagBattery", "currentBattery", "batteryRemaining", "positionAlt", "groundSpeed", "satCount", "latitude", "longitude", "fixType", "planeState", "pingNet", "getPlaneMode", "loadweight", "hookstatus", "altitude" };
int topicPubCount = sizeof(topicPub) / sizeof(topicPub[0]); //发送主题总数
String topicPubMsg[16]; //发送数据存放 对应topicPub
String oldMsg[16]; //记录旧的数据 用来对比有没有更新
int topicPubCount = sizeof(topicPub) / sizeof(topicPub[0]); //登记 json成员总数
String topicPubMsg[16]; //登记 json成员的值 对应topicPub
String oldMsg[16]; //记录旧的 用来对比有没有更新
/*触发发送 主题*/
//0:对频信息
String topicHandle[] = { "crosFrequency" };
@ -507,8 +508,11 @@ void mavlink_receiveCallback(uint8_t c) {
* @description: 线
*/
void pubThread() {
/*解析mavlink 数据流等 此函数内会把解析的数据按信息类型 发布到对应主题*/
for (int i = 0; i < topicPubCount; i++) { //遍历向订阅主题 有数据更新时 发送信息
/*解析mavlink 数据流等 此函数内会把解析的数据按信息类型 发布到mqtt服务器 planeState/macadd主题 */
// 创建一个JSON对象
DynamicJsonDocument doc(200); // 200字节 缓冲区
//遍历 有更新的数据 组成一个json对象
for (int i = 0; i < topicPubCount; i++) {
if (topicPubMsg[i] != oldMsg[i]) {
if (i == 0) { //心跳包 每每向心跳主题发布信息
//启动飞控 第一次心跳 ps:防止飞控 滞后启动 拿不到数据
@ -516,14 +520,18 @@ void pubThread() {
fc.setIsInit(false);
fc.mav_request_data(); //再向飞控请求一次 设定飞控输出数据流内容
}
//发送心跳包
fc.pubMQTTmsg(topicPub[0], topicPubMsg[0]);
} else { //非心跳包 有更新才向对应主题发布信息
fc.pubMQTTmsg(topicPub[i], topicPubMsg[i]);
//设置对象成员 ps:心跳
doc[topicPub[0]] = topicPubMsg[0];
} else { //非心跳 有更新 录入成员
doc[topicPub[i]] = topicPubMsg[i];
oldMsg[i] = topicPubMsg[i];
}
}
}
// 将JSON对象序列化为JSON字符串
String jsonString;
serializeJson(doc, jsonString);
fc.pubMQTTmsg("planeState", jsonString);
/*更新4G网络测速ping值*/
//pingNetTest();
}