#include <DHT.h> // Bibliothek für Temperatur- und Feuchtigkeitssensor
#include <Adafruit_NeoPixel.h> // Bibliothek für LED-Ring
//#include <Wire.h> // needed for LCD with PCF8574 port expander
#include <LiquidCrystal_I2C.h> // Bibliothek für LCD-Displays
//LCD Objekt definieren
constexpr byte cols = 20; // Zeichen pro Zeile
constexpr byte rows = 4; // Anzahl Zeilen
constexpr byte addr = 0x27; // LCD Addresse auf 0x3F oder 0x27 setzen
LiquidCrystal_I2C lcd(addr, cols, rows); // LCD-Objekt erstellen anhand der oben gegebenen Faktoren
// this example uses buttons to navigate through the menu
constexpr byte upButton = A8;
constexpr byte downButton = A10;
constexpr byte backButton = A11;
constexpr byte selectButton = A9;
constexpr byte encoderButton = 4;
/////////////////HIER
int updatetimer = 2000; // Häufigkeit der Messungen, 2000 = 1 Sekunde
DHT sensor_dht22(13, DHT22); // <- Pin, Typ von Temperatur- und Feuchtigkeitssensor (DHT22)
const float GAMMA = 0.7; // Für Auslese der Helligkeit benötigte Werte
const float RL10 = 50;
#define sensor_illumination A0 //<- Pin von Helligkeitssensor
float temperature, humidity;
float max_temperature = 45.00;
float max_humidity = 75.00;
Adafruit_NeoPixel ledring = Adafruit_NeoPixel(16, 0, NEO_GRB + NEO_KHZ800); // <- Anzahl Pixel, Pin von LED-Ring
//-- Pinbelegungen ------------------------------------------------------
// Temperatur- und Feuchtigkeitssensor (DHT22):
// GND -> GND.1
// SDA -> 13
// VCC -> 5V
// NC -> /
// Helligkeitssensor (Photoresistor LDR Sensor):
// AO -> A0
// DO -> /
// GND -> GND.2
// VCC -> 5V
// LED Ring (WS2812):
// GND -> GND.1
// VCC -> 5V
// DIN -> 0
// Alarm
// LED
// C -> GND.1
// A -> 1
//----------------------------------------------------------------------
void setup() {
// Übertragungsgeschwindigkeit festlegen:
Serial.begin(9600);
// Sensorenbibliothek laden:
sensor_dht22.begin();
// LED einrichten:
pinMode(1, OUTPUT);
}
void loop() {
//-- Temperaturmessung ---------------------------------------------------
Serial.print("Temperatur: ");
temperature = sensor_dht22.readTemperature();
Serial.print(temperature);
Serial.print("°C\n");
//-- Feuchtigskeitsmessung -----------------------------------------------
Serial.print("Luftfeuchtigkeit: ");
humidity = sensor_dht22.readHumidity();
Serial.print(humidity);
Serial.print("%\n");
//-- Helligkeitsmessung -----------------------------------------------
int helligkeit_analog = analogRead(sensor_illumination);
float voltage = helligkeit_analog / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.print("Helligkeit: ");
Serial.print(lux);
Serial.print(" Lux\n");
//-- LED-Ring ---------------
set_ledring(5);
//-----------------------------------------------------------------------
set_alarm();
delay(updatetimer);
}
//-- Steuerung der Alarmelemente -----------------------------------------
void set_alarm() {
if(temperature > max_temperature || humidity > max_humidity) {
// LED einschalten
digitalWrite(1, HIGH);
Serial.println("LED ist eingeschaltet!");
// Buzzer einschalten
}
else {
// LED ausschalten
digitalWrite(1, LOW);
Serial.println("LED ist ausgeschaltet!");
// Buzzer ausschalten
}
}
//-- Steuerung des LED-Rings ---------------------------------------------
void set_ledring(int amount) {
ledring.clear();
for (int i = amount - 1; i >= 0; i--) {
ledring.setPixelColor(i, Adafruit_NeoPixel::Color(255, 0, 0));
ledring.show();
}
}
//-- Umwandlung der Lux-Werte für den LED-Ring --------------------------
float lux_to_16(float lux) {
float lux16;
return lux16;
}