#include "WS_WIFI.h"
// The name and password of the WiFi access point
const char *ssid = APSSID;
const char *password = APPSK;
// IPAddress apIP(192, 168, 4, 1); // Set the IP address of the AP
char ipStr[16];
WebServer server(80);
void handleRoot() {
String myhtmlPage =
String("") +
""+
"
"+
" "+
""+
"";
server.send(200, "text/html", myhtmlPage);
// Serial.println("用户访问了主页");
printf("The user visited the home page\r\n");
}
void handleGetData() {
String json = "[";
for (int i = 0; i < sizeof(Relay_Flag) / sizeof(Relay_Flag[0]); i++) {
json += String(Relay_Flag[i]);
if (i < sizeof(Relay_Flag) / sizeof(Relay_Flag[0]) - 1) {
json += ",";
}
}
json += "]";
server.send(200, "application/json", json);
}
void handleSwitch(uint8_t ledNumber) {
uint8_t Data[1]={0};
Data[0]=ledNumber+48;
Relay_Analysis(Data,WIFI_Mode);
server.send(200, "text/plain", "OK");
}
void handleSwitch1() { handleSwitch(1); }
void handleSwitch2() { handleSwitch(2); }
void handleSwitch3() { handleSwitch(3); }
void handleSwitch4() { handleSwitch(4); }
void handleSwitch5() { handleSwitch(5); }
void handleSwitch6() { handleSwitch(6); }
void handleSwitch7() { handleSwitch(7); }
void handleSwitch8() { handleSwitch(8); }
void WIFI_Init()
{
WiFi.mode(WIFI_AP);
while(!WiFi.softAP(ssid, password)) {
printf("Soft AP creation failed.\r\n");
printf("Try setting up the WIFI again.\r\n");
}
// WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); // Set the IP address and gateway of the AP
delay(100);
RGB_Light(0, 60, 0);
delay(1000);
RGB_Light(0, 0, 0);
IPAddress myIP = WiFi.softAPIP();
uint32_t ipAddress = WiFi.softAPIP();
printf("AP IP address: ");
sprintf(ipStr, "%d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
printf("%s\r\n", ipStr);
server.on("/", handleRoot);
server.on("/getData", handleGetData);
server.on("/Switch1", handleSwitch1);
server.on("/Switch2", handleSwitch2);
server.on("/Switch3", handleSwitch3);
server.on("/Switch4", handleSwitch4);
server.on("/Switch5", handleSwitch5);
server.on("/Switch6", handleSwitch6);
server.on("/AllOn" , handleSwitch7);
server.on("/AllOff" , handleSwitch8);
server.begin();
printf("Web server started\r\n");
}
void WIFI_Loop()
{
server.handleClient(); // Processing requests from clients
}