Files
J1B3-Sensor-boxes/arduino/node-code/nodeCodeFinal/nodeCodeHeader.cpp
2024-03-19 16:15:02 +01:00

104 lines
2.6 KiB
C++

#include "arduino.h"
#include "nodeCodeHeader.h"
#include <WebSocketsClient.h>
WebSocketsClient webSocket;
nodeReadings::nodeReadings() {
}
void nodeReadings::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);
}
void nodeReadings::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();
}
void nodeReadings::resetValues() {
counter = 0;
eCO2 = 0;
TVOC = 0;
temperature = 0;
humidity = 0;
currentMillis = 0;
lastMillis = 0;
errorSGP30 = false;
errorDHT11 = false;
noise = false;
}
void nodeReadings::update(){
// display sensordata on oled screen
displayData();
// webSocket.sendTXT("{\"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);
// read dht11 sensor
temperature = float(dht.readTemperature());
humidity = float(dht.readHumidity());
// check if any errors occured when reading sensors
checkForError();
}
void nodeReadings::displayData() {
// clear display for new info
display.clearDisplay();
// display the data on the oled screen
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
display.setCursor(0,0);
display.println("Temperature: " + String(temperature) + " C");
display.println("Humidity: " + String(humidity) + " %");
display.println("eCO2: " + String(sgp.eCO2) + " ppm");
display.println("TVOC: " + String(sgp.TVOC) + " ppb");
display.display();
}
void nodeReadings::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");
errorDHT11 = true;
} else {
Serial.println("DHT11: OK");
errorDHT11 = false;
}
}