Add working version

This commit is contained in:
Aaron Fischer 2024-06-25 15:01:05 +02:00
commit fbf337f00e
2 changed files with 138 additions and 0 deletions

20
platformio.ini Normal file
View file

@ -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

118
src/main.cpp Normal file
View file

@ -0,0 +1,118 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#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);
}