75 lines
2.1 KiB
C++
75 lines
2.1 KiB
C++
// Sietse Jonker
|
|
// 09/02/2024
|
|
|
|
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
|
|
#include "SSD1306Wire.h" // legacy: #include "SSD1306.h"
|
|
#include "Adafruit_SGP30.h"
|
|
#include "images.h"
|
|
#include "DHT.h"
|
|
|
|
#define DHTPIN 7
|
|
|
|
#define DHTTYPE DHT11
|
|
|
|
SSD1306Wire display(0x3c, SDA, SCL); // ADDRESS, SDA, SCL - SDA and SCL usually populate automatically based on your board's pins_arduino.h e.g. https://github.com/esp8266/Arduino/blob/master/variants/nodemcu/pins_arduino.h
|
|
DHT dht(DHTPIN, DHTTYPE);
|
|
Adafruit_SGP30 sgp;
|
|
|
|
uint32_t getAbsoluteHumidity(float temperature, float humidity) {
|
|
// approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
|
|
const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
|
|
const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
|
|
return absoluteHumidityScaled;
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
// Initialising the UI will init the display too.
|
|
display.init();
|
|
dht.begin();
|
|
|
|
display.flipScreenVertically();
|
|
|
|
Serial.println("SGP30 test");
|
|
|
|
if (!sgp.begin()) {
|
|
Serial.println("Sensor not found :(");
|
|
while (1)
|
|
;
|
|
}
|
|
Serial.print("Found SGP30 serial #");
|
|
Serial.print(sgp.serialnumber[0], HEX);
|
|
Serial.print(sgp.serialnumber[1], HEX);
|
|
Serial.println(sgp.serialnumber[2], HEX);
|
|
}
|
|
|
|
void loop() {
|
|
display.clear();
|
|
|
|
display.setFont(ArialMT_Plain_24);
|
|
display.setTextAlignment(TEXT_ALIGN_LEFT);
|
|
display.drawString(0, 0, String(sgp.eCO2) + "ppm");
|
|
display.drawString(0, 32, String(dht.readTemperature()));
|
|
|
|
display.display();
|
|
|
|
if (!sgp.IAQmeasure()) {
|
|
Serial.println("Measurement failed");
|
|
return;
|
|
}
|
|
|
|
uint16_t TVOC_base, eCO2_base;
|
|
if (!sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
|
|
Serial.println("Failed to get baseline readings");
|
|
return;
|
|
}
|
|
|
|
|
|
Serial.print("eCO2 ");
|
|
Serial.print(sgp.eCO2);
|
|
Serial.println(" ppm");
|
|
|
|
delay(1000);
|
|
}
|