diff --git a/src/TJC_Show.cpp b/src/TJC_Show.cpp index 0a7ba17..724547e 100644 --- a/src/TJC_Show.cpp +++ b/src/TJC_Show.cpp @@ -2,6 +2,7 @@ #include "WiFiControl.h" #include "LightControl.h" + extern RunTime runtime; extern DS3502 ds3502; extern BH1750 bh1750_a, bh1750_b; @@ -34,34 +35,28 @@ void TJC_Show::init() { goToPage("start"); // 跳转到开机页面 } + void TJC_Show::showQR() { char addr[64]; IPAddress ip; - #if SELECTED_WIFI_MODE == WIFI_MODE_STA - ip = WiFi.localIP(); - snprintf(addr, sizeof(addr), "wifi.t0.txt=\"STA:%d.%d.%d.%d\"\xff\xff\xff", ip[0], ip[1], ip[2], ip[3]); - sendCommand(addr); - #elif SELECTED_WIFI_MODE == WIFI_MODE_AP + if (WiFi.getMode() == WIFI_AP) { // AP模式 ip = WiFi.softAPIP(); snprintf(addr, sizeof(addr), "wifi.t0.txt=\"AP:%d.%d.%d.%d\"\xff\xff\xff", ip[0], ip[1], ip[2], ip[3]); sendCommand(addr); - #endif + Serial.printf("wifi.t0.txt=\"AP:%d.%d.%d.%d\"\xff\xff\xff", ip[0], ip[1], ip[2], ip[3]); + } else if (WiFi.getMode() == WIFI_STA) { // STA模式 + ip = WiFi.localIP(); + snprintf(addr, sizeof(addr), "wifi.t0.txt=\"STA:%d.%d.%d.%d\"\xff\xff\xff", ip[0], ip[1], ip[2], ip[3]); + sendCommand(addr); + Serial.printf("wifi.t0.txt=\"STA:%d.%d.%d.%d\"\xff\xff\xff", ip[0], ip[1], ip[2], ip[3]); + } snprintf(addr, sizeof(addr), "wifi.qr0.txt=\"http://%d.%d.%d.%d/\"\xff\xff\xff", ip[0], ip[1], ip[2], ip[3]); sendCommand(addr); - - // if (SELECTED_WIFI_MODE == WIFI_MODE_STA){ - // snprintf(addr, sizeof(addr), "t15.txt=\"STA:%d.%d.%d.%d\"\xff\xff\xff", ip[0], ip[1], ip[2], ip[3]); - // sendCommand(addr); - // } - // if(SELECTED_WIFI_MODE == WIFI_MODE_AP){ - // snprintf(addr, sizeof(addr), "t15.txt=\"AP:%d.%d.%d.%d\"\xff\xff\xff", ip[0], ip[1], ip[2], ip[3]); - // sendCommand(addr); - // } - } + void TJC_Show::showInfo() { char str[256]; @@ -119,19 +114,23 @@ void TJC_Show::showInfo() { sendCommand(str); // === wifi 页面信息 === + char addr[64]; IPAddress ip; - #if SELECTED_WIFI_MODE == WIFI_MODE_STA - ip = WiFi.localIP(); - #elif SELECTED_WIFI_MODE == WIFI_MODE_AP - ip = WiFi.softAPIP(); - #endif - snprintf(str, sizeof(str), - "wifi.t0.txt=\"STA:%d.%d.%d.%d\"\xff\xff\xff" - "wifi.qr0.txt=\"http://%d.%d.%d.%d/\"\xff\xff\xff", - ip[0], ip[1], ip[2], ip[3], - ip[0], ip[1], ip[2], ip[3]); - sendCommand(str); + if (WiFi.getMode() == WIFI_AP) { // AP模式 + ip = WiFi.softAPIP(); + snprintf(addr, sizeof(addr), "wifi.t0.txt=\"AP:%d.%d.%d.%d\"\xff\xff\xff", ip[0], ip[1], ip[2], ip[3]); + sendCommand(addr); + //Serial.printf("wifi.t0.txt=\"AP:%d.%d.%d.%d\"\xff\xff\xff", ip[0], ip[1], ip[2], ip[3]); + } else if (WiFi.getMode() == WIFI_STA) { // STA模式 + ip = WiFi.localIP(); + snprintf(addr, sizeof(addr), "wifi.t0.txt=\"STA:%d.%d.%d.%d\"\xff\xff\xff", ip[0], ip[1], ip[2], ip[3]); + sendCommand(addr); + //Serial.printf("wifi.t0.txt=\"STA:%d.%d.%d.%d\"\xff\xff\xff", ip[0], ip[1], ip[2], ip[3]); + } + + snprintf(addr, sizeof(addr), "wifi.qr0.txt=\"http://%d.%d.%d.%d/\"\xff\xff\xff", ip[0], ip[1], ip[2], ip[3]); + sendCommand(addr); } // void TJC_Show::processSerial() { // #define FRAME_LENGTH 10 diff --git a/src/WiFiControl.cpp b/src/WiFiControl.cpp index 3bb4767..8abbf41 100644 --- a/src/WiFiControl.cpp +++ b/src/WiFiControl.cpp @@ -1,71 +1,84 @@ #include "WiFiControl.h" +#include +#include -const char* STA_SSIDS[STA_WIFI_COUNT] = {"SERVIRST-CT", "IRIS"}; -const char* STA_PASSWORDS[STA_WIFI_COUNT] = {"servirst8888", "irishk*******"}; +// AP 模式下的 SSID 和密码 +const char* AP_SSID = "LampController_AP"; +const char* AP_PASSWORD = "12345678"; + +// 存储从手机接收到的 WiFi 凭证 +char user_ssid[64] = ""; +char user_pass[64] = ""; -void WiFi_Init() -{ - #if SELECTED_WIFI_MODE == WIFI_MODE_STA - if (!connectToKnownNetworks()) { - switchToAPMode(); +//extern AsyncWebServer server(80); // 创建 Web 服务器实例 + +// 简单的 HTML 表单页面 +const char html_form[] PROGMEM = R"rawliteral( + + + + WiFi Configuration + + + +
+

