繁体   English   中英

ESP8266 网络服务器

[英]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;
}

替代方法:删除 JQuery 依赖项

另一种方法是重写 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM