#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
#include <FS.h>
ESP8266WiFiMulti wifiMulti;
ESP8266WebServer esp8266_server(80);
bool pinState;
void setup(void){
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
IPAddress local_IP(192, 168, 0, 166);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
WiFi.config(local_IP, gateway, subnet);
WiFi.mode(WIFI_STA);
wifiMulti.addAP("B902", "meiyoumima123456");
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
Serial.println("Connecting ...");
int i = 0;
while (wifiMulti.run() != WL_CONNECTED) {
delay(1000);
Serial.print(i++); Serial.print(' ');
}
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID());
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
if(SPIFFS.begin()){
Serial.println("SPIFFS Started.");
} else {
Serial.println("SPIFFS Failed to Start.");
}
esp8266_server.enableCORS(true);
esp8266_server.begin();
esp8266_server.on("/setled", HTTP_GET, setHandleLED);
esp8266_server.on("/getled", HTTP_GET, getHandleLED);
esp8266_server.onNotFound(handleUserRequet);
Serial.println("HTTP esp8266_server started");
}
void loop(void){
esp8266_server.handleClient();
pinState = digitalRead(LED_BUILTIN);
}
void handleUserRequet() {
String webAddress = esp8266_server.uri();
bool fileReadOK = handleFileRead(webAddress);
if (!fileReadOK){
esp8266_server.send(404, "text/plain", "404 Not Found");
}
}
bool handleFileRead(String path) {
if (path.endsWith("/")) {
path = "/index.html";
}
String contentType = getContentType(path);
if (SPIFFS.exists(path)) {
File file = SPIFFS.open(path, "r");
esp8266_server.streamFile(file, contentType);
file.close();
return true;
}
return false;
}
String getContentType(String filename){
if(filename.endsWith(".htm")) return "text/html";
else if(filename.endsWith(".html")) return "text/html";
else if(filename.endsWith(".css")) return "text/css";
else if(filename.endsWith(".js")) return "application/javascript";
else if(filename.endsWith(".png")) return "image/png";
else if(filename.endsWith(".gif")) return "image/gif";
else if(filename.endsWith(".jpg")) return "image/jpeg";
else if(filename.endsWith(".ico")) return "image/x-icon";
else if(filename.endsWith(".xml")) return "text/xml";
else if(filename.endsWith(".pdf")) return "application/x-pdf";
else if(filename.endsWith(".zip")) return "application/x-zip";
else if(filename.endsWith(".gz")) return "application/x-gzip";
return "text/plain";
}
void setHandleLED() {
digitalWrite(LED_BUILTIN,!digitalRead(LED_BUILTIN));
pinState = digitalRead(LED_BUILTIN);
if(pinState)
{
esp8266_server.send(200, "text/plain", "OFF");
}else
{
esp8266_server.send(200, "text/plain", "OPEN");
}
}
void getHandleLED() {
pinState = digitalRead(LED_BUILTIN);
if(pinState)
{
esp8266_server.send(200, "text/plain", "OFF");
}else
{
esp8266_server.send(200, "text/plain", "OPEN");
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<style>
.item_form {
margin-top: 5%;
margin-left: 45%;
float: left;
}
.item_led {
margin-top: 5%;
margin-left: 20px;
float: left;
border-radius: 100%;
width: 30px;
height: 30px;
}
</style>
</head>
<body bgcolor="rgb(57,e6,de)">
<button id="ledButton" class='item_form' style="float:left">开关按钮</button>
<div id="led" class='item_led' style='background-color: #FF0000;'></div>
</body>
<script>
setInterval(
function name(params) {
$.get("http://192.168.0.166:80/getled", function (data, status) {
if (data == "OFF") {
document.getElementById('led').style = "background-color: #FF0000;"
console.log(1);
} else {
document.getElementById('led').style = "background-color: #00FF00;"
console.log(0);
}
});
},3*1000);
$("#ledButton").click(function () {
$.get("http://192.168.0.166:80/setled", function (data, status) {
if (data == "OFF") {
document.getElementById('led').style = "background-color: #FF0000;"
console.log(1);
} else {
document.getElementById('led').style = "background-color: #00FF00;"
console.log(0);
}
});
});
</script>
</html>