Initial commit ESP32 Feuchtigkeit Sensor Projekt
This commit is contained in:
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.pio
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
10
.vscode/extensions.json
vendored
Normal file
10
.vscode/extensions.json
vendored
Normal file
@@ -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"
|
||||
]
|
||||
}
|
||||
29
docs/01_installation.md
Normal file
29
docs/01_installation.md
Normal file
@@ -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.
|
||||
48
docs/02_home_assistant_integration.md
Normal file
48
docs/02_home_assistant_integration.md
Normal file
@@ -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!"
|
||||
```
|
||||
8
docs/03_troubleshooting.md
Normal file
8
docs/03_troubleshooting.md
Normal file
@@ -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 |
|
||||
19
docs/04_developer_notes.md
Normal file
19
docs/04_developer_notes.md
Normal file
@@ -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
|
||||
39
include/README
Normal file
39
include/README
Normal file
@@ -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
|
||||
46
lib/README
Normal file
46
lib/README
Normal file
@@ -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 <Foo.h>
|
||||
#include <Bar.h>
|
||||
|
||||
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
|
||||
12
platformio.ini
Normal file
12
platformio.ini
Normal file
@@ -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
|
||||
120
src/main.cpp
Normal file
120
src/main.cpp
Normal file
@@ -0,0 +1,120 @@
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
// -------- 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);
|
||||
}
|
||||
167
test/README
Normal file
167
test/README
Normal file
@@ -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)**
|
||||
Reference in New Issue
Block a user