updates node code
This commit is contained in:
@@ -1,74 +1,92 @@
|
|||||||
// Sietse Jonker
|
// Sietse Jonker
|
||||||
// 09/02/2024
|
// 09/02/2024
|
||||||
|
|
||||||
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
|
#include <Wire.h>
|
||||||
#include "SSD1306Wire.h" // legacy: #include "SSD1306.h"
|
#include "SSD1306Wire.h"
|
||||||
#include "Adafruit_SGP30.h"
|
#include "Adafruit_SGP30.h"
|
||||||
#include "images.h"
|
|
||||||
#include "DHT.h"
|
#include "DHT.h"
|
||||||
|
|
||||||
|
#define MICPIN 6
|
||||||
#define DHTPIN 7
|
#define DHTPIN 7
|
||||||
|
#define SCL 9
|
||||||
|
#define SDA 8
|
||||||
|
|
||||||
#define DHTTYPE DHT11
|
#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
|
SSD1306Wire display(0x3c, SDA, SCL);
|
||||||
|
|
||||||
DHT dht(DHTPIN, DHTTYPE);
|
DHT dht(DHTPIN, DHTTYPE);
|
||||||
|
|
||||||
Adafruit_SGP30 sgp;
|
Adafruit_SGP30 sgp;
|
||||||
|
|
||||||
uint32_t getAbsoluteHumidity(float temperature, float humidity) {
|
uint16_t TVOC_base, eCO2_base;
|
||||||
// approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
|
int counter = 0;
|
||||||
const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
|
float temperature = 0;
|
||||||
const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
|
float humidity = 0;
|
||||||
return absoluteHumidityScaled;
|
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(){
|
||||||
|
display.setFont(ArialMT_Plain_16);
|
||||||
|
display.setTextAlignment(TEXT_ALIGN_LEFT);
|
||||||
|
display.drawString(0, 0, String(sgp.eCO2) + " ppm" + " " + String(sgp.TVOC) + " ppb");
|
||||||
|
display.drawString(0, 18, String(temperature) + " C");
|
||||||
|
display.drawString(0, 39, String(humidity) + "%");
|
||||||
|
|
||||||
|
display.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
// Initialising the UI will init the display too.
|
|
||||||
display.init();
|
display.init();
|
||||||
dht.begin();
|
|
||||||
|
|
||||||
display.flipScreenVertically();
|
display.flipScreenVertically();
|
||||||
|
|
||||||
Serial.println("SGP30 test");
|
dht.begin();
|
||||||
|
|
||||||
if (!sgp.begin()) {
|
sgp.begin();
|
||||||
Serial.println("Sensor not found :(");
|
pinMode(MICPIN, INPUT);
|
||||||
while (1)
|
pinMode(DHTPIN, INPUT);
|
||||||
;
|
|
||||||
}
|
resetValues();
|
||||||
Serial.print("Found SGP30 serial #");
|
|
||||||
Serial.print(sgp.serialnumber[0], HEX);
|
display.setFont(ArialMT_Plain_16);
|
||||||
Serial.print(sgp.serialnumber[1], HEX);
|
display.setTextAlignment(TEXT_ALIGN_LEFT);
|
||||||
Serial.println(sgp.serialnumber[2], HEX);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
display.clear();
|
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()) {
|
if (!sgp.IAQmeasure()) {
|
||||||
Serial.println("Measurement failed");
|
Serial.println("SGP30: BAD");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
uint16_t TVOC_base, eCO2_base;
|
Serial.println("SGP30: OK");
|
||||||
if (!sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
|
|
||||||
Serial.println("Failed to get baseline readings");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
temperature = float(dht.readTemperature());
|
||||||
|
humidity = float(dht.readHumidity());
|
||||||
|
|
||||||
|
displayData();
|
||||||
|
|
||||||
|
sgp.getIAQBaseline(&eCO2_base, &TVOC_base);
|
||||||
|
|
||||||
Serial.print("eCO2 ");
|
Serial.print("eCO2 ");
|
||||||
Serial.print(sgp.eCO2);
|
Serial.print(sgp.eCO2);
|
||||||
Serial.println(" ppm");
|
Serial.println(" ppm");
|
||||||
|
|
||||||
delay(1000);
|
delay(100);
|
||||||
|
counter++;
|
||||||
}
|
}
|
Reference in New Issue
Block a user