Refactor code and update websocket connection
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
// Sietse Jonker & Dano Bosch
|
||||
// 28/02/2024
|
||||
|
||||
// include these libraries
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_SH110X.h>
|
||||
@@ -21,9 +18,6 @@
|
||||
#define i2c_adress 0x3c
|
||||
#define USE_SERIAL Serial
|
||||
|
||||
// define node identification number
|
||||
#define nodeIdentificationNumber 1
|
||||
|
||||
// make new objects
|
||||
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
|
||||
DHT dht(DHTPIN, DHTTYPE);
|
||||
@@ -45,6 +39,41 @@ bool errorSGP30 = false;
|
||||
bool errorDHT11 = false;
|
||||
bool noise = false;
|
||||
|
||||
// setup function
|
||||
void setup() {
|
||||
// make serial connection at 115200 baud
|
||||
Serial.begin(115200);
|
||||
|
||||
// tell display what settings to use
|
||||
display.begin(i2c_adress, true);
|
||||
display.clearDisplay();
|
||||
|
||||
// tell sensors to start reading
|
||||
dht.begin();
|
||||
sgp.begin();
|
||||
|
||||
pinMode(MICPIN, INPUT);
|
||||
pinMode(DHTPIN, INPUT);
|
||||
|
||||
websocketSetup();
|
||||
resetValues();
|
||||
}
|
||||
|
||||
// loop function
|
||||
void loop() {
|
||||
// loop the websocket connection so it stays alive
|
||||
webSocket.loop();
|
||||
|
||||
// update when interval is met
|
||||
if (currentMillis - lastMillis >= interval){
|
||||
lastMillis = millis();
|
||||
update();
|
||||
}
|
||||
|
||||
// update the counter
|
||||
currentMillis = millis();
|
||||
}
|
||||
|
||||
// hexdump function for websockets binary handler
|
||||
void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
|
||||
const uint8_t* src = (const uint8_t*) mem;
|
||||
@@ -112,7 +141,7 @@ void websocketSetup(){
|
||||
webSocket.setReconnectInterval(500);
|
||||
}
|
||||
|
||||
// fucntion to reset the values if needed
|
||||
// function to reset the values if needed
|
||||
void resetValues() {
|
||||
TVOC_base;
|
||||
eCO2_base;
|
||||
@@ -153,7 +182,7 @@ void update(){
|
||||
displayData();
|
||||
|
||||
// webSocket.sendTXT("{\"Temp\":\"" + String(temperature) + "\",\"Humi\":\"" + String(humidity) + "\",\"eCO2\":\"" + String(sgp.eCO2) + "\",\"TVOC\":\"" + String(sgp.TVOC) + "\"}");
|
||||
webSocket.sendTXT("{\"node\": \"" + String(nodeIdentificationNumber) + "\", \"Temp\":\"" + String(temperature) + "\",\"Humi\":\"" + String(humidity) + "\",\"eCO2\":\"" + String(sgp.eCO2) + "\",\"TVOC\":\"" + String(sgp.TVOC) + "\"}");
|
||||
webSocket.sendTXT("{\"node\": \"" + String(WiFi.macAddress()) + "\", \"Temp\":\"" + String(temperature) + "\",\"Humi\":\"" + String(humidity) + "\",\"eCO2\":\"" + String(sgp.eCO2) + "\",\"TVOC\":\"" + String(sgp.TVOC) + "\"}");
|
||||
|
||||
sgp.getIAQBaseline(&eCO2_base, &TVOC_base);
|
||||
|
||||
@@ -183,39 +212,4 @@ void displayData() {
|
||||
|
||||
// display the screen
|
||||
display.display();
|
||||
}
|
||||
|
||||
// setup function
|
||||
void setup() {
|
||||
// make serial connection at 115200 baud
|
||||
Serial.begin(115200);
|
||||
|
||||
// tell display what settings to use
|
||||
display.begin(i2c_adress, true);
|
||||
display.clearDisplay();
|
||||
|
||||
// tell sensors to start reading
|
||||
dht.begin();
|
||||
sgp.begin();
|
||||
|
||||
pinMode(MICPIN, INPUT);
|
||||
pinMode(DHTPIN, INPUT);
|
||||
|
||||
websocketSetup();
|
||||
resetValues();
|
||||
}
|
||||
|
||||
// loop function
|
||||
void loop() {
|
||||
// loop the websocket connection so it stays alive
|
||||
webSocket.loop();
|
||||
|
||||
// update when interval is met
|
||||
if (currentMillis - lastMillis >= interval){
|
||||
lastMillis = millis();
|
||||
update();
|
||||
}
|
||||
|
||||
// update the counter
|
||||
currentMillis = millis();
|
||||
}
|
10
web/main.js
10
web/main.js
@@ -56,7 +56,7 @@ function handleIncomingData(data) {
|
||||
|
||||
function processNodeData(nodeNumber, temperature, humidity, CO2, TVOC) {
|
||||
// Initialize the array for this node if it doesn't exist yet
|
||||
if (!sensorData[nodeNumber]) {
|
||||
if (!nodeNumber[nodeNumber]) {
|
||||
sensorData[nodeNumber] = [];
|
||||
}
|
||||
|
||||
@@ -191,14 +191,16 @@ function updateNodeData(node, temperature, humidity, eCO2, TVOC) {
|
||||
document.getElementById("TVOCStatus").textContent = "Connected";
|
||||
|
||||
// Update the graph
|
||||
liveGraphs[node - 1].updateData(temperature, humidity, eCO2, TVOC);
|
||||
liveGraphs[node - 1].updateGraph();
|
||||
liveGraphs[0].updateData(temperature, humidity, eCO2, TVOC);
|
||||
liveGraphs[0].updateGraph();
|
||||
}
|
||||
|
||||
createNodeData("34:85:18:B5:CF:20");
|
||||
liveGraphs.push(new liveGraph("34:85:18:B5:CF:20"));
|
||||
// Call the function to create the HTML structure
|
||||
for (let i = 1; i <= amountOfNodes; i++) {
|
||||
createNodeData(i);
|
||||
liveGraphs.push(new liveGraph(i));
|
||||
|
||||
}
|
||||
|
||||
// make the graphs
|
||||
|
Reference in New Issue
Block a user