update arduino file

This commit is contained in:
2026-04-27 12:08:54 +02:00
parent 47f876f533
commit e1400e81e8
3 changed files with 156 additions and 99 deletions

137
arduino/arduino.ino Normal file
View File

@@ -0,0 +1,137 @@
#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);
}

View File

@@ -1,99 +0,0 @@
#include <DHT.h>
// ================= DHT22 =================
#define DHT22PIN 4
#define DHT22TYPE DHT22
DHT dht22(DHT22PIN, DHT22TYPE);
// ================= DHT11 (deaktiviert) =================
// #define DHT11PIN 3
// #define DHT11TYPE DHT11
// DHT dht11(DHT11PIN, DHT11TYPE);
// ================= Regensensor =================
const int rainAnalogPin = A5;
const int rainDigitalPin = 2;
// ---------------- SETUP ----------------
void setup() {
Serial.begin(9600);
dht22.begin();
// dht11.begin(); // deaktiviert
pinMode(rainDigitalPin, INPUT);
Serial.println("System gestartet...");
}
// ---------------- LOOP ----------------
void loop() {
delay(2000);
readDHT22();
// readDHT11(); // deaktiviert
readRainSensor();
Serial.println("------------------------");
}
void readDHT22() {
float temp = dht22.readTemperature();
float hum = dht22.readHumidity();
Serial.println("DHT22:");
if (isnan(temp) || isnan(hum)) {
Serial.println(" Fehler beim Lesen!");
return;
}
Serial.print(" Temperatur: ");
Serial.print(temp);
Serial.println(" °C");
Serial.print(" Feuchte: ");
Serial.print(hum);
Serial.println(" %");
}
/*
void readDHT11() {
float temp = dht11.readTemperature();
float hum = dht11.readHumidity();
Serial.println("DHT11:");
if (isnan(temp) || isnan(hum)) {
Serial.println(" Fehler beim Lesen!");
return;
}
Serial.print(" Temperatur: ");
Serial.print(temp);
Serial.println(" °C");
Serial.print(" Feuchte: ");
Serial.print(hum);
Serial.println(" %");
}
*/
void readRainSensor() {
int analogValue = analogRead(rainAnalogPin);
int digitalValue = digitalRead(rainDigitalPin);
Serial.println("Regenmodul:");
Serial.print(" Analogwert: ");
Serial.println(analogValue);
if (digitalValue == LOW) {
Serial.println(" Regen erkannt!");
} else {
Serial.println(" Kein Regen");
}
}