-
-
Save sarasantos/1dd707a1d89a7f3c6afcfb6c9ded62da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Complete project details: https://randomnerdtutorials.com/esp32-plot-readings-charts-multiple/ | |
// Get current sensor readings when the page loads | |
window.addEventListener('load', getReadings); | |
// Create Temperature Chart | |
var chartT = new Highcharts.Chart({ | |
chart:{ | |
renderTo:'chart' | |
}, | |
yAxis: [{ // Primary yAxis | |
labels: { | |
format: '{value}*C', | |
style: { | |
color: '#101D42' | |
} | |
}, | |
title: { | |
text: 'Temperature', | |
style: { | |
color: '#101D42' | |
} | |
}, | |
min: 0, | |
max: 50 | |
}, { // Secondary yAxis | |
gridLineWidth: 0, | |
title: { | |
text: 'Humidity', | |
style: { | |
color: '#00A6A6' | |
} | |
}, | |
labels: { | |
format: '{value} %', | |
style: { | |
color: '#00A6A6' | |
} | |
}, | |
opposite: true, | |
min: 0, | |
max: 100 | |
}, { // Tertiary yAxis | |
gridLineWidth: 0, | |
title: { | |
text: 'Pressure', | |
style: { | |
color:'#8B2635' | |
} | |
}, | |
labels: { | |
format: '{value} hPa', | |
style: { | |
color: '#8B2635' | |
} | |
}, | |
opposite: true, | |
min: 900, | |
max: 1100 | |
}], | |
tooltip: { | |
shared: true | |
}, | |
legend: { | |
layout: 'vertical', | |
align: 'left', | |
x: 80, | |
verticalAlign: 'top', | |
y: 55, | |
floating: true | |
}, | |
series: [ | |
{ | |
name: 'Temperature #1', | |
type: 'spline', | |
yAxis: 0, | |
type: 'line', | |
color: '#101D42', | |
marker: { | |
symbol: 'circle', | |
radius: 3, | |
fillColor: '#101D42', | |
} | |
}, | |
{ | |
name: 'Humidity', | |
yAxis: 1, | |
type: 'line', | |
color: '#00A6A6', | |
marker: { | |
symbol: 'square', | |
radius: 3, | |
fillColor: '#00A6A6', | |
} | |
}, | |
{ | |
name: 'Pressure', | |
dashStyle: 'shortdot', | |
yAxis: 2, | |
type: 'line', | |
color: '#8B2635', | |
marker: { | |
symbol: 'triangle', | |
radius: 3, | |
fillColor: '#8B2635', | |
} | |
} | |
], | |
title: { | |
text: undefined | |
}, | |
xAxis: { | |
type: 'datetime', | |
dateTimeLabelFormats: { second: '%H:%M:%S' } | |
}, | |
credits: { | |
enabled: false | |
} | |
}); | |
//Plot temperature in the temperature chart | |
function plotTemperature(jsonValue) { | |
var keys = Object.keys(jsonValue); | |
console.log(keys); | |
console.log(keys.length); | |
for (var i = 0; i < keys.length; i++){ | |
var x = (new Date()).getTime(); | |
console.log(x); | |
const key = keys[i]; | |
var y = Number(jsonValue[key]); | |
console.log(y); | |
if(chartT.series[i].data.length > 40) { | |
chartT.series[i].addPoint([x, y], true, true, true); | |
} else { | |
chartT.series[i].addPoint([x, y], true, false, true); | |
} | |
} | |
} | |
// Function to get current readings on the webpage when it loads for the first time | |
function getReadings(){ | |
var xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange = function() { | |
if (this.readyState == 4 && this.status == 200) { | |
var myObj = JSON.parse(this.responseText); | |
console.log(myObj); | |
plotTemperature(myObj); | |
} | |
}; | |
xhr.open("GET", "/readings", true); | |
xhr.send(); | |
} | |
if (!!window.EventSource) { | |
var source = new EventSource('/events'); | |
source.addEventListener('open', function(e) { | |
console.log("Events Connected"); | |
}, false); | |
source.addEventListener('error', function(e) { | |
if (e.target.readyState != EventSource.OPEN) { | |
console.log("Events Disconnected"); | |
} | |
}, false); | |
source.addEventListener('message', function(e) { | |
console.log("message", e.data); | |
}, false); | |
source.addEventListener('new_readings', function(e) { | |
console.log("new_readings", e.data); | |
var myObj = JSON.parse(e.data); | |
console.log(myObj); | |
plotTemperature(myObj); | |
}, false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment