From 5558289ba2ffebc97ef8ca9a8f3c5366d68ef7c0 Mon Sep 17 00:00:00 2001 From: sietse jonker Date: Tue, 27 Feb 2024 19:17:47 +0100 Subject: [PATCH] cleans up code and adds comments --- arduino/node-code/node-code-final.ino | 53 +++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/arduino/node-code/node-code-final.ino b/arduino/node-code/node-code-final.ino index 9b50484..28d6d24 100644 --- a/arduino/node-code/node-code-final.ino +++ b/arduino/node-code/node-code-final.ino @@ -28,7 +28,8 @@ Adafruit_SGP30 sgp; WebSocketsClient webSocket; // define variables -uint16_t TVOC_base, eCO2_base; +uint16_t TVOC_base; +uint16_t eCO2_base; uint16_t counter = 0; float temperature = 0; float humidity = 0; @@ -36,8 +37,9 @@ uint16_t eCO2 = 0; uint16_t TVOC = 0; bool noise = false; bool update = false; -double counterBaseline = 15000; -int counterInterval = 5000; +unsigned long currentCounter; +unsigned long lastCounter; +uint16_t interval = 5000 // hexdump function for websockets binary handler void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) { @@ -87,6 +89,7 @@ void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { } } +// special function to setup websocket void websocketSetup(){ WiFiMulti.addAP("iotroam", "vbK9gbDBIB"); @@ -104,6 +107,7 @@ void websocketSetup(){ webSocket.setReconnectInterval(500); } +// fucntion to reset the values if needed void resetValues() { TVOC_base; eCO2_base; @@ -116,6 +120,45 @@ void resetValues() { lastCounter = 0; } +// function to check for errors in sensors +bool checkForError(){ + if (!sgp.IAQmeasure()) { + Serial.println("SGP30: BAD"); + errorSGP30 = true; + } else { + Serial.println("SGP30: OK"); + errorSGP30 = false; + } + + if (isnan(temperature || isnan(humidity)){ + Serial.println("DHT11: BAD") + } else { + Serial.println("DHT11: OK"); + errorDHT11 = false; + } +} + +// function to update when interval is met (see intervalCounter variable) +void update(){ + webSocket.sendTXT("Temp: " + String(temperature)); + webSocket.sendTXT("Humi: " + String(humidity)); + webSocket.sendTXT("eCO2: " + String(sgp.eCO2)); + webSocket.sendTXT("TVOC: " + String(sgp.TVOC)); + + sgp.getIAQBaseline(&eCO2_base, &TVOC_base); + + // if sensor doesnt work print in serialmonitor + + + // read dht11 sensor + temperature = float(dht.readTemperature()); + humidity = float(dht.readHumidity()); + + // display sensordata on oled screen + displayData(); +} + +// function to display data on oled screen void displayData() { // clear display for new info display.clearDisplay(); @@ -134,6 +177,7 @@ void displayData() { display.display(); } +// setup function void setup() { Serial.begin(115200); @@ -152,6 +196,7 @@ void setup() { resetValues(); } +// loop function void loop() { webSocket.loop(); @@ -179,7 +224,7 @@ void loop() { // display sensordata on oled screen displayData(); - if (currentCounter - lastCounter >= 5000){ + if (currentCounter - lastCounter >= interval){ lastcounter = millis() update(); }