我在ESP8266上运行的Web服务器无法正常工作。
如果我请求某些页面服务器返回空或未完成的响应。
HTML页面将不会加载,它将始终以白屏加载。我试图在浏览器中使用检查元素功能检查页面,但发现页面中仅head
,而body
丢失了。有时HTML可以正确加载但没有CSS或JS,并且在控制台中出现错误Failed to load resource: net::ERR_EMPTY_RESPONSE
。
有时服务器根本没有响应。浏览器显示错误Cannot reach host
。
我不得不提到,这是在重置我的ESP一段时间后发生的,例如,这对我来说是正常的,但对于其他网络上的其他人而言,它正常工作。或者,如果我通过VPN(某些随机的Chrome Estension我正在使用Touch VPN)连接到代理服务器,则效果很好。
几天来我都在尝试解决此问题,但没有找到任何有效的解决方案。我已将资源存储在包含的*.h
文件中。
这是代码:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ESP8266mDNS.h>
#include "console.h"
/*#include "mines.h"*/
#include "tictactoe.h"
#include "protools.h"
#include "main.h"
#include "translator.h"
#define L_GREEN 2
#define L_YELLOW 0
#define L_ORANGE 4
#define L_RED 5
const char* ssid = "***";
const char* password = "***";
int requests = 0;
String IPs = "[";
IPAddress staticIP86_10(192,168,0,115);
IPAddress gateway86_10(192,168,0,1);
IPAddress subnet86_10(255,255,255,0);
ESP8266WebServer server(80); //Server on port 80
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "1.sk.pool.ntp.org", 60*60, 60*1000);
//===============================================================
// Game Data
//===============================================================
/*String ticPlayer1 = "";
String ticPlayer2 = "";
String ticClick = "-1";
int ticReset = 0;*/
//===============================================================
// LEDs
//===============================================================
void LEDblink(int pin) {
requests++;
digitalWrite(pin, HIGH);
delay(10);
digitalWrite(pin, LOW);
}
//===============================================================
// This routine is executed when you open its IP in browser
//===============================================================
void handleNotFound(){
if(server.uri().indexOf(".php")>-1) {
Serial.print("404 "+server.uri()+": "+server.client().remoteIP().toString()+" (Blocked)\n");
return;
}
Serial.print("404 "+server.uri()+": "+server.client().remoteIP().toString()+"\n");
String s = HTML_Pro_notFound;
server.send(404, "text/html", s);
//String c = "{\"uptime\":"+String(millis(),DEC)+",\"req\":"+String(requests)+",\"ips\":"+IPs+"]}";
//server.send(200, "text/plane", c);
LEDblink(L_YELLOW);
}
void proMainCSS(){
String s = CSS_Pro_main;
server.send(200, "text/css", s);
LEDblink(L_ORANGE);
}
void handleRoot(){
Serial.print("Root: "+server.client().remoteIP().toString()+"\n");
String s = HTML_Pro_page;
server.send(200, "text/html", s);
LEDblink(L_YELLOW);
}
void proPageCSS(){
String s = CSS_Pro_page;
server.send(200, "text/css", s);
LEDblink(L_ORANGE);
}
void proPageJS(){
String ip = server.client().remoteIP().toString();
if(IPs=="["){
IPs+="{\"time\":"+String(timeClient.getEpochTime(),DEC)+",\"ip\":\""+ip+"\"}";
}else{
IPs+=",{\"time\":"+String(timeClient.getEpochTime(),DEC)+",\"ip\":\""+ip+"\"}";
}
String s = JS_Pro_page;
server.send(200, "text/javascript", s);
LEDblink(L_ORANGE);
}
//---
void proTranslatorHTML() {
Serial.print("Translator: "+server.client().remoteIP().toString()+"\n");
String s = HTML_Pro_translator;
server.send(200, "text/html", s);
LEDblink(L_YELLOW);
}
void proTranslatorCSS() {
String s = CSS_Pro_translator;
server.send(200, "text/css", s);
LEDblink(L_ORANGE);
}
void proTranslatorJS() {
String s = JS_Pro_translator;
server.send(200, "text/javascript", s);
LEDblink(L_ORANGE);
}
//---
//===============================================================
void reboot() {
server.send(200, "text/plane", "true");
LEDblink(L_RED);
digitalWrite(15, LOW);
digitalWrite(0, HIGH);
digitalWrite(2, HIGH);
ESP.restart();
}
void proLoginHTML() {
Serial.print("Login: "+server.client().remoteIP().toString()+"\n");
String c = HTML_Pro_login;
server.send(200, "text/html", c);
LEDblink(L_YELLOW);
}
void proLoginCSS() {
String c = CSS_Pro_login;
server.send(200, "text/css", c);
LEDblink(L_ORANGE);
}
void admin() {
if(!server.hasArg("user") || !server.hasArg("pass") || server.arg("user") == NULL || server.arg("pass") == NULL || server.arg("request")=="login") {
String expire = "";
if(server.arg("remember")=="on"){expire="30";}else{expire="0";}
server.send(200, "text/html", "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>ProTools.tk | Please Wait</title><link rel='stylesheet' type='text/css' href='/main.css'><script src='/cookies.js'></script></head><body style='background:#2d2d2d'><form class='center' action='/admin' method='post' style='display:none;'><input class='a' type='text' name='user'><input class='a' type='password' name='pass'><input class='a' type='text' name='request' value='panel'><button class='a' type='submit'></button></form><div class='loading center' style='margin-top:150px'></div><script type='text/javascript'>var u = '"+server.arg("user")+"';var p = '"+server.arg("pass")+"';if(u.length > 7 && p.length > 7) {delCookie('user');delCookie('pass');setCookie('user', u, "+expire+");setCookie('pass', p, "+expire+");}if(getCookie('user') && getCookie('user')) {var a=document.getElementsByClassName('a');a[0].value=getCookie('user');a[1].value=getCookie('pass');a[3].click();}else{location.replace('/login');}</script></body></html>");
//server.sendHeader("Location","/login");
//server.send(303);
}
if((server.arg("user")=="***")&&(server.arg("pass")=="***")) {
if(server.arg("request")=="data") {
String c = "{\"uptime\":"+String(millis(),DEC)+",\"req\":"+String(requests)+",\"ips\":"+IPs+"]}";
server.send(200, "text/json", c);
}
else if(server.arg("request")=="panel") {
server.send(200, "text/plane", "Admin panel here");
}
else {
server.send(400, "text/plane", "400 Bad Request");
}
}
else{
server.sendHeader("Location","/login");
server.send(303);
}
LEDblink(L_RED);
}
void proCookieJS() {
String s = JS_Pro_cookie;
server.send(200, "text/javascript", s);
LEDblink(L_ORANGE);
}
void console() {
Serial.print("Console :"+server.client().remoteIP().toString()+"\n");
String s = HTML_console;
server.send(200, "text/html", s);
LEDblink(L_ORANGE);
}
void buspage() {
String s = HTML_buspage;
server.send(200, "text/html", s);
LEDblink(L_ORANGE);
}
//===============================================================
/*void minesPage() {
String s = HTML_mines;
server.send(200, "text/html", s);
LEDblink(L_YELLOW);
}
void minesScript() {
String s = JS_mines;
server.send(200, "text/javascript", s);
LEDblink(L_YELLOW);
}
void minesStyle() {
String s = CSS_mines;
server.send(200, "text/css", s);
LEDblink(L_YELLOW);
}*/
//===============================================================
/*void ticPage() {
String s = HTML_tictactoe;
server.send(200, "text/html", s);
LEDblink(L_YELLOW);
}
void ticScript() {
String s = JS_tictactoe;
server.send(200, "text/javascript", s);
LEDblink(L_YELLOW);
}
void ticStyle() {
String s = CSS_tictactoe;
server.send(200, "text/css", s);
LEDblink(L_YELLOW);
}
void ticData() {
Serial.print("TicTacToe: "+server.client().remoteIP().toString()+"\n");
String user = server.arg("user");
String clicked = server.arg("click");
String request = server.arg("request");
String leave = server.arg("leave");
if(user != "") {
if(user == "get") {
server.send(200, "text/plane", "{\"Player1\":\""+ticPlayer1+"\", \"Player2\":\""+ticPlayer2+"\"}");
}
else if(ticPlayer1 == "") {
ticPlayer1 = user;
server.send(200, "text/plane", "player1");
}
else if(ticPlayer2 == "") {
ticPlayer2 = user;
server.send(200, "text/plane", "player2");
}
else {
server.send(200, "text/plane", "Full");
}
if(ticPlayer1 != "" && ticPlayer2 != "") {
ticClick = "0";
}
digitalWrite(L_ORANGE,HIGH);
delay(500);
digitalWrite(L_ORANGE,LOW);
}
else if(leave != "") {
if(leave == ticPlayer1) {
ticPlayer1 = "";
}
else if(leave == ticPlayer2) {
ticPlayer2 = "";
}
ticClick = "-1";
server.send(200, "text/plane", leave+" left");
}
else if(clicked != "") {
ticClick = clicked;
server.send(200, "text/plane", "OK: "+ticClick);
}
else if(request != "") {
if(request == "checkClicked") {
server.send(200, "text/plane", ticClick);
}
if(request == "reset") {
ticReset++;
if(ticReset == 2) {
ticPlayer1 = "";
ticPlayer2 = "";
ticClick = "-1";
ticReset = 0;
server.send(200, "text/plane", "Reseted");
}
else {
server.send(200, "text/plane", "Verification required");
}
}
}
LEDblink(L_YELLOW);
}*/
//==============================================================
// SETUP
//==============================================================
void setup(void){
Serial.begin(9600);
pinMode(L_GREEN,OUTPUT);
pinMode(L_YELLOW,OUTPUT);
pinMode(L_ORANGE,OUTPUT);
pinMode(L_RED,OUTPUT);
digitalWrite(L_RED, HIGH);
WiFi.hostname("ESP8266");
WiFi.begin(ssid, password);
MDNS.begin("ESP8266");
MDNS.addService("http", "tcp", 80);
digitalWrite(L_RED, LOW);
digitalWrite(L_ORANGE, HIGH);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
digitalWrite(L_ORANGE, LOW);
digitalWrite(L_YELLOW, HIGH);
WiFi.config(staticIP86_10, gateway86_10, subnet86_10);
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/main.css", proMainCSS);
server.on("/page.css", proPageCSS);
server.on("/page.js", proPageJS);
server.on("/login/style.css", proLoginCSS);
server.on("/cookies.js", proCookieJS);
server.on("/translator", proTranslatorHTML);
server.on("/translator/style.css", proTranslatorCSS);
server.on("/translator/script.js", proTranslatorJS);
server.on("/login", proLoginHTML);
server.on("/admin", admin);
server.on("/reboot", reboot);
server.on("/console", console);
server.on("/buspage", buspage);
/*server.on("/minesweeper", minesPage);
server.on("/minesweeper/script.js", minesScript);
server.on("/minesweeper/style.css", minesStyle);
server.on("/tictactoe", ticPage);
server.on("/tictactoe/script.js", ticScript);
server.on("/tictactoe/style.css", ticStyle);
server.on("/tictactoe/data", ticData);*/
server.onNotFound(handleNotFound);
server.begin();
timeClient.begin();
Serial.println("HTTP server started");
digitalWrite(L_YELLOW, LOW);
digitalWrite(L_GREEN, HIGH);
}
//==============================================================
// LOOP
//==============================================================
void loop(void){
timeClient.update();
server.handleClient();
}
我有一个网页和一些作为学校项目制作的“游戏”。
包含的文件位于以下位置:
const char Some_Name[] PROGMEM = R"=====(
some(code);
)=====";
这可能是由于大型源代码引起的吗?还是包含的大型文件?我也将其拆分为几个文件(main.h
,console.h
,...),但没有帮助。
如何解决该问题?我不知道应该做什么或做错了什么。
答案 0 :(得分:1)
最好将SPIFFS文件系统用于网站的静态文件。您可以将文件放在项目文件夹的数据子文件夹中,并使用SPIFFS上传IDE插件上传SPIFFS文件系统。