From fbf337f00eb9322ef8a896e51bcd30f50d325595 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Tue, 25 Jun 2024 15:01:05 +0200 Subject: [PATCH] Add working version --- platformio.ini | 20 +++++++++ src/main.cpp | 118 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 platformio.ini create mode 100644 src/main.cpp diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..5a4f32b --- /dev/null +++ b/platformio.ini @@ -0,0 +1,20 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + + +[env:d1_mini] +platform = espressif8266 +board = d1_mini +framework = arduino +monitor_speed = 115200 + +[env] +lib_deps = https://github.com/arduino-libraries/NTPClient + https://github.com/knolleary/pubsubclient \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..d6287cd --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +#include + +#define MQTT_SERVER "mqtt.lan" +#define MQTT_PORT 1883 +#define MQTT_PUB_TOPIC_DISTANCE "luftentfeuchter/distance-to-water" +#define MQTT_PUB_TOPIC_DATE "luftentfeuchter/date" + +#define WIFI_SSID "XXX" +#define WIFI_PASSWORD "XXX" + +WiFiClient espClient; +PubSubClient client(espClient); + +WiFiUDP ntpUDP; +NTPClient timeClient(ntpUDP); + +long lastMsg = 0; +char data[75]; + +void setup() { + // Status LED (onboard) + pinMode(LED_BUILTIN, OUTPUT); + + // MoistureSensor + pinMode(D6, INPUT); + + // HallSensor + pinMode(D7, INPUT); // Echo + pinMode(D8, OUTPUT); // Trigger + + // Relay + pinMode(D2, OUTPUT); + + digitalWrite(LED_BUILTIN, LOW); + + WiFi.setAutoConnect(true); + WiFi.setAutoReconnect(true); + WiFi.hostname("luftentfeuchter-pumpe"); + + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); + while (WiFi.status() != WL_CONNECTED) { + delay(2000); + } + client.setServer(MQTT_SERVER, MQTT_PORT); + + timeClient.begin(); + + digitalWrite(LED_BUILTIN, HIGH); +} + +int connectToMQTT() { + if (client.connected()) { + return 0; + } + + String clientId = "luftentfeuchter-pumpe"; + if (client.connect(clientId.c_str())) { + return 0; + } + + return 1; +} + +int measureDistanceToWater() { + digitalWrite(D8, LOW); + delayMicroseconds(2); + digitalWrite(D8, HIGH); + delayMicroseconds(10); + digitalWrite(D8, LOW); + + long duration = pulseIn(D7, HIGH); + int distance = duration * 0.034 / 2; + + return distance; +} + +bool isMoistureSensorSubmerged() { + return digitalRead(D6) == HIGH; +} + +void loop() { + if (!WiFi.isConnected()) { + WiFi.reconnect(); + } + connectToMQTT(); + timeClient.update(); + + int distance = measureDistanceToWater(); + snprintf(data, sizeof(data), "%d", distance); + client.publish(MQTT_PUB_TOPIC_DISTANCE, data); + + snprintf(data, sizeof(data), "%d", timeClient.getEpochTime()); + client.publish(MQTT_PUB_TOPIC_DATE, data); + + if (distance <= 13) { + // Turn on the status LED + digitalWrite(LED_BUILTIN, LOW); + + // Run the pump for 2 minutes, 45 seconds + digitalWrite(D2, HIGH); + delay((60+60+45) * 1000); + digitalWrite(D2, LOW); + + // Turn the status LED off again + digitalWrite(LED_BUILTIN, HIGH); + } else { + digitalWrite(LED_BUILTIN, HIGH); + digitalWrite(D2, LOW); + } + + // Wait for a minute + delay(60000); +}