From 23a3bc2cb23820c24f0ee31414ec9a5ed8208a9c Mon Sep 17 00:00:00 2001 From: Sietse Jonker Date: Wed, 14 Feb 2024 14:07:53 +0100 Subject: [PATCH] Add Arduino code for environmental monitoring --- arduino/node-code/node-code-final.ino | 98 +++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/arduino/node-code/node-code-final.ino b/arduino/node-code/node-code-final.ino index e69de29..ecaa647 100644 --- a/arduino/node-code/node-code-final.ino +++ b/arduino/node-code/node-code-final.ino @@ -0,0 +1,98 @@ +// Sietse Jonker +// 09/02/2024 + +#include +#include +#include +#include + +#define MICPIN 6 +#define DHTPIN 7 +#define SCL 9 +#define SDA 8 +#define DHTTYPE DHT11 +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 +#define i2c_adress 0x3c + +Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire); +DHT dht(DHTPIN, DHTTYPE); + +Adafruit_SGP30 sgp; + +uint16_t TVOC_base, eCO2_base; +int counter = 0; +float temperature = 0; +float humidity = 0; +int eCO2 = 0; +int TVOC = 0; +bool noise = false; + +void resetValues() { + TVOC_base, eCO2_base; + counter = 0; + temperature = 0; + humidity = 0; + eCO2 = 0; + TVOC = 0; + noise = false; +} + +void displayData() { + // clear display for new info + display.clearDisplay(); + + // set custom text properties + display.setTextSize(2); + display.setTextColor(SH110X_WHITE); + display.setCursor(0, 0); + + // display info on oled screen + display.println((String) "Temp: " + int(temperature) + "C" + '\n' + + "Humi: " + int(humidity) + "%" + '\n' + + "eCO2: " + int(sgp.eCO2) + '\n' + + "TVOC: " + int(sgp.TVOC)); + + display.display(); +} + +void setup() { + 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); + + resetValues(); +} + +void loop() { + // if sensor doesnt work print in serialmonitor + if (!sgp.IAQmeasure()) { + Serial.println("SGP30: BAD"); + return; + } else { + Serial.println("SGP30: OK"); + } + + // read dht11 sensor + temperature = float(dht.readTemperature()); + humidity = float(dht.readHumidity()); + + // display sensordata on oled screen + displayData(); + + // get new baseline every 30 seconds + if (counter == 30) { + sgp.getIAQBaseline(&eCO2_base, &TVOC_base); + } + delay(1000); + counter++; +} \ No newline at end of file