диаграмма. js хранится на не работающих spiffs (диаграмма не отображается на веб-странице) - PullRequest
0 голосов
/ 30 января 2020

Я реализую веб-страницу из esp8266 в режиме точки доступа. Я сохранил этот html файл и диаграмму. js плагин в папке данных и загрузил в spiffs. веб-страница загружается и читает Chart. js Пример, но диаграмма не отображается. Любые идеи? спасибо x

файл веб-сервера, расположенный в папке данных с индексом. html и диаграммой. js

<!DOCTYPE HTML>
<html>
    <head>
      <meta charset="utf-8" />
      <title>Chart.js demo</title>
      <script src="/Chart.js" type="text/javascript"></script>
    </head>

<body>   
    <h1>Chart.js Sample</h1>
    <div class="chart-container" style="width: 600px; height: 400px">
        <canvas id="countries"></canvas>
    </div>

    <script src="/Chart.min.js" type="text/javascript">
        var pieData = {
            datasets: [{
                data: [20, 40, 10, 30],
                backgroundColor: ["#878BB6", "#4ACAB4", "#FF8153", "#FFEA88"]
            }]
        };

        // Get the context of the canvas element we want to select
        var countries = document.getElementById("countries").getContext("2d");
        new Chart(countries, {
            type: 'pie',
            data: pieData
        });
    </script>
</body>

</html>

файл веб-сервера:

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

static const char *wifi_ssid = "ESP32";
static const char *wifi_pass = "testing1";

static ESP8266WebServer server(80);

static bool wifi_connected;

static void handleRootOld() {
  server.send(200, "text/plain", "Welcome to the ESP8266 server test");
}

static void handleNotFound() {
  String path = server.uri();
  if (!SPIFFS.exists(path)) {
    server.send(404, "text/plain", "Path " + path + " not found. Please double-check the URL");
    return;
  }
  String contentType = "text/plain";
  if (path.endsWith(".css")) {
    contentType = "text/css";
  }
  else if (path.endsWith(".html")) {
    contentType = "text/html";
  }
  else if (path.endsWith(".js")) {
    contentType = "application/javascript";
  }
  File file = SPIFFS.open(path, "r");
  server.streamFile(file, contentType);
  file.close();
}

static void handleRoot() {
  File file = SPIFFS.open("/index.html", "r");
  if (!file) {
    server.send(500, "text/plain", "Problem with filesystem!\n");
    return;
  }
  server.streamFile(file, "text/html");
  file.close();
}

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

  Serial.println("\n\n### ESP8266 - Web server + SPIFFS ###\n\n");

  WiFi.softAP(wifi_ssid, wifi_pass);

  server.on("/", handleRoot);
  server.onNotFound(handleNotFound);

  if (!SPIFFS.begin()) {
    Serial.println("Failed to mount filesystem!\n");
  }

 if (SPIFFS.begin())
{
    Dir dir = SPIFFS.openDir("/");
    while (dir.next())
    {
        String fileName = dir.fileName();
        size_t fileSize = dir.fileSize();
        Serial.printf("FS File: %s, size: %u\n", fileName.c_str(), fileSize);
    }
    Serial.printf("\n");
}
  server.begin();
}

void loop() {
  delay(50);
  server.handleClient();
}
...