0

我有一个运行良好的工作代码。代码的本质是该人连接到 Wi-Fi 网络,将他转移到 Captive Portal,他在其中输入一些发送到 esp8266 的文本。但是这样不行,因为我想在不连接网络的情况下做这一切,那么从服务器导入JQuery是不合适的,你需要通过文件连接JQuery来做到这一点。因此,为此我将它加载到 esp8266 文件系统中,但我现在找不到有关如何在 html 代码中包含此文件的信息。请告诉我该怎么做?

代码:(关键行突出显示)。

#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <FS.h>

const byte DNS_PORT = 53;
IPAddress apIP(172, 0, 0, 1);
DNSServer dnsServer;
ESP8266WebServer webServer(80);

String handleRoot = R"=====(
<!DOCTYPE html>
<html lang='en'>
   <head>


   <script src="jquery"></script>


    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
  </head>
  <body>
      <h1>Ввод:</h1>
      <input type='text' name='input' id='input' size=2 autofocus>
      <div>
      <br><button id='send_button'>Send</button>
      </div>
<script>
  var input;
  $('#send_button').click(function(e){
    e.preventDefault();
    input = $('#input').val();
    $.get('/send?input=' + input, function(data){
     console.log(data);
    });
  });   
</script>
</body>
</html>
)=====";

void handleSend() {
  Serial.println("Input");
  if (webServer.arg("input")!= ""){
    Serial.println("Input is: " + webServer.arg("input"));
  }
}

void setup() {
  Serial.begin(115200);


  SPIFFS.begin();
  File jquery = SPIFFS.open("/jquery-3.5.1.min.js", "r");


  delay(10);
  Serial.println("Started");
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  WiFi.softAP("INFO");
  
  dnsServer.start(DNS_PORT, "*", apIP);

  webServer.onNotFound([]() {
   webServer.send(200, "text/html", handleRoot);
  });
   webServer.on ("/send", handleSend);
  webServer.begin();
}

void loop() {
  dnsServer.processNextRequest();
  webServer.handleClient();}
4

0 回答 0