137 lines
2.5 KiB
C++
Executable File
137 lines
2.5 KiB
C++
Executable File
#include <Wire.h>
|
|
#include <Adafruit_Sensor.h>
|
|
#include <Adafruit_BMP280.h>
|
|
#include <DHT.h>
|
|
|
|
// ================= SENSOR AKTIVIERUNG =================
|
|
#define USE_DHT22 1
|
|
#define USE_RAIN 1
|
|
#define USE_BMP280 0 // statt USE_SOIL
|
|
|
|
// ================= DHT22 =================
|
|
#define DHT22PIN 4
|
|
#define DHT22TYPE DHT22
|
|
DHT dht22(DHT22PIN, DHT22TYPE);
|
|
|
|
// ================= Regenmodul =================
|
|
const int rainAnalogPin = A1;
|
|
const int rainDigitalPin = 2;
|
|
|
|
// ================= BMP280 =================
|
|
Adafruit_BMP280 bmp;
|
|
|
|
// ---------------- SENSOR FUNKTIONEN ----------------
|
|
|
|
#if USE_DHT22
|
|
void readDHT22(float &temp, float &hum) {
|
|
temp = dht22.readTemperature();
|
|
hum = dht22.readHumidity();
|
|
}
|
|
#endif
|
|
|
|
#if USE_RAIN
|
|
void readRainSensor(int &analogValue, int &digitalValue) {
|
|
analogValue = analogRead(rainAnalogPin);
|
|
digitalValue = digitalRead(rainDigitalPin);
|
|
}
|
|
#endif
|
|
|
|
#if USE_BMP280
|
|
void readBMP280(float &pressure) {
|
|
pressure = bmp.readPressure() / 100.0F; // Pa → hPa
|
|
}
|
|
#endif
|
|
|
|
// ---------------- JSON BUILDER ----------------
|
|
|
|
String buildJSON(float t22, float h22,
|
|
int rainA, int rainD,
|
|
float pressure) {
|
|
|
|
String json = "{";
|
|
|
|
// ===== DHT22 =====
|
|
json += "\"dht22\":{";
|
|
json += "\"temp\":" + String(t22, 2) + ",";
|
|
json += "\"hum\":" + String(h22, 2);
|
|
json += "},";
|
|
|
|
// ===== Regen =====
|
|
json += "\"rain\":{";
|
|
json += "\"analog\":" + String(rainA) + ",";
|
|
json += "\"wet\":";
|
|
json += (rainD == LOW ? "true" : "false");
|
|
json += "},";
|
|
|
|
// ===== BMP280 =====
|
|
json += "\"bmp280\":{";
|
|
json += "\"pressure\":" + String(pressure, 2);
|
|
json += "}";
|
|
|
|
json += "}";
|
|
|
|
return json;
|
|
}
|
|
|
|
// ---------------- SETUP ----------------
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
|
|
#if USE_DHT22
|
|
dht22.begin();
|
|
#endif
|
|
|
|
#if USE_RAIN
|
|
pinMode(rainDigitalPin, INPUT);
|
|
#endif
|
|
|
|
#if USE_BMP280
|
|
/*if (!bmp.begin(0x77)) {
|
|
Serial.println("BMP280 nicht gefunden!");
|
|
}*/
|
|
|
|
bool bmp_ok = false;
|
|
|
|
|
|
bmp_ok = bmp.begin(0x76);
|
|
|
|
if (!bmp_ok) {
|
|
bmp_ok = bmp.begin(0x77);
|
|
}
|
|
|
|
if (bmp_ok) {
|
|
Serial.println("BMP280 OK");
|
|
} else {
|
|
Serial.println("BMP280 NICHT GEFUNDEN");
|
|
}
|
|
#endif
|
|
|
|
Serial.println("System gestartet...");
|
|
}
|
|
|
|
// ---------------- LOOP ----------------
|
|
|
|
void loop() {
|
|
delay(2000);
|
|
|
|
float t22 = 0, h22 = 0;
|
|
int rainA = 0, rainD = 0;
|
|
float pressure = 0;
|
|
|
|
#if USE_DHT22
|
|
readDHT22(t22, h22);
|
|
#endif
|
|
|
|
#if USE_RAIN
|
|
readRainSensor(rainA, rainD);
|
|
#endif
|
|
|
|
#if USE_BMP280
|
|
readBMP280(pressure);
|
|
#endif
|
|
|
|
String json = buildJSON(t22, h22, rainA, rainD, pressure);
|
|
|
|
Serial.println(json);
|
|
} |