From cfd5f4e11f7cbeba79e1ef95d501c0d9f5d6a0de Mon Sep 17 00:00:00 2001 From: huskee Date: Wed, 13 Dec 2023 20:30:06 +0200 Subject: [PATCH] Add datetime to web interface --- data/index.html | 1 + data/script.js | 50 +++++++++++++++++++++++++++---------------------- src/main.cpp | 1 + 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/data/index.html b/data/index.html index 984fbe7..924558e 100644 --- a/data/index.html +++ b/data/index.html @@ -6,6 +6,7 @@

A:

B:

+

time:

\ No newline at end of file diff --git a/data/script.js b/data/script.js index 49f6f77..487d5c5 100644 --- a/data/script.js +++ b/data/script.js @@ -1,44 +1,50 @@ - var gateway = `ws://${window.location.hostname}/ws`; var websocket; // Init web socket when the page loads -window.addEventListener('load', onload); +window.addEventListener("load", onload); function onload(event) { - initWebSocket(); + initWebSocket(); } -function getReadings(){ - websocket.send("getReadings"); +function getReadings() { + websocket.send("getReadings"); } function initWebSocket() { - console.log('Trying to open a WebSocket connection...'); - websocket = new WebSocket(gateway); - websocket.onopen = onOpen; - websocket.onclose = onClose; - websocket.onmessage = onMessage; + console.log("Trying to open a WebSocket connection..."); + websocket = new WebSocket(gateway); + websocket.onopen = onOpen; + websocket.onclose = onClose; + websocket.onmessage = onMessage; } // When websocket is established, call the getReadings() function function onOpen(event) { - console.log('Connection opened'); - getReadings(); + console.log("Connection opened"); + getReadings(); } function onClose(event) { - console.log('Connection closed'); - setTimeout(initWebSocket, 2000); + console.log("Connection closed"); + setTimeout(initWebSocket, 2000); } // Function that receives the message from the ESP32 with the readings function onMessage(event) { - console.log(event.data); - var myObj = JSON.parse(event.data); - var keys = Object.keys(myObj); + console.log(event.data); + var myObj = JSON.parse(event.data); + var keys = Object.keys(myObj); - for (var i = 0; i < keys.length; i++){ - var key = keys[i]; - document.getElementById(key).innerHTML = myObj[key]; - } -} \ No newline at end of file + for (var i = 0; i < keys.length - 1; i++) { + var key = keys[i]; + document.getElementById(key).innerHTML = myObj[key]; + } + var today = new Date(); + var date = + today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate(); + var time = + today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); + var dateTime = date + " " + time; + document.getElementById("time").innerHTML = dateTime; +} diff --git a/src/main.cpp b/src/main.cpp index 6c91c7c..57ca825 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,6 +52,7 @@ String getSensorReadings() { double tempB = thermocoupleB.readCelsius(); readings["tempA"] = String(tempA) + "°C"; readings["tempB"] = String(tempB) + "°C"; + readings["time"] = String(lastTime); } String jsonString = JSON.stringify(readings); return jsonString;