修改wifi和mqtt连接,不阻塞主流程,mqtt连接会阻塞,放到另外一个核心
This commit is contained in:
parent
2628d972a2
commit
3adea2fc5e
@ -15,6 +15,7 @@ FoodCube::FoodCube(char* userSsid, char* userPassword, char* userMqttServer, int
|
||||
voiceSerial = userVoiceSerial; //声音模块用的串口
|
||||
udpServerIP = userUdpServerIP; //云台相机ip
|
||||
udpServerPort = userUdpServerPort; //云台相机端口
|
||||
wificonnected=false;
|
||||
|
||||
//初始化飞控通讯串口 波特率
|
||||
switch (mavlinkSerial) { //初始化指定 串口号
|
||||
@ -113,6 +114,7 @@ void FoodCube::connectWifi() {
|
||||
WiFi.begin(ssid, password);
|
||||
//连接wifi
|
||||
logln("Connecting Wifi...");
|
||||
/*
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
log(".");
|
||||
delay(150);
|
||||
@ -131,15 +133,39 @@ void FoodCube::connectWifi() {
|
||||
logln(macAdd);
|
||||
playText("[v1]网络连接成功");
|
||||
delay(500);
|
||||
*/
|
||||
}
|
||||
|
||||
bool FoodCube::checkWiFiStatus()
|
||||
{
|
||||
if(!wificonnected&&(WiFi.status() == WL_CONNECTED))
|
||||
{
|
||||
wificonnected=true;
|
||||
//获取局域网ip
|
||||
logln("");
|
||||
logln("WiFi connected");
|
||||
log("IP address: ");
|
||||
logln(WiFi.localIP());
|
||||
localIp = WiFi.localIP();
|
||||
//设置开发板为无线终端 获取物理mac地址
|
||||
WiFi.mode(WIFI_STA);
|
||||
macAdd = WiFi.macAddress();
|
||||
macAdd.replace(":", ""); //板子的物理地址 并且去除冒号
|
||||
log("macAdd: ");
|
||||
logln(macAdd);
|
||||
playText("[v1]网络连接成功");
|
||||
}
|
||||
return wificonnected;
|
||||
}
|
||||
/**
|
||||
* @description: 连接mqtt
|
||||
* @param {String[]} topicSub 主题数组
|
||||
* @param {int} topicSubCount 数组总数
|
||||
*/
|
||||
void FoodCube::connectMqtt(String topicSub[], int topicSubCount) {
|
||||
if (mqttClient->connected()) return;
|
||||
/*尝试连接mqtt*/
|
||||
log("connect_mqtt");
|
||||
if (mqttClient->connect(macAdd.c_str(), mqttName, mqttPassword)) {
|
||||
logln("MQTT Server Connected.");
|
||||
log("Server Address: ");
|
||||
@ -156,7 +182,7 @@ void FoodCube::connectMqtt(String topicSub[], int topicSubCount) {
|
||||
//失败返回状态码
|
||||
log("MQTT Server Connect Failed. Client State:");
|
||||
logln(mqttClient->state());
|
||||
delay(3000);
|
||||
//delay(3000);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -68,6 +68,7 @@ public:
|
||||
void mav_channels_override(uint16_t chan[]);
|
||||
/*云台相机控制*/
|
||||
void udpSendToCamera(uint8_t* p_command, uint32_t len);
|
||||
bool checkWiFiStatus();
|
||||
|
||||
private:
|
||||
char* ssid; //wifi帐号
|
||||
@ -83,7 +84,7 @@ private:
|
||||
WiFiClient wifiClient; //网络客户端
|
||||
IPAddress localIp; //板子的IP地址
|
||||
String macAdd; //板子的物理地址(已去掉冒号分隔符)
|
||||
|
||||
bool wificonnected; //网络是否连接
|
||||
/*云台相机控制*/
|
||||
WiFiUDP udp; //udp信息操作对象
|
||||
char* udpServerIP; //云台相机ip地址
|
||||
|
10
src/main.cpp
10
src/main.cpp
@ -187,10 +187,10 @@ void setup()
|
||||
/////////////////////////////////MQTT_语音_MAVLINK 部分
|
||||
/*初始化*/
|
||||
Serial1.begin(115200, SERIAL_8N1, SERIAL_REPORT_RX, SERIAL_REPORT_TX); //声音模块引串口脚映射
|
||||
fc.playText("[v1]正在连接网络");
|
||||
fc.playText("[v1]开始启动");
|
||||
fc.connectWifi(); //连接wifi
|
||||
// fc.playText("正在连接服务器");
|
||||
fc.connectMqtt(topicSub, topicSubCount); //连接mqtt
|
||||
// fc.connectMqtt(topicSub, topicSubCount); //连接mqtt
|
||||
fc.mqttClient->setCallback(mqtt_receiveCallback); //设置订阅成功 回调
|
||||
fc.mav_request_data(); //指定飞控串口返回的数据类别(飞控启动之后发送才有意义)
|
||||
|
||||
@ -396,8 +396,6 @@ void loop()
|
||||
/////////////////////////////////MQTT_语音_MAVLINK 部分
|
||||
/*从飞控拿数据*/
|
||||
fc.comm_receive(mavlink_receiveCallback);
|
||||
/*保持mqtt心跳*/
|
||||
fc.mqttLoop(topicSub, topicSubCount);
|
||||
/////////////////////////////////MQTT_语音_MAVLINK 部分结束
|
||||
delay(1);
|
||||
|
||||
@ -424,7 +422,9 @@ void Task1(void *pvParameters)
|
||||
|
||||
/*保持mqtt心跳*/
|
||||
// fc.mqttLoop(topicSub, topicSubCount);
|
||||
|
||||
if (fc.checkWiFiStatus())
|
||||
/*保持mqtt心跳,如果Mqtt没有连接会自动连接*/
|
||||
fc.mqttLoop(topicSub, topicSubCount);
|
||||
vTaskDelay(10);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user