Commit a80c5b31 authored by 学习的菜鸟's avatar 学习的菜鸟

加入了WiFi切换和失败切回,测试几次通过

parent 6d565a90
No preview for this file type
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
pthread_t thread[6]; // 全局线程句柄数组(或传参) pthread_t thread[6]; // 全局线程句柄数组(或传参)
int gwebrtc_index=0;
int grc=0; int grc=0;
void* args[6] = { NULL, NULL, NULL, NULL, NULL, NULL }; void* args[6] = { NULL, NULL, NULL, NULL, NULL, NULL };
...@@ -40,7 +42,8 @@ int thread_start(ThreadFunc AppExit, ThreadFunc Mqttbeat, ...@@ -40,7 +42,8 @@ int thread_start(ThreadFunc AppExit, ThreadFunc Mqttbeat,
return 0; return 0;
} }
void *AppExit(void *arg) { //出现意外自动停止 //出现意外自动停止
void *AppExit(void *arg) {
while(1){ while(1){
Delay_Ms(0,100); Delay_Ms(0,100);
gPwmCount++; gPwmCount++;
...@@ -63,6 +66,7 @@ void *Mqttbeat(void *arg) { ...@@ -63,6 +66,7 @@ void *Mqttbeat(void *arg) {
my_zlog_info("Mqttbeat start"); my_zlog_info("Mqttbeat start");
Delay_Ms(5,0); Delay_Ms(5,0);
wifi_change_sendmqtt_init(); wifi_change_sendmqtt_init();
gwebrtc_index=1;
while(1) { while(1) {
if( AppExit_pin_pwm == 202) Delay_Ms(0,45); if( AppExit_pin_pwm == 202) Delay_Ms(0,45);
if( AppExit_pin_pwm != 202) Delay_Ms(0,100); if( AppExit_pin_pwm != 202) Delay_Ms(0,100);
...@@ -79,12 +83,19 @@ void *Mqttbeat(void *arg) { ...@@ -79,12 +83,19 @@ void *Mqttbeat(void *arg) {
void *opensh(void *arg) { void *opensh(void *arg) {
Delay_Ms(5,0); Delay_Ms(5,0);
//printf("open cam\n"); //printf("open cam\n");
my_zlog_info("open cam"); while(1){
opencamsh();//10s后打开游览器并且进入网址 if(gwebrtc_index==1) {
my_zlog_info("open cam");
opencamsh();//10s后打开游览器并且进入网址
}
}
return NULL; return NULL;
} }
void *Mqtt_onnect(void *arg) {//mqtt异常处理,断开自动重连,简单粗暴 //mqtt异常处理,断开自动重连,简单粗暴
void *Mqtt_onnect(void *arg) {
Delay_Ms(5,0); Delay_Ms(5,0);
if (mqtt_init() != 0) { if (mqtt_init() != 0) {
...@@ -120,7 +131,8 @@ void *Mqtt_onnect(void *arg) {//mqtt异常处理,断开自动重连,简单 ...@@ -120,7 +131,8 @@ void *Mqtt_onnect(void *arg) {//mqtt异常处理,断开自动重连,简单
return NULL; return NULL;
} }
void *delay_count(void *arg) {//专门用于计时的线程 //专门用于计时的线程
void *delay_count(void *arg) {
while (1) { while (1) {
Delay_Ms(0,5); Delay_Ms(0,5);
device_delay_count++; device_delay_count++;
...@@ -129,7 +141,8 @@ void *delay_count(void *arg) {//专门用于计时的线程 ...@@ -129,7 +141,8 @@ void *delay_count(void *arg) {//专门用于计时的线程
return NULL; return NULL;
} }
void *play_mp3_thread(void* arg) {//专门处理MP3播放 //专门处理MP3播放
void *play_mp3_thread(void* arg) {
audioplay_cycle(); audioplay_cycle();
return NULL; return NULL;
} }
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include "judg.h" #include "judg.h"
char current_ssid[SSID_MAX_LEN] = {0};//用于存现在已连接WiFi char current_ssid[SSID_MAX_LEN] = {0};//用于存现在已连接WiFi
char wifi_Last_ssid[SSID_MAX_LEN]= {0} ;
char wifi_Last_password[SSID_MAX_LEN]= {0} ;
//获取当前连接wifi //获取当前连接wifi
void get_current_wifi() { void get_current_wifi() {
...@@ -170,7 +172,7 @@ int connect_wifi_with_fallback(const char* ssid, const char* password, const cha ...@@ -170,7 +172,7 @@ int connect_wifi_with_fallback(const char* ssid, const char* password, const cha
} }
//改变WiFi连接 //改变WiFi连接
int change_wifi_connect(const char *wifi_ssid,const char *wifi_password) { int change_wifi_connect(const char *wifi_ssid,const char *wifi_password) {
get_current_wifi(current_ssid, sizeof(current_ssid)); get_current_wifi();
int ret = connect_wifi_with_fallback(wifi_ssid, wifi_password, current_ssid); int ret = connect_wifi_with_fallback(wifi_ssid, wifi_password, current_ssid);
my_zlog_notice("返回代码:%d", ret); my_zlog_notice("返回代码:%d", ret);
return ret; return ret;
...@@ -300,6 +302,35 @@ void delete_wifi_conf() { ...@@ -300,6 +302,35 @@ void delete_wifi_conf() {
closedir(dir); closedir(dir);
} }
//提取WiFi名称和密码
int extract_wifi_file(){
FILE *fp = fopen(WIFI_CONF_PATH, "r");
if (fp == NULL) {
perror("无法打开配置文件");
return -1;
}
char line[256];
while (fgets(line, sizeof(line), fp)) {
// 去除换行符
line[strcspn(line, "\r\n")] = 0;
if (strncmp(line, "SSID=", 5) == 0) {
strncpy(wifi_Last_ssid, line + 5, sizeof(wifi_Last_ssid) - 1);
} else if (strncmp(line, "PASSWORD=", 9) == 0) {
strncpy(wifi_Last_password, line + 9, sizeof(wifi_Last_password) - 1);
}
}
fclose(fp);
my_zlog_debug("SSID: %s", wifi_Last_ssid);
my_zlog_debug("PASSWORD: %s", wifi_Last_password);
return 0;
}
// 从文件读取 SSID,比较当前连接的 WiFi 名称 // 从文件读取 SSID,比较当前连接的 WiFi 名称
int compare_ssid_with_file() { int compare_ssid_with_file() {
char file_ssid[128] = {0}; char file_ssid[128] = {0};
...@@ -333,8 +364,8 @@ int compare_ssid_with_file() { ...@@ -333,8 +364,8 @@ int compare_ssid_with_file() {
} }
} }
/*当wifi_status为1为修改成功,2是继续使用正常wifi,3是恢复到默认jking,4为wifi不存在 /*当wifi_status为0为连接jking,当wifi_status为1为修改成功,2是继续使用正常wifi,3是恢复到默认jking,4为wifi不存在
,5为密码错误,6为未知错误,7为回退失败*/ ,5为密码错误,6为未知错误,7为回退失败,8为没网切回默认wifi*/
void wifichange_sendmqtt(int wifi_status) { //改WiFi发送 void wifichange_sendmqtt(int wifi_status) { //改WiFi发送
topic_middle_value();//指针中间函数 topic_middle_value();//指针中间函数
...@@ -365,23 +396,68 @@ void connect_wifi_jking() { ...@@ -365,23 +396,68 @@ void connect_wifi_jking() {
int ret=change_wifi_connect(default_SSID,default_password); int ret=change_wifi_connect(default_SSID,default_password);
Delay_Ms(15,0); Delay_Ms(15,0);
if(can_access_internet()==0) { if(can_access_internet()==0) {
//system("sudo reboot");//重启香橙派 system("sudo reboot");//重启香橙派
my_zlog_debug("重启成功"); my_zlog_debug("重启成功");
} }
} }
int wifi_change_sendmqtt_init(){//wifi改变初始化,放在main开头,必须要等mqtt直播就绪才行 //wifi改变初始化,放在main开头,必须要等mqtt直播就绪才行
int wifi_change_sendmqtt_init(){
int wififile_fd = compare_ssid_with_file(); int wififile_fd = compare_ssid_with_file();
if(wififile_fd==0){ if(wififile_fd==-1){
wifichange_sendmqtt(1); if(can_access_internet()!=0){
delete_wifi_conf(); get_current_wifi();
if(strcmp(default_SSID,current_ssid)==0){
my_zlog_debug("没有更改WiFi");
return 0;
}
check_or_create_wifi_conf();
write_wifi_conf();
change_wifi_connect(default_SSID,default_password);
delete_wifi_by_ssid(current_ssid);
}
if(strcmp(default_SSID,current_ssid)==0) wifichange_sendmqtt(0);
}else if(wififile_fd==1){
get_current_wifi();
if(strcmp(default_SSID,current_ssid)==0){
extract_wifi_file();
if(change_wifi_connect(wifi_Last_ssid,wifi_Last_password)==0){
Delay_Ms(15,0);
if(can_access_internet()==0){
delete_wifi_by_ssid(current_ssid);
delete_wifi_conf();
my_zlog_debug("成功回朔到上次WiFi");
}else {
change_wifi_connect(default_SSID,default_password);
my_zlog_debug("成功回朔到上次WiFi但没网回到默认wifi");
}
}else {
change_wifi_connect(default_SSID,default_password);
my_zlog_debug("说明不止一次更换wifi过");
}
}else{
if(can_access_internet()==0){
wifichange_sendmqtt(1);
my_zlog_debug("wifi更改成功");
delete_wifi_conf();
}else {
if(change_wifi_connect(default_SSID,default_password)==0)delete_wifi_conf();
my_zlog_debug("WiFi无网切回");
Delay_Ms(10,0);
}
} else if(wififile_fd==1){ }
} else if(wififile_fd==0){
if(can_access_internet()==0) { if(can_access_internet()==0) {
wifichange_sendmqtt(2);//2是继续使用正常wifi wifichange_sendmqtt(2);//2是继续使用正常wifi
my_zlog_debug("wifi更改失败,继续使用原WiFi,可使用查询指令查询");
delete_wifi_conf(); delete_wifi_conf();
} else { } else {//不需要删除文件,不同回删除conf源文件,相同也会删除
wifichange_sendmqtt(3);//3是恢复到默认jking wifichange_sendmqtt(3);//3是恢复到默认jking
my_zlog_debug("在WiFi更改失败情况下使用默认连接jking");
delete_wifi_conf(); delete_wifi_conf();
connect_wifi_jking(); connect_wifi_jking();
} }
...@@ -400,12 +476,27 @@ void wifi_change_recmqtt(cJSON *body){ ...@@ -400,12 +476,27 @@ void wifi_change_recmqtt(cJSON *body){
if(check_or_create_wifi_conf() == 0) my_zlog_debug("文件已创建"); if(check_or_create_wifi_conf() == 0) my_zlog_debug("文件已创建");
if(write_wifi_conf() == 0) my_zlog_debug("写入成功"); if(write_wifi_conf() == 0) my_zlog_debug("写入成功");
int ret = change_wifi_connect(ssid,password); int ret = change_wifi_connect(ssid,password);
Delay_Ms(15,0); Delay_Ms(15,0);
if(can_access_internet()==0&&ret == 0){ if(strcmp(default_SSID,ssid)==0){
delete_wifi_by_ssid(current_ssid); delete_wifi_by_ssid(current_ssid);
my_zlog_debug("ssid:%S",current_ssid); my_zlog_debug("ssid:%S",current_ssid);
//system("sudo reboot");//重启香橙派 delete_wifi_conf();
system("sudo reboot");//重启香橙派
my_zlog_debug("重启成功"); my_zlog_debug("重启成功");
}
if(ret == 0){
if(can_access_internet()==0){
delete_wifi_by_ssid(current_ssid);
my_zlog_debug("ssid:%S",current_ssid);
system("sudo reboot");//重启香橙派
my_zlog_debug("重启成功");
}else {
delete_wifi_conf();
if(change_wifi_connect(default_SSID,default_password)==0) delete_wifi_by_ssid(current_ssid);
my_zlog_debug("无网切回默认WiFi");
Delay_Ms(15,0);
wifichange_sendmqtt(8);
}
} }
if(ret == -1) { if(ret == -1) {
wifichange_sendmqtt(4); wifichange_sendmqtt(4);
...@@ -415,7 +506,7 @@ void wifi_change_recmqtt(cJSON *body){ ...@@ -415,7 +506,7 @@ void wifi_change_recmqtt(cJSON *body){
wifichange_sendmqtt(6); wifichange_sendmqtt(6);
}else if(ret == -4 || ret == -5){ }else if(ret == -4 || ret == -5){
wifichange_sendmqtt(7); wifichange_sendmqtt(7);
//system("sudo reboot");//重启香橙派 system("sudo reboot");//重启香橙派
my_zlog_debug("重启成功"); my_zlog_debug("重启成功");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment