From 77f01032c0c9b9a510e1abfa7d2b3be1d588e74d Mon Sep 17 00:00:00 2001 From: szdot Date: Wed, 13 Sep 2023 13:43:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E4=B8=BB=E9=A2=98=20=20=E5=A4=9A=E4=B8=BB=E9=A2=98=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E4=B8=BA=20planeState/macadd=20PS:=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E6=8A=8A=E9=A1=B9=E7=9B=AE=20=E7=94=A8=E4=B8=BB=E9=A2=98?= =?UTF-8?q?=E5=90=8D=E5=AD=97=E5=8F=91=E9=80=81=20=E7=8E=B0=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=8C=85=E5=90=AB=E5=9C=A8=20json=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E4=B8=AD=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FoodDelivery.ino | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/FoodDelivery.ino b/FoodDelivery.ino index 93e45ac..e9333e2 100644 --- a/FoodDelivery.ino +++ b/FoodDelivery.ino @@ -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(); }