簡體   English   中英

Arduino 導入 jquery 文件

[英]Arduino import jquery file

我有 esp8266 的工作代碼,其中 jquery 是從互聯網導入的,但我需要從下載的文件中導入它,但我做不到。 當我寫#include“jquery-3.5.1.min.js”時,它給出了一個錯誤:jquery-3.5.1.min.js:沒有這樣的文件或目錄(如果我指定完整路徑,它也會給出這個錯誤) . 盡管此文件與項目文件位於同一目錄中。 你能告訴我我做錯了什么嗎?

編碼:

#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include "jquery-3.5.1.min.js"


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>
<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='save_button'>Send</button>
  </div>
<script src="/jquery-3.5.1.min.js"></script>
<script>
var input;
$('#save_button').click(function(e){
    e.preventDefault();
    input = $('#input').val();
    $.get('/send?input=' + input, function(data){
     console.log(data);
    });
  });   
</script>
</body>
</html>
)=====";

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


void setup() {
  Serial.begin(115200);
  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();
}

您的 html 代碼期望從您的服務器(即您的 ESP8266)加載/jquery-3.5.1.min.js ,您的服務器不提供服務。 Giving that jQuery requires 20-30kB of memory and it is probably too slow for ESP8266 to serve it and take too much of precious memory, one quick and alternative solution is to load the jQuery directly from jquery CDN site by changing your JavaScript line to:

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

還請從您的草圖中刪除#include "jquery-3.5.1.min.js" ,因為您的#include是用於C/C++ 加載模塊,而不是設計用於通過Internet 提供數據文件。

請注意,這是一個可行的解決方案,但不是最佳實踐。 It is better to write pure JavaScript to handle the requests rather than depend on jQuery library which will take up about 30kB+ memory, especially for IoT application when using an MCU as a web server.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM