Skip to content

Commit

Permalink
Merge pull request #12 from gelanchez/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
gelanchez authored Nov 27, 2020
2 parents 122e46e + f24f14c commit d4c7a12
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 192 deletions.
Binary file added ESP32-simpleServer-data_screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This repository contains an implementation of a simple HTTP webserver for an ESP
The communication between the clients and the webserver is done using [AJAX](https://en.wikipedia.org/wiki/Ajax_(programming)) with [jQuery](https://jquery.com/) in the clients. [JSON](https://www.json.org/json-en.html) is used as data format and [charts.js](https://www.chartjs.org/) for data visualization.

## Website appearance
<img src="./ESP32-simpleServer-data_screenshot.png" alt="Website appearance" width="100%" height="auto">

## Schematic
The following parts have been used in the electronic circuit. Nevertheless, they can be replaced by other similar ones with little change in the code.
Expand Down
194 changes: 2 additions & 192 deletions html/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,7 @@
<meta name="description" content="ESP32 simple webserver data sensor">
<meta name="author" content="José Ángel Sánchez">
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
<style>
html {
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}

#title {
margin: 0px auto;
text-decoration: underline;
}

#data {
margin: 5px auto;
}

#led {
margin-bottom: -20px;
}
</style>
<link rel="stylesheet" href="styles.css">
</head>

<body>
Expand All @@ -51,179 +33,7 @@ <h2 id="title">ESP32 simple server with AJAX</h2>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.js"
integrity="sha512-QEiC894KVkN9Tsoi6+mKf8HaCLJvyA6QIRzY5KrfINXYuP9NxdIkRQhGq3BZi0J4I7V5SidGM3XUQ5wFiMDuWg=="
crossorigin="anonymous"></script>
<script type="text/javascript">
var counter = 0;

// Draw LED
var contextLED = document.getElementById("led").getContext("2d");
contextLED.arc(25, 25, 15, 0, Math.PI * 2, false);
contextLED.lineWidth = 3;
contextLED.strokeStyle = "black";
contextLED.fillStyle = "black";
contextLED.stroke();
contextLED.fill();

var ctxTemp = document.getElementById('temperatureChart').getContext('2d');
var temperatureChart = new Chart(ctxTemp, {
type: 'line',
data: {
//labels: [1, 2, 3],
datasets: [{
label: 'Temperature',
borderColor: 'red',
backgroundColor: 'red',
borderWidth: 2,
pointRadius: 1,
fill: false
}]
},
options: {
legend: {
display: false
},
responsive: true,
scales: {
xAxes: [{
display: true,
ticks: {
display: true
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Temperature (ºC)'
},
ticks: {
min: 0,
max: 40
}
}]
},
showLines: true,
elements: {
line: {
tension: 0 // disables bezier curves
}
},
animation: {
duration: 0 // general animation time
},
hover: {
animationDuration: 0 // duration of animations when hovering an item
},
responsiveAnimationDuration: 0 // animation duration after a resize
}
});

var ctxIllum = document.getElementById('illuminanceChart').getContext('2d');
var illuminanceChart = new Chart(ctxIllum, {
type: 'line',
data: {
datasets: [{
label: 'Illuminance',
borderColor: 'gold',
backgroundColor: 'gold',
borderWidth: 2,
pointRadius: 1,
fill: false
}]
},
options: {
legend: {
display: false
},
responsive: true,
scales: {
xAxes: [{
display: true,
ticks: {
display: true
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Illuminance (lux)'
},
ticks: {
min: 0,
max: 10000
}
}]
},
showLines: true,
animation: {
duration: 0 // general animation time
},
hover: {
animationDuration: 0 // duration of animations when hovering an item
},
responsiveAnimationDuration: 0 // animation duration after a resize
}
});

function changeLed() {
$.post("/changeled", function (data, status) {
if (data.ledStatus) {
contextLED.fillStyle = "red";
contextLED.fill();
}
else {
contextLED.fillStyle = "black";
contextLED.fill();
}
},
"json");
}

function updateValues() {
$.ajax({
url: "/_sensors",
dataType: "json",
success: function (data) {
document.getElementById("temperature").innerHTML = data.temperature;
document.getElementById("illuminance").innerHTML = data.illuminance;
updateCharts(data.temperature, data.illuminance);
}
});
}

function updateCharts(temperature, illuminance) {
let date = new Date();
let timeDislpayed = date.getMinutes().toString().padStart(2, '0') + ":" + date.getSeconds().toString().padStart(2, '0');
addData(temperatureChart, timeDislpayed, [temperature]);
addData(illuminanceChart, timeDislpayed, [illuminance]);
// Remove values from chart after 100 data
if (counter < 100) {
counter++;
}
else {
removeData(temperatureChart);
removeData(illuminanceChart);
}
}

function addData(chart, label, data) {
chart.data.labels.push(label);
chart.data.datasets.forEach((dataset) => {
dataset.data.push(data);
});
chart.update();
}

function removeData(chart) {
chart.data.labels.shift();
chart.data.datasets.forEach((dataset) => {
dataset.data.shift();
});
chart.update();
}

window.setInterval(updateValues, 2000);
</script>
<script src="main.js"></script>
</body>

</html>
171 changes: 171 additions & 0 deletions html/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
var counter = 0;

// Draw LED
var contextLED = document.getElementById("led").getContext("2d");
contextLED.arc(25, 25, 15, 0, Math.PI * 2, false);
contextLED.lineWidth = 3;
contextLED.strokeStyle = "black";
contextLED.fillStyle = "black";
contextLED.stroke();
contextLED.fill();

var ctxTemp = document.getElementById('temperatureChart').getContext('2d');
var temperatureChart = new Chart(ctxTemp, {
type: 'line',
data: {
//labels: [1, 2, 3],
datasets: [{
label: 'Temperature',
borderColor: 'red',
backgroundColor: 'red',
borderWidth: 2,
pointRadius: 1,
fill: false
}]
},
options: {
legend: {
display: false
},
responsive: true,
scales: {
xAxes: [{
display: true,
ticks: {
display: true
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Temperature (ºC)'
},
ticks: {
min: 0,
max: 40
}
}]
},
showLines: true,
elements: {
line: {
tension: 0 // disables bezier curves
}
},
animation: {
duration: 0 // general animation time
},
hover: {
animationDuration: 0 // duration of animations when hovering an item
},
responsiveAnimationDuration: 0 // animation duration after a resize
}
});

var ctxIllum = document.getElementById('illuminanceChart').getContext('2d');
var illuminanceChart = new Chart(ctxIllum, {
type: 'line',
data: {
datasets: [{
label: 'Illuminance',
borderColor: 'gold',
backgroundColor: 'gold',
borderWidth: 2,
pointRadius: 1,
fill: false
}]
},
options: {
legend: {
display: false
},
responsive: true,
scales: {
xAxes: [{
display: true,
ticks: {
display: true
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Illuminance (lux)'
},
ticks: {
min: 0,
max: 10000
}
}]
},
showLines: true,
animation: {
duration: 0 // general animation time
},
hover: {
animationDuration: 0 // duration of animations when hovering an item
},
responsiveAnimationDuration: 0 // animation duration after a resize
}
});

function changeLed() {
$.post("/changeled", function (data, status) {
if (data.ledStatus) {
contextLED.fillStyle = "red";
contextLED.fill();
}
else {
contextLED.fillStyle = "black";
contextLED.fill();
}
},
"json");
}

function updateValues() {
$.ajax({
url: "/_sensors",
dataType: "json",
success: function (data) {
document.getElementById("temperature").innerHTML = data.temperature;
document.getElementById("illuminance").innerHTML = data.illuminance;
updateCharts(data.temperature, data.illuminance);
}
});
}

function updateCharts(temperature, illuminance) {
let date = new Date();
let timeDislpayed = date.getMinutes().toString().padStart(2, '0') + ":" + date.getSeconds().toString().padStart(2, '0');
addData(temperatureChart, timeDislpayed, [temperature]);
addData(illuminanceChart, timeDislpayed, [illuminance]);
// Remove values from chart after 100 data
if (counter < 100) {
counter++;
}
else {
removeData(temperatureChart);
removeData(illuminanceChart);
}
}

function addData(chart, label, data) {
chart.data.labels.push(label);
chart.data.datasets.forEach((dataset) => {
dataset.data.push(data);
});
chart.update();
}

function removeData(chart) {
chart.data.labels.shift();
chart.data.datasets.forEach((dataset) => {
dataset.data.shift();
});
chart.update();
}

window.setInterval(updateValues, 2000);
Loading

0 comments on commit d4c7a12

Please sign in to comment.