Hi all,
I’m trying to show live data through index.html served by ESP8266WebServer.h
<!DOCTYPE html>
<svg width="960" height="673" stroke="#fff" stroke-width="0.5"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-hsv.v0.1.min.js"></script>
<script src="https://d3js.org/d3-contour.v1.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var i0 = d3.interpolateHsvLong(d3.hsv(120, 1, 0.65), d3.hsv(60, 1, 0.90)),
i1 = d3.interpolateHsvLong(d3.hsv(60, 1, 0.90), d3.hsv(0, 0, 0.95)),
interpolateTerrain = function(t) { return t < 0.5 ? i0(t * 2) : i1((t - 0.5) * 2); },
color = d3.scaleSequential(interpolateTerrain).domain([90, 190]);
d3.json("volcano.json", function(error, volcano) {
if (error) throw error;
svg.selectAll("path")
.data(d3.contours()
.size([volcano.width, volcano.height])
.thresholds(d3.range(90, 195, 5))
(volcano.values))
.enter().append("path")
.attr("d", d3.geoPath(d3.geoIdentity().scale(width / volcano.width)))
.attr("fill", function(d) { return color(d.value); });
});
</script>
used in Contour Plot - bl.ocks.org, who needs json.
But the code does not refresh client side continously:
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <FS.h>
const char* ssid = "yourSSID";
const char* password = "yourPASS";
const char* htmlfile = "/index.html";
ESP8266WebServer server(80);
// several const definitions on attached.
void setup() {
// several definitions on attached
Serial.begin(115200);
SPIFFS.begin();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/volcano.json", volcano);
server.on("/",HTTP_GET, volcano);
server.onNotFound(handleNotFound);
/*server.on("/index.html", HTTP_GET, []() {
// like volcano(), see attached
});*/
server.begin();
Serial.println("HTTP server started");
}
void loop () {
WiFiClient(keepalive);
WiFiClient client;
server.handleClient();
}
void volcano() {
StaticJsonBuffer<1174> jsonBuffer;
JsonObject& volcano = jsonBuffer.createObject();
volcano["width"] = 8;
volcano["height"] = 8;
JsonArray& analogValues = volcano.createNestedArray("values");
for (int rowCount = 0; rowCount < 8; rowCount++) {
r0 = metaMsk[rowPinRef[rowCount]][0];
r1 = metaMsk[rowPinRef[rowCount]][1];
r2 = metaMsk[rowPinRef[rowCount]][2];
r3 = metaMsk[rowPinRef[rowCount]][3];
digitalWrite(twoA, r0); //send to Digital OUT 1
digitalWrite(twoB, r1); //send to Digital OUT 2
digitalWrite(twoC, r2); //send to Digital OUT 3
digitalWrite(twoD, r3); //send to Digital OUT 4
for (int colCount = 0; colCount < 8; colCount++) {
c0 = metaMsk[colPinRef[colCount]][0];
c1 = metaMsk[colPinRef[colCount]][1];
c2 = metaMsk[colPinRef[colCount]][2];
c3 = metaMsk[colPinRef[colCount]][3];
digitalWrite(oneA, c0); //send to Digital OUT 1
digitalWrite(oneB, c1); //send to Digital OUT 2
digitalWrite(oneC, c2); //send to Digital OUT 3
digitalWrite(oneD, c3); //send to Digital OUT 4
int val = analogRead(COM1);
matrixValues[colCount] = val;
analogValues.add(val);
}//end for
delay(2);
}//end for
output = "";
volcano.printTo(output);
Serial.println("volcano" + (output));
server.send(200, "application/json", output);
// client.println(output);
}
void handleRoot() {
server.sendHeader("Location", "/index.html", true); //Redirect to our html web page
server.send(302, "text/plane", "");
}
void handleNotFound() {
if (loadFromSpiffs(server.uri())) return;
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
bool loadFromSpiffs(String path) {
String dataType = "text/plain";
if (path.endsWith("/")) path += "index.htm";
if (path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
else if (path.endsWith(".html")) dataType = "text/html";
else if (path.endsWith(".htm")) dataType = "text/html";
else if (path.endsWith(".css")) dataType = "text/css";
else if (path.endsWith(".js")) dataType = "application/javascript";
else if (path.endsWith(".png")) dataType = "image/png";
else if (path.endsWith(".gif")) dataType = "image/gif";
else if (path.endsWith(".jpg")) dataType = "image/jpeg";
else if (path.endsWith(".ico")) dataType = "image/x-icon";
else if (path.endsWith(".xml")) dataType = "text/xml";
else if (path.endsWith(".pdf")) dataType = "application/pdf";
else if (path.endsWith(".zip")) dataType = "application/zip";
File dataFile = SPIFFS.open(path.c_str(), "r");
if (server.hasArg("download")) dataType = "application/octet-stream";
if (server.streamFile(dataFile, dataType) != dataFile.size()) {
}
dataFile.close();
return true;
}
I’ve tried several ways, but none of them works. What’s wrong? Note some parts are only for testing. Attached are
SPIFF files used in project.
Thank you
Toni
manta_esp8266_json.zip (76.1 KB)