-
Notifications
You must be signed in to change notification settings - Fork 1
/
ACROBOTIC_plot_sensor_data9_git.ino
289 lines (276 loc) · 8.4 KB
/
ACROBOTIC_plot_sensor_data9_git.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*------------------------------------------------------------------------------
10/20/2018
Author: Cisco • A C R O B O T I C
Platforms: ESP8266
Language: C++/Arduino
File: bmp180_gui.ino
------------------------------------------------------------------------------
Description:
Code for YouTube video demonstrating how to plot sensor data from a BMP180 tem-
perature, and barometric pressure sensor. A web server running on the ESP8266
serves a web page that uses Chart.js and websockets to plot the data.
https://youtu.be/lEVoRJSsEa8
------------------------------------------------------------------------------
Do you like my work? You can support me:
https://patreon.com/acrobotic
https://paypal.me/acrobotic
https://buymeacoff.ee/acrobotic
------------------------------------------------------------------------------
Please consider buying products and kits to help fund future Open-Source
projects like this! We'll always put our best effort in every project, and
release all our design files and code for you to use.
https://acrobotic.com/
https://amazon.com/shops/acrobotic
Adapted by Stuart A Spray C.Eng MIEE 'simplyEngineering'
------------------------------------------------------------------------------
License:
Please see attached LICENSE.txt file for details.
------------------------------------------------------------------------------*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <Ticker.h>
#include <Adafruit_BME280.h> // modified from original bmp180
String VERSION = "ACROBOTIC_plot_sensor_data8";
Adafruit_BME280 bme; // I2C
// Collecting BMP180 sensor data
Ticker timer;
bool get_data = false;
// Connecting to the Internet
const char* ssid = "BTHub5-9R6R";
const char* password = "5a7fe98fab";
// Running a web server
ESP8266WebServer server;
// Adding a websocket to the server
WebSocketsServer webSocket = WebSocketsServer(81);
// Serving a web page (from flash memory)
// formatted as a string literal!
char webpage[] PROGMEM = R"=====(
<html>
<!-- Adding a data chart using Chart.js -->
<head>
<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js'></script>
</head>
<body onload="javascript:init()">
<!-- Adding a slider for controlling data rate -->
<span>
<input type="range" min="1" max="60" value="1" id="dataRateSlider" oninput="sendDataRate()" />
<label for="dataRateSlider" id="dataRateLabel">Interval (Seconds)</label>
</span>
<span>
<input type="range" min="20" max="100" value="20" id="dataLengthSlider" oninput="fixDataLength()" />
<label for="dataLengthSlider" id="dataLengthLabel">Length</label>
</span><hr/>
<span><label id = "latestTemp">Latest temperature</label></span>
<hr />
<div>
<canvas id="line-chartT" width="400" height="150"></canvas>
</div>
<div>
<canvas id="line-chartH" width="400" height="150"></canvas>
</div>
<div>
<canvas id="line-chartP" width="400" height="150"></canvas>
</div>
<!-- Adding a websocket to the client (webpage) -->
<script>
var webSocket, dataPlotT,dataPlotH,dataPlotP;
var maxDataPoints = 20;
function removeData(){
dataPlotT.data.labels.shift();
dataPlotT.data.datasets[0].data.shift();
dataPlotH.data.labels.shift();
dataPlotH.data.datasets[0].data.shift();
dataPlotP.data.labels.shift();
dataPlotP.data.datasets[0].data.shift();
}
function addData(label, data) {
document.getElementById("latestTemp").innerHTML = "<div><h1>Latest: " + data.temp.toFixed(1)
+ "C " + data.humid.toFixed(0) + "% " + data.press.toFixed(0) + "mBar </h1><h6>ACROBOTIC_plot_sensor_data9</h6></div>";
if(dataPlotT.data.labels.length > maxDataPoints) removeData();
dataPlotT.data.labels.push(label);
dataPlotT.data.datasets[0].data.push(data.temp);
dataPlotT.update();
console.log(dataPlotT.data.datasets[0].data);
dataPlotH.data.labels.push(label);
dataPlotH.data.datasets[0].data.push(data.humid);
dataPlotH.update();
dataPlotP.data.labels.push(label);
dataPlotP.data.datasets[0].data.push(data.press);
dataPlotP.update();
}
function init() {
webSocket = new WebSocket('ws://' + window.location.hostname + ':81/');
dataPlotT = new Chart(document.getElementById("line-chartT"), {
type: 'line',
data: {
labels: [],
datasets: [{
data: [],
label: "Temperature (C)",
borderColor: "#3e95cd",
fill: false
}]
},
options:
{
scales:
{
yAxes:
[{
scaleLabel:
{
display: true,
labelString: 'C'
},
ticks:
{
max: 40,
min: 10
}
}],
}
}
});
dataPlotH = new Chart(document.getElementById("line-chartH"), {
type: 'line',
data: {
labels: [],
datasets: [{
data: [],
label: "Relative Humidity %",
borderColor: "#3e95cd",
fill: false
}]
},
options:
{
scales:
{
yAxes:
[{
scaleLabel:
{
display: true,
labelString: 'RH%'
},
ticks:
{
max: 100,
min: 10
}
}],
}
}
});
dataPlotP = new Chart(document.getElementById("line-chartP"), {
type: 'line',
data: {
labels: [],
datasets: [{
data: [],
label: "Pressure mBar",
borderColor: "#3e95cd",
fill: false
}]
},
options:
{
scales:
{
yAxes:
[{
scaleLabel:
{
display: true,
labelString: 'mBar'
},
ticks:
{
max: 1020,
min: 980
}
}],
}
}
});
webSocket.onmessage = function(event) {
var data = JSON.parse(event.data);
// console.log(data);
var today = new Date();
var t = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
addData(t, data);
}
}
function fixDataLength()
{
maxDataPoints = document.getElementById("dataLengthSlider").value;
document.getElementById("dataLengthLabel").innerHTML = "Length: " + maxDataPoints;
}
function sendDataRate(){
var dataRate = document.getElementById("dataRateSlider").value;
webSocket.send(dataRate);
//console.log(dataRate);
document.getElementById("dataRateLabel").innerHTML = "Interval: " + dataRate + " Seconds";
}
</script>
</body>
</html>
)=====";
void setup() {
// put your setup code here, to run once:
WiFi.begin(ssid, password);
Serial.begin(115200);
delay(500);
while(WiFi.status()!=WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println(VERSION);
Serial.println("");
Serial.print("Local IP Address: ");
Serial.println(WiFi.localIP());
server.on("/",[](){
server.send_P(200, "text/html", webpage);
});
server.begin();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
bool status = bme.begin(0x76);
if (!status)
{
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
timer.attach(5, getData);
}
void loop() {
// put your main code here, to run repeatedly:
webSocket.loop();
server.handleClient();
if(get_data){
const float cTemp = bme.readTemperature();
Serial.println(cTemp);
String json = "{\"temp\":";
json += bme.readTemperature();
json += ",\"humid\":";
json += bme.readHumidity();
json += ",\"press\":";
json += bme.readPressure()/100;
json += "}";
Serial.println(json);
webSocket.broadcastTXT(json.c_str(), json.length());
get_data = false;
}
}
void getData() {
get_data = true;
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length){
// Do something with the data from the client
if(type == WStype_TEXT){
float dataRate = (float) atof((const char *) &payload[0]);
timer.detach();
timer.attach(dataRate, getData);
}
}