commit 10bfd3075a4d594cbe9d483f8fd29691bd29989a Author: Ismail Ali Date: Wed Mar 11 21:40:51 2026 +0100 Initial commit ESP32 Feuchtigkeit Sensor Projekt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/docs/01_installation.md b/docs/01_installation.md new file mode 100644 index 0000000..20d0c3f --- /dev/null +++ b/docs/01_installation.md @@ -0,0 +1,29 @@ +# đŸ§© Installation + +## Voraussetzungen +- Visual Studio Code mit PlatformIO +- WLAN und Home Assistant im gleichen Netzwerk +- MQTT-Broker (z. B. Mosquitto) aktiv +- Benutzername und Passwort fĂŒr MQTT vorhanden + +## Schritte +1. **Projekt klonen oder entpacken** + ```bash + git clone https://github.com/deinuser/esp32-feuchtesensor.git + ``` +2. **In VS Code öffnen** + Öffne den Projektordner in Visual Studio Code → PlatformIO wird automatisch aktiv. +3. **WLAN- und MQTT-Zugangsdaten anpassen** + In `src/main.cpp` bearbeiten: + ```cpp + const char* ssid = "DEIN_WLAN"; + const char* password = "DEIN_PASSWORT"; + const char* mqtt_server = "192.168.x.x"; + const char* mqtt_user = "mqttuser"; + const char* mqtt_pass = "mqttpasswort"; + ``` +4. **ESP32 anschließen und flashen** + Über **PlatformIO → Upload** kompilieren und auf den ESP32 hochladen. + Serielle Ausgabe mit `PlatformIO → Monitor` prĂŒfen (Baudrate 115200). +5. **Verbindung prĂŒfen** + Nach dem Neustart sollte der ESP32 die MQTT-Themen automatisch an den Broker senden. diff --git a/docs/02_home_assistant_integration.md b/docs/02_home_assistant_integration.md new file mode 100644 index 0000000..3fba784 --- /dev/null +++ b/docs/02_home_assistant_integration.md @@ -0,0 +1,48 @@ +# 🏠 Home Assistant Integration + +## MQTT-Integration aktivieren +- Einstellungen → GerĂ€te & Dienste → Integration hinzufĂŒgen → MQTT +- Falls Home Assistant keinen eigenen Broker nutzt, installiere den **Mosquitto-Add-on**. + +## EntitĂ€ten definieren +In `configuration.yaml`: +```yaml +binary_sensor: + - name: "Feuchte Alarm" + state_topic: "home/feuchte/alarm" + payload_on: "1" + payload_off: "0" + device_class: moisture + +sensor: + - name: "Feuchte Sensor 1" + state_topic: "home/feuchte/sensor1" + unit_of_measurement: "%" + - name: "Feuchte Sensor 2" + state_topic: "home/feuchte/sensor2" + unit_of_measurement: "%" + - name: "Feuchte Sensor 3" + state_topic: "home/feuchte/sensor3" + unit_of_measurement: "%" + - name: "Feuchte Sensor 4" + state_topic: "home/feuchte/sensor4" + unit_of_measurement: "%" +``` + +## Anzeige im Dashboard +1. Übersicht → Bearbeiten → Karte hinzufĂŒgen → EntitĂ€t. +2. Suche nach `feuchte` → Sensoren oder Alarm auswĂ€hlen. + +## Beispiel-Automation +```yaml +automation: + - alias: "Feuchtealarm" + trigger: + - platform: state + entity_id: binary_sensor.feuchte_alarm + to: "on" + action: + - service: notify.mobile_app_iphone + data: + message: "🚹 Feuchte erkannt im Gartenbereich!" +``` diff --git a/docs/03_troubleshooting.md b/docs/03_troubleshooting.md new file mode 100644 index 0000000..ada9ec8 --- /dev/null +++ b/docs/03_troubleshooting.md @@ -0,0 +1,8 @@ +# 🔧 Troubleshooting + +| Problem | Ursache | Lösung | +|----------|----------|---------| +| Keine MQTT-Daten in HA | Broker nicht erreichbar | PrĂŒfe IP-Adresse und Zugangsdaten in `main.cpp` | +| ESP32 verbindet sich nicht mit WLAN | SSID oder Passwort falsch | Werte prĂŒfen, ggf. Serial Monitor ansehen | +| Sensorwerte zu hoch/niedrig | Kalibrierung nötig | Werte `V_TROCKEN` und `V_NASS` anpassen | +| Fehlermeldung `filename` in HA | YAML-Dateiname unzulĂ€ssig | Achte auf Kleinbuchstaben und `.yaml`-Endung | diff --git a/docs/04_developer_notes.md b/docs/04_developer_notes.md new file mode 100644 index 0000000..7cd8584 --- /dev/null +++ b/docs/04_developer_notes.md @@ -0,0 +1,19 @@ +# 🧠 Entwicklerhinweise + +## Kalibrierung der Sensoren +```cpp +float V_TROCKEN = 2.80f; // Spannung an Luft +float V_NASS = 1.20f; // Spannung im Wasser +float ALERT_THRESHOLD = 70.0f; // Alarm ab 70% +``` +Passe die Werte so an, dass „Trocken“ und „Nass“ korrekt erkannt werden. + +## Erweiterungsideen +- **Mehr Sensoren:** bis zu 4 gleichzeitig ĂŒber analoge EingĂ€nge (GPIO 32–35) +- **Bluetooth-UnterstĂŒtzung:** statt WLAN kann BLE genutzt werden (z. B. via ESPHome) +- **Relaissteuerung:** Schalte BewĂ€sserungspumpe bei Alarm automatisch ein +- **OLED-Display:** Anzeige der Feuchtewerte direkt am GerĂ€t + +📘 **Autor:** Ismail Ali +📅 Stand: November 2025 +Lizenz: MIT License diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..2593a33 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..00307ea --- /dev/null +++ b/platformio.ini @@ -0,0 +1,12 @@ +[env:upesy_wroom] +platform = espressif32 +board = upesy_wroom +framework = arduino + +upload_port = COM4 ; <-- Anpassen falls dein Port anders ist +monitor_port = COM4 +monitor_speed = 115200 +monitor_filters = direct + +lib_deps = + knolleary/PubSubClient@^2.8 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..2cae490 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,120 @@ +#include +#include +#include + +// -------- WLAN-DATEN -------- +const char* ssid = "FRITZ!Box 5530 QT"; +const char* password = "14749662370082380252"; + +// -------- MQTT-DATEN -------- +const char* mqtt_server = "192.168.10.117"; // IP von Home Assistant +const int mqtt_port = 1883; +const char* mqtt_user = "mqttesp32"; // ohne Leerzeichen besser +const char* mqtt_pass = "Jasmin28799"; + +WiFiClient espClient; +PubSubClient client(espClient); + +// -------- Sensor-Konfiguration -------- +const int PINS[] = {32, 33, 34, 35}; +const int NUM_SENSORS = sizeof(PINS) / sizeof(PINS[0]); +float V_TROCKEN = 2.80f; // Kalibrieren: an Luft messen +float V_NASS = 1.20f; // Kalibrieren: in Wasser messen +float ALERT_THRESHOLD = 70.0f; // >=70% = nass -> Alarm +const int ADC_RESOLUTION = 12; +const float VREF = 3.30f; +const int SAMPLES = 16; + +// -------- Funktionen -------- +float readAdcVoltage(int pin) { + uint32_t sum = 0; + for (int i = 0; i < SAMPLES; i++) { + sum += analogRead(pin); + delay(2); + } + float raw = (float)sum / (float)SAMPLES; + return (raw / ((1 << ADC_RESOLUTION) - 1)) * VREF; +} + +float mapToPercent(float v, float dry, float wet) { + float pct = 100.0f * (dry - v) / (dry - wet); + if (pct < 0) pct = 0; + if (pct > 100) pct = 100; + return pct; +} + +// -------- WLAN / MQTT Setup -------- +void reconnect() { + while (!client.connected()) { + Serial.print("Verbinde mit MQTT..."); + if (client.connect("ESP32_Feuchte", mqtt_user, mqtt_pass)) { + Serial.println(" verbunden!"); + } else { + Serial.print("Fehler, rc="); + Serial.print(client.state()); + Serial.println(" -> warte 5s"); + delay(5000); + } + } +} + +void setup_wifi() { + delay(10); + Serial.println(); + Serial.print("Verbinde mit WLAN "); + Serial.println(ssid); + + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("✅ WLAN verbunden!"); + Serial.print("IP-Adresse: "); + Serial.println(WiFi.localIP()); +} + +void setup() { + Serial.begin(115200); + delay(1000); + Serial.println("\nESP32 Feuchte-MQTT-Logger startet..."); + + analogReadResolution(ADC_RESOLUTION); + analogSetAttenuation(ADC_11db); + + setup_wifi(); + client.setServer(mqtt_server, mqtt_port); +} + +void loop() { + if (!client.connected()) reconnect(); + client.loop(); + + float volt[NUM_SENSORS]; + float pct[NUM_SENSORS]; + bool alarm = false; + + for (int i = 0; i < NUM_SENSORS; i++) { + volt[i] = readAdcVoltage(PINS[i]); + pct[i] = mapToPercent(volt[i], V_TROCKEN, V_NASS); + if (pct[i] >= ALERT_THRESHOLD) alarm = true; + + // MQTT-Publish fĂŒr jeden Sensor + char topic[64]; + snprintf(topic, sizeof(topic), "home/feuchte/sensor%d", i + 1); + char payload[32]; + snprintf(payload, sizeof(payload), "%.1f", pct[i]); + client.publish(topic, payload); + } + + client.publish("home/feuchte/alarm", alarm ? "1" : "0"); + + Serial.print("Alarm: "); + Serial.println(alarm ? "JA" : "nein"); + + delay(5000); +} diff --git a/test/README b/test/README new file mode 100644 index 0000000..20a56ca --- /dev/null +++ b/test/README @@ -0,0 +1,167 @@ +# đŸŒĄïž ESP32 Feuchtesensor mit MQTT & Home Assistant + +Dieses Projekt liest mehrere analoge Feuchtesensoren ĂŒber den **ESP32** aus und sendet die Messwerte per **MQTT** an **Home Assistant**. +Ein definierter Schwellenwert löst zusĂ€tzlich einen **Feuchtealarm** aus. + +--- + +## ⚙ Hardware + +- ESP32 Dev-Board (z. B. UPesy Wroom) +- bis zu **4 analoge Bodenfeuchtesensoren** +- USB-Kabel fĂŒr Programmierung +- WLAN mit Zugang zu Home Assistant (im gleichen Netzwerk) + +--- + +## đŸ§™â€â™‚ïž Projektstruktur + +``` +ESP32-FEUCHTE/ +├── src/ +│ └── main.cpp # Hauptprogramm mit Sensorlogik und MQTT +├── platformio.ini # Build- und Upload-Konfiguration +├── lib/ # (optional) weitere Libraries +├── test/ # Tests +├── README.md # Projektbeschreibung (diese Datei) +└── docs/ # Entwickler- und Benutzerhandbuch +``` + +--- + +## ⚖ Einrichtung + +### 1. PlatformIO öffnen + +Projekt im VS Code öffnen und sicherstellen, dass die Erweiterung **PlatformIO** installiert ist. + +### 2. WLAN- und MQTT-Daten anpassen + +In `main.cpp` die folgenden Werte Ă€ndern: + +```cpp +const char* ssid = "DEIN_WLAN"; +const char* password = "DEIN_PASSWORT"; +const char* mqtt_server = "192.168.x.x"; // IP deines Home Assistant +const char* mqtt_user = "mqttuser"; +const char* mqtt_pass = "mqttpasswort"; +``` + +### 3. ESP32 flashen + +USB-Port im `platformio.ini` prĂŒfen: + +```ini +upload_port = COM4 +monitor_speed = 115200 +``` + +Dann ĂŒber **PlatformIO → Upload** das Programm kompilieren und flashen. + +--- + +## 🌐 MQTT-Struktur + +Der ESP32 veröffentlicht folgende Topics: + +| Topic | Beschreibung | Beispiel | +| ---------------------- | ------------------------------ | -------- | +| `home/feuchte/sensor1` | Feuchtewert Sensor 1 (0–100 %) | `42.3` | +| `home/feuchte/sensor2` | Feuchtewert Sensor 2 (0–100 %) | `85.7` | +| `home/feuchte/sensor3` | Feuchtewert Sensor 3 (0–100 %) | `10.4` | +| `home/feuchte/sensor4` | Feuchtewert Sensor 4 (0–100 %) | `70.1` | +| `home/feuchte/alarm` | 1 = Feucht, 0 = Trocken | `1` | + +--- + +## 🏠 Integration in Home Assistant + +1. **MQTT-Integration aktivieren** + → Einstellungen → GerĂ€te & Dienste → Integration hinzufĂŒgen → MQTT + → Broker konfigurieren oder den internen Broker von Home Assistant nutzen. + +2. **EntitĂ€ten anlegen** (in `configuration.yaml` oder per GUI): + +```yaml +binary_sensor: + - name: "Feuchte Alarm" + state_topic: "home/feuchte/alarm" + payload_on: "1" + payload_off: "0" + device_class: moisture + +sensor: + - name: "Feuchte Sensor 1" + state_topic: "home/feuchte/sensor1" + unit_of_measurement: "%" + - name: "Feuchte Sensor 2" + state_topic: "home/feuchte/sensor2" + unit_of_measurement: "%" + - name: "Feuchte Sensor 3" + state_topic: "home/feuchte/sensor3" + unit_of_measurement: "%" + - name: "Feuchte Sensor 4" + state_topic: "home/feuchte/sensor4" + unit_of_measurement: "%" +``` + +3. **Neuladen:** + → Entwicklerwerkzeuge → YAML → _Manuell konfigurierte MQTT-EntitĂ€ten neu laden_ + +4. **PrĂŒfen:** + → Entwicklerwerkzeuge → ZustĂ€nde → Suchbegriff „feuchte“ + → Werte sollten live aktualisiert werden. + +--- + +## 🔔 Optionale Automationen + +Beispiel: Benachrichtigung, wenn Feuchtealarm aktiv ist: + +```yaml +automation: + - alias: "Feuchtealarm" + trigger: + - platform: state + entity_id: binary_sensor.feuchte_alarm + to: "on" + action: + - service: notify.mobile_app_iphone + data: + message: "🚹 Feuchte erkannt! Bitte prĂŒfen!" +``` + +--- + +## 🧠 Entwicklerhinweise + +- **Kalibrierung:** + Werte in `main.cpp` anpassen: + ```cpp + float V_TROCKEN = 2.80f; // Spannung an Luft + float V_NASS = 1.20f; // Spannung im Wasser + float ALERT_THRESHOLD = 70.0f; // Alarm ab 70% + ``` +- **Sampling:** + Jeder Sensor wird 16× gemessen und gemittelt (`SAMPLES = 16`). + +- **Logging:** + Serielle Ausgabe mit `monitor_speed = 115200`. + +--- + +## 📘 Lizenz + +MIT License +Autor: Ismail Ali +© 2025 + +--- + +## 📂 Weitere Dokumentation + +Siehe `docs/`-Ordner fĂŒr: + +- **Installationsanleitung Schritt-fĂŒr-Schritt** +- **Troubleshooting (WLAN, MQTT, HA)** +- **Kalibrierung & Erweiterung (mehr Sensoren, Relais, Displays)**