-
Notifications
You must be signed in to change notification settings - Fork 54
/
bmp180_gui.ino
161 lines (150 loc) · 4.81 KB
/
bmp180_gui.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
/*------------------------------------------------------------------------------
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
------------------------------------------------------------------------------
License:
Please see attached LICENSE.txt file for details.
------------------------------------------------------------------------------*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <Adafruit_BMP085.h>
#include <Ticker.h>
// Collecting BMP180 sensor data
Ticker timer;
Adafruit_BMP085 bmp;
bool get_data = false;
// Connecting to the Internet
char * ssid = "YOUR_SSID";
char * password = "YOUR_PASSWORD";
// 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 -->
<div>
<input type="range" min="1" max="10" value="5" id="dataRateSlider" oninput="sendDataRate()" />
<label for="dataRateSlider" id="dataRateLabel">Rate: 0.2Hz</label>
</div>
<hr />
<div>
<canvas id="line-chart" width="800" height="450"></canvas>
</div>
<!-- Adding a websocket to the client (webpage) -->
<script>
var webSocket, dataPlot;
var maxDataPoints = 20;
function removeData(){
dataPlot.data.labels.shift();
dataPlot.data.datasets[0].data.shift();
}
function addData(label, data) {
if(dataPlot.data.labels.length > maxDataPoints) removeData();
dataPlot.data.labels.push(label);
dataPlot.data.datasets[0].data.push(data);
dataPlot.update();
}
function init() {
webSocket = new WebSocket('ws://' + window.location.hostname + ':81/');
dataPlot = new Chart(document.getElementById("line-chart"), {
type: 'line',
data: {
labels: [],
datasets: [{
data: [],
label: "Temperature (C)",
borderColor: "#3e95cd",
fill: false
}]
}
});
webSocket.onmessage = function(event) {
var data = JSON.parse(event.data);
var today = new Date();
var t = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
addData(t, data.value);
}
}
function sendDataRate(){
var dataRate = document.getElementById("dataRateSlider").value;
webSocket.send(dataRate);
dataRate = 1.0/dataRate;
document.getElementById("dataRateLabel").innerHTML = "Rate: " + dataRate.toFixed(2) + "Hz";
}
</script>
</body>
</html>
)=====";
void setup() {
// put your setup code here, to run once:
WiFi.begin(ssid, password);
Serial.begin(115200);
while(WiFi.status()!=WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
server.on("/",[](){
server.send_P(200, "text/html", webpage);
});
server.begin();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
bmp.begin();
timer.attach(5, getData);
}
void loop() {
// put your main code here, to run repeatedly:
webSocket.loop();
server.handleClient();
if(get_data){
// Serial.println(bmp.readTemperature());
String json = "{\"value\":";
json += bmp.readTemperature();
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);
}
}