Files
J1B3-Sensor-boxes/arduino/node-code/node-code-final.ino
2024-02-14 16:58:07 +01:00

99 lines
1.9 KiB
C++

// Sietse Jonker
// 09/02/2024
#include <Wire.h>
#include <Adafruit_SH110X.h>
#include <Adafruit_SGP30.h>
#include <DHT.h>
#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;
uint16_t counter = 0;
float temperature = 0;
float humidity = 0;
uint16_t eCO2 = 0;
uint16_t 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++;
}