[英]ESP8266 WebServer
我發現了關於堆棧溢出的有趣代碼,但我唯一不喜歡的是它使用了通過 Internet 導入的 JQuery,我需要它在不連接到 Internet 的情況下全部工作。 你能告訴我如何改變嗎?
代碼:
void handleRoot() {
snprintf ( htmlResponse, 3000,
"<!DOCTYPE html>\
<html lang=\"en\">\
<head>\
<meta charset=\"utf-8\">\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
</head>\
<body>\
<h1>Time</h1>\
<input type='text' name='date_hh' id='date_hh' size=2 autofocus> hh \
<div>\
<br><button id=\"save_button\">Save</button>\
</div>\
<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\"></script>\
<script>\
var hh;\
$('#save_button').click(function(e){\
e.preventDefault();\
hh = $('#date_hh').val();\
$.get('/save?hh=' + hh , function(data){\
console.log(data);\
});\
});\
</script>\
</body>\
</html>");
server.send ( 200, "text/html", htmlResponse );
}
void handleSave() {
if (server.arg("hh")!= ""){
Serial.println("Hours: " + server.arg("hh"));
}
}
void setup() {
// Start serial
Serial.begin(115200);
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
server.on ( "/", handleRoot );
server.on ("/save", handleSave);
server.begin();
}
void loop() {
server.handleClient();
}
縮小的 jquery javascript 可以存儲在 ESP 上,並在瀏覽器請求時由模塊提供。
一種簡單的方法是使用SPI Flash 文件系統來提供 HTML 和 JQuery ZDE9B9ED708D7E919ZEEFFEE。
這意味着在草圖的data
子目錄中創建一個index.html
。 將原始草圖中的 HTML 添加到文件中。 還要將此文件中的腳本源更改為相對路徑:
<script src="jquery.min.js"></script>
然后下載jquery.min.js
並將其復制到data
子目錄中。
The example code at https://tttapa.github.io/ESP8266/Chap11%20-%20SPIFFS.html can be used as a starting point for the rest of the code. 其中的主要部分涉及初始化 SPIFFS 和為文件請求設置處理程序:
SPIFFS.begin();
server.onNotFound([]() {
if (!handleFileRead(server.uri()))
server.send(404, "text/plain", "404: Not Found");
});
// retain the save endpoint
server.on("/save", handleSave);
server.begin();
然后實現文件處理程序及其內容類型處理程序:
String getContentType(String filename)
{
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(".ico")) return "image/x-icon";
return "text/plain";
}
bool handleFileRead(String path) {
Serial.println("handleFileRead: " + path);
if (path.endsWith("/"))
{
path += "index.html";
}
String contentType = getContentType(path);
if (SPIFFS.exists(path))
{
File file = SPIFFS.open(path, "r");
size_t sent = server.streamFile(file, contentType);
file.close();
return true;
}
Serial.println("\tFile Not Found");
return false;
}
另一種方法是重寫 javascript 以便不需要 JQuery。
This involves registering an onclick handler on the button ( https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Event_handlers ), getting the value from the input field ( https://stackoverflow.com /a/11563667/1373856 )並發送 GET 請求( https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send )
您只需將其作為腳本標簽包含在您機器上的本地路徑中即可。
<script src="path-to-jquery/jquery.min.js" type="text/javascript"></script>
編輯:首先,您必須下載所需的 jquery 文件並將其存儲在本地路徑中。 所需的 jquery 路徑應該是從 html 文件到 jquery 的相對路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.