A Live Data Plotter and Logger in the Browser with an ESP8266

Stream sensor readings from an ESP8266 to a web page over WebSockets, plot them live and download them, with no app to install.
Software Engineering
Signal Processing
Author
Published

September 11, 2020

When you’re building a sensor prototype, you spend a lot of time just looking at the signal. A USB cable and the Arduino Serial Plotter work on a bench, but not when the device is battery powered, strapped to someone, or across the room.

So I wrote a small template for the ESP8266 (NodeMCU) that streams readings over Wi-Fi to a web page. Open the page on any phone or laptop and you get a live plot, start and stop buttons, and a download button for the recording. I later used it as the base for the CMC-Spiro spirometer.

Code: Zilch123/ESP8266-NodeMCU-Webpage-Data-plotter-and-logger

How it works

flowchart LR
  S[Sensor] --> E[ESP8266]
  E -- "HTTP: serves the page once" --> B[Browser]
  E -- "WebSocket: streams readings" --> B

The ESP8266 does two jobs:

  1. Serves the web page. The whole page (HTML and JavaScript) is stored in the chip’s flash memory as a string, and sent when you open the ESP8266’s IP address.
  2. Streams the data. The page opens a WebSocket back to the ESP8266 on port 81, and the ESP8266 pushes each new reading to it.

WebSockets fit this well. Instead of the browser asking for data again and again, the connection stays open and the readings just arrive.

The firmware

Storing the page in flash with PROGMEM keeps it out of the ESP8266’s limited RAM:

char webpage[] PROGMEM = R"=====(
<html>
  ...
</html>
)=====";

The setup connects to Wi-Fi, prints the IP address, and starts the web server and the WebSocket server:

server.on("/",[](){
  server.send_P(200, "text/html", webpage);
});
server.begin();
webSocket.begin();

The loop reads the sensor, wraps the value in a small JSON message and sends it to every connected browser:

void loop() {
  webSocket.loop();
  server.handleClient();
  results = analogRead(A0);
  String json = "{\"value\":";
  json += results;
  json += "}";
  webSocket.broadcastTXT(json.c_str(), json.length());
  delay(5);
}

Swap analogRead(A0) for any other sensor read, such as an external ADC over I2C, and the rest stays the same.

The web page

The page uses Plotly for the chart. Each message from the WebSocket gets a timestamp and, while recording, is added to the plot and to an array:

webSocket.onmessage = function(event) {
  var data = JSON.parse(event.data);
  var today = new Date();
  var t = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds() + ":" + today.getMilliseconds();
  if(recordFlag==1)
  {
    df_t.push(t);
    df.push(data.value);
    Plotly.extendTraces('graph', {x: [[t]], y: [[data.value]]}, [0]);
  }
}

The Download button turns the recorded arrays into a tab-separated text file in the browser, so nothing needs to be stored on the ESP8266:

var textToSaveAsBlob = new Blob([textToSave], {type:"text/plain"});
var textToSaveAsURL = window.URL.createObjectURL(textToSaveAsBlob);

Things to know

  • Plotly loads from a CDN, so the phone or laptop viewing the page needs internet access. For fully offline use, you’d have to serve a smaller charting library from the ESP8266 itself.
  • Timestamps come from the browser, not the sensor, so they include network delay. That’s fine for watching a signal, but for precise timing the ESP8266 should send its own timestamp with each reading.
  • Long recordings slow the page down, because every point stays on the plot. Keeping only the last few hundred points on screen, while still saving everything, fixes that.
  • Keep Wi-Fi credentials out of the code you publish. Use placeholders, or a separate config file that isn’t committed.