ESP8266 Data Logging With Real Time Graphs
ESP8266 Data Logging With Real Time Graphs
graphs
https://circuits4you.com/2019/01/11/e
sp8266-data-logging-with-real-time-
graphs/
January 11, 2019ESP8266, IoT TutorialsESP8266, NodeMCU
In this tutorial we are going to make really really cool thing with NodeMCU ESP8266. That
is data logger web server with real time graphs and tables, mostly seen
on thingspeak. But this time we are putting complete graphs and tables all inside
NodeMCU without using thingspeak. For this we need graphs library that we are using
from chartsjs.org an open source Chart drawing library and Knowledge of alax, html and
javascipt.
Things we need
1. NodeMCU ESP8266
2. USB Cable
3. Laptop
4. Internet and wifi router
Programming NodeMCU
To draw graphs we use chart.js from CDN. you can use char.js file directly inside the
NodeMCU for this see : AJAX Web Server
To get knowledge of real time data update without refresh read this.
Program consists of two parts one is HTML, Javascipts and Arduino Code
Before directly uploading make changes in WiFi SSID and Password as per yours WiFi.
most of the things are explained inside code comments. also make index.h header file
and save it near to your ino file.
In this example we are creating web server inside ESP8266 for data logging. It has two
parts one part that displays HTML web GUI and second is that takes AJAX request and
reads ADC data.
ESPGraph.ino File
1 /*
4 * https://circuits4you.com
5 */
6 #include <ESP8266WiFi.h>
7 #include <WiFiClient.h>
8 #include <ESP8266WebServer.h>
11
17
19
20 //===============================================================
22 //===============================================================
23 void handleRoot() {
26 }
27
28 void handleADC() {
29 int a = analogRead(A0);
32 server.send(200, "text/plane", adcValue); //Send ADC value only to client ajax request
33 }
34 //==============================================================
35 // SETUP
36 //==============================================================
37 void setup(void){
38 Serial.begin(115200);
39
41 Serial.println("");
42
45
48 delay(500);
49 Serial.print(".");
50 }
51
53 Serial.println("");
54 Serial.print("Connected to ");
55 Serial.println(ssid);
58
59 server.on("/", handleRoot); //Which routine to handle at root location. This is display page
61
64 }
65 //==============================================================
66 // LOOP
67 //==============================================================
68 void loop(void){
70 }
2 <!doctype html>
3 <html>
5 <head>
8 server/ -->
10 <style>
11 canvas{
12 -moz-user-select: none;
13 -webkit-user-select: none;
14 -ms-user-select: none;
15 }
16
18 #dataTable {
20 border-collapse: collapse;
21 width: 100%;
22 }
23
26 padding: 8px;
27 }
28
30
32
33 #dataTable th {
34 padding-top: 12px;
35 padding-bottom: 12px;
36 text-align: left;
37 background-color: #4CAF50;
38 color: white;
39 }
40 </style>
41 </head>
42
43 <body>
47 </div>
48 <div>
49 <table id="dataTable">
50 <tr><th>Time</th><th>ADC Value</th></tr>
51 </table>
52 </div>
53 <br>
54 <br>
55
56 <script>
60 function showGraph()
61 {
63 values.push(arguments[i]);
64 }
65
68 type: 'line',
69 data: {
71 datasets: [{
76 data: values,
77 }],
78 },
79 options: {
80 title: {
81 display: true,
83 },
84 maintainAspectRatio: false,
85 elements: {
86 line: {
88 }
89 },
90 scales: {
91 yAxes: [{
92 ticks: {
93 beginAtZero:true
94 }
95 }]
96 }
97 }
98 });
99
100 }
101
105 showGraph(5,10,4,58);
106 };
107
110
111 setInterval(function() {
113 getData();
115
123 values.push(ADCValue);
124 timeStamp.push(time);
133 }
134 };
136 xhttp.send();
137 }
138
139 </script>
140 </body>
141
142 </html>
143
)=====";
Results
Upload code and get IP Address from serial monitor. and enter it in web browser. wait for
few seconds and see the updating of graphs.
References
1. AJAX Tutorial
2. SPIFFS for directly loading java script file.