Connect to WiFi

+
+ + -bool connectToKnownNetworks() + + + + +
+
+ + +)rawliteral"; + +void WiFi_Init() { - Serial.println("Scanning nearby networks..."); - - int numNetworks = WiFi.scanNetworks(); - if (numNetworks == 0) { - Serial.println("No networks found."); - return false; - } - - Serial.print("Found "); - Serial.print(numNetworks); - Serial.println(" networks."); - - for (int i = 0; i < numNetworks; ++i) { - String scannedSSID = WiFi.SSID(i); - - for (int j = 0; j < STA_WIFI_COUNT; ++j) { - if (scannedSSID == STA_SSIDS[j]) { - Serial.print("Found known network: "); - Serial.println(scannedSSID); - - WiFi.begin(STA_SSIDS[j], STA_PASSWORDS[j]); - unsigned long startTime = millis(); - - while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) { - delay(500); - Serial.print("."); - } - - if (WiFi.status() == WL_CONNECTED) { - Serial.println("\nConnected to WiFi"); - Serial.print("IP Address: "); - Serial.println(WiFi.localIP()); - return true; - } else { - Serial.println("Failed to connect."); - } - } - } - } - - Serial.println("Could not connect to any known network."); - return false; -} - -void switchToAPMode() -{ - SELECTED_WIFI_MODE == WIFI_MODE_AP; - Serial.println("Switching to AP mode."); + // 默认启动时设置为 AP 模式 + WiFi.mode(WIFI_AP); WiFi.softAP(AP_SSID, AP_PASSWORD); + + Serial.println("AP Mode started"); Serial.print("AP IP address: "); Serial.println(WiFi.softAPIP()); + +} + + +void connectToWiFi() +{ + WiFi.mode(WIFI_STA); // 切换为 Station 模式 + WiFi.begin(user_ssid, user_pass); + + Serial.println("Connecting to WiFi..."); + + unsigned long startTime = millis(); + while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) { + delay(500); + Serial.print("."); + } + + if (WiFi.status() == WL_CONNECTED) { + Serial.println("\nConnected to WiFi"); + Serial.print("IP Address: "); + Serial.println(WiFi.localIP()); + } else { + Serial.println("Failed to connect to WiFi."); + WiFi_Init(); // 连接失败返回 AP 模式 + } } \ No newline at end of file diff --git a/src/WiFiControl.h b/src/WiFiControl.h index d03e567..6420192 100644 --- a/src/WiFiControl.h +++ b/src/WiFiControl.h @@ -1,51 +1,10 @@ -// #include - -// #define WIFI_MODE_STA 1 -// #define WIFI_MODE_AP 2 - -// // 选择WiFi模式,取消注释需要的模式 -// #define SELECTED_WIFI_MODE WIFI_MODE_STA -// //#define SELECTED_WIFI_MODE WIFI_MODE_AP - - -// // STA模式下的SSID和密码 -// // #define STA_SSID "SERVIRST-CT" -// // #define STA_PASSWORD "servirst8888" -// #define STA_SSID "IRIS" -// #define STA_PASSWORD "irishk*******" - -// // AP模式下的SSID和密码 -// #define AP_SSID "APSSID" -// #define AP_PASSWORD "APPassword" - -// void WiFi_Init(void); - -// WiFiControl.h #ifndef WIFICONTROL_H #define WIFICONTROL_H -#include - -#define WIFI_MODE_STA 0 -#define WIFI_MODE_AP 1 - -// 默认选择WiFi模式 -#define SELECTED_WIFI_MODE WIFI_MODE_STA -//#define SELECTED_WIFI_MODE WIFI_MODE_AP - -// 设置多个 Wi-Fi 账号密码 -#define STA_WIFI_COUNT 2 -extern const char* STA_SSIDS[STA_WIFI_COUNT]; -extern const char* STA_PASSWORDS[STA_WIFI_COUNT]; - -// AP模式下的SSID和密码 -#define AP_SSID "APSSID" -#define AP_PASSWORD "APPassword" - -void WiFi_Init(void); - -bool connectToKnownNetworks(void); -void switchToAPMode(void); +#include +void WiFi_Init(); +void connectToWiFi(); +void setupWebServer(); #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 3ca2490..f719f2c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,7 +18,9 @@ #include // #include - +// 全局变量用于存储 WiFi 凭据 +extern char user_ssid[64]; // 存储 SSID +extern char user_pass[64]; // 存储密码 // Ticker 用于定时检查电流状态 Ticker currentCheckTicker; @@ -166,9 +168,41 @@ void WebServer_Init(void) { request->send_P(200, "text/html", index_html); }); + + // /*************************** AP配网 ***************** */ + server.on("/config", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send_P(200, "text/html", config_html); + }); + + + server.on("/saveWiFi", HTTP_POST, [](AsyncWebServerRequest *request){ + if (request->hasParam("ssid", true) && request->hasParam("password", true)) { + // 获取 SSID 和密码 + String ssid = request->getParam("ssid", true)->value(); + String password = request->getParam("password", true)->value(); + + // 存储到全局变量 + ssid.toCharArray(user_ssid, sizeof(user_ssid)); + password.toCharArray(user_pass, sizeof(user_pass)); + + Serial.println("Received WiFi credentials:"); + Serial.print("SSID: "); + Serial.println(user_ssid); + Serial.print("Password: "); + Serial.println(user_pass); + + connectToWiFi(); // 连接目标 WiFi + } + + // 返回确认页面 + String response = "

Configuration Received

Please wait while the device connects to WiFi...

"; + request->send(200, "text/html", response); + }); + // 合并数据接口 server.on("/data", HTTP_GET, [](AsyncWebServerRequest *request){ - StaticJsonDocument<256> doc; + //StaticJsonDocument<256> doc; + JsonDocument doc; doc["temperature"] = sensorData.temperature; doc["humidity"] = sensorData.humidity; doc["lightA"] = sensorData.lightA; diff --git a/src/webpages.h b/src/webpages.h index 229fef6..b796112 100644 --- a/src/webpages.h +++ b/src/webpages.h @@ -10,47 +10,38 @@ // ESP32 Data Display // // // // //

实时数据显示

+ //

累计使用时长: 加载中...

// + //

温度: 加载中... °C

+//

湿度: 加载中... %RH

//

光照强度(A): 加载中... lx

//

光照强度(B): 加载中... lx

//

电位器Wiper值: 加载中...

//

总线电压: 加载中... V

//

分流电压: 加载中... mV

-//

电流: 加载中... mA

-//

功率: 加载中... mW

+//

电流: 加载中... A

+//

功率: 加载中... W

+ //

设置Wiper值

-// +// // + +//

灯光控制

+//

灯光状态: 加载中...

+// +// + +//

自动调节

+//

状态: 加载中...

+//

目标照度: lux

+// +// // // -// )rawliteral"; // 添加分号 +// )rawliteral"; // #endif // WEBPAGES_H - #ifndef WEBPAGES_H #define WEBPAGES_H @@ -254,4 +310,45 @@ const char index_html[] PROGMEM = R"rawliteral( )rawliteral"; +// 新增 config_html 页面 +const char config_html[] PROGMEM = R"rawliteral( + + + + ESP32 WiFi Setup + + + + +

WiFi 设置

+
+
+

+ +
+

+ + +
+ + +)rawliteral"; + #endif // WEBPAGES_H \ No newline at end of file