cleans up code and adds comments

This commit is contained in:
sietse jonker
2024-02-27 19:17:47 +01:00
parent dc3566f5e5
commit 5558289ba2

View File

@@ -28,7 +28,8 @@ Adafruit_SGP30 sgp;
WebSocketsClient webSocket; WebSocketsClient webSocket;
// define variables // define variables
uint16_t TVOC_base, eCO2_base; uint16_t TVOC_base;
uint16_t eCO2_base;
uint16_t counter = 0; uint16_t counter = 0;
float temperature = 0; float temperature = 0;
float humidity = 0; float humidity = 0;
@@ -36,8 +37,9 @@ uint16_t eCO2 = 0;
uint16_t TVOC = 0; uint16_t TVOC = 0;
bool noise = false; bool noise = false;
bool update = false; bool update = false;
double counterBaseline = 15000; unsigned long currentCounter;
int counterInterval = 5000; unsigned long lastCounter;
uint16_t interval = 5000
// hexdump function for websockets binary handler // hexdump function for websockets binary handler
void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) { 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(){ void websocketSetup(){
WiFiMulti.addAP("iotroam", "vbK9gbDBIB"); WiFiMulti.addAP("iotroam", "vbK9gbDBIB");
@@ -104,6 +107,7 @@ void websocketSetup(){
webSocket.setReconnectInterval(500); webSocket.setReconnectInterval(500);
} }
// fucntion to reset the values if needed
void resetValues() { void resetValues() {
TVOC_base; TVOC_base;
eCO2_base; eCO2_base;
@@ -116,6 +120,45 @@ void resetValues() {
lastCounter = 0; 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() { void displayData() {
// clear display for new info // clear display for new info
display.clearDisplay(); display.clearDisplay();
@@ -134,6 +177,7 @@ void displayData() {
display.display(); display.display();
} }
// setup function
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
@@ -152,6 +196,7 @@ void setup() {
resetValues(); resetValues();
} }
// loop function
void loop() { void loop() {
webSocket.loop(); webSocket.loop();
@@ -179,7 +224,7 @@ void loop() {
// display sensordata on oled screen // display sensordata on oled screen
displayData(); displayData();
if (currentCounter - lastCounter >= 5000){ if (currentCounter - lastCounter >= interval){
lastcounter = millis() lastcounter = millis()
update(); update();
} }