Hallo zusammen
wir sind sicher nicht die einzigen, welche z.B. einen Kriechkeller haben der durch den Regen drohte überzulaufen
Ich habe daher aus meiner Kramkiste ein 1.77" TFT, einen ESP32 und einen HC-SR04 gekramt und mir eine Anzeige gebaut
Die Ausgabe erfolgt über den Monitor, der Hintergrund wird rot, sobald der Wasserspiegel steigt. Zusätzlich habe ich das an meinen MQTT Server in Openhab angebunden.
Da ich immer noch ziemlich am Anfang der Programmierung des ESPs stehe möge man mir den ein oder anderen Fehler verzeihen.
Alles funktioniert wie es soll, ich bin nur mit der Anzeige des Datums / der Zeit unzufrieden.
Evtl. hat jemand da noch einen Hinweis für mich
Aber nun zum Aufbau.
Das Radar HC-SR04 ist im Kriechkeller montiert, die Anzeige habe ich zusammen mit dem ESP32 in ein selbst gedrucktes Gehäuse eingebaut.
Nachstehend der Code, falls jemand nach was ähnlichem sucht.
/*
Pinout HC-SR04
Sensor ESP32
MINUS GND
ECHO 22
TRIG 5
VCC 5v
PINOUT TFT 1.77
* Display ESP32
LEDA 3,3V
CS 17
RS 2
RES 14
MOSI SDA 23
SCL SCK 18
VCC 5V
GND GND
*/
/*********
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-hc-sr04-ultrasonic-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*********/
const int trigPin = 5;
const int echoPin = 22;
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
long duration;
float distanceCm;
const int WasserHoehe = 22; //Wasserhöhe in CM
const int distanceOffset = 30; // Abstand Sensor zum Wasser in cm, Annahme hier 22 + 30 = 52cm Sensor bis Boden
float Wasser;
// Define meter size
#define M_SIZE 0.667
#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
#define TFT_GREY 0x5AEB
#define TFT_ORANGE 0xFD20 /* 255, 165, 0 */
float ltx = 0; // Saved x coord of bottom of needle
uint16_t osx = M_SIZE*120, osy = M_SIZE*120; // Saved x & y coords
uint32_t updateTime = 0; // time for next update
int old_analog = -999; // Value last displayed
int value[6] = {0, 0, 0, 0, 0, 0};
int old_value[6] = { -1, -1, -1, -1, -1, -1};
int d = 0;
#include <WiFi.h>
#include <PubSubClient.h>
#include <time.h>
WiFiClient espClient;
PubSubClient client(espClient);
#ifndef STASSID
#define STASSID "SID des WLAN"
#define STAPSK "PWD des WLAN
String newHostname = "Monitor-kriechkeller";
#endif
const char* mqtt_server = "IP des MQTT Servers";
const int mqtt_port = 1883;
const char* mqtt_user = "MQTT User";
const char* mqtt_password = "MQTT PWD";
char* clientId = "Monitor-Kriechkeller";
const char* ssid = STASSID;
const char* password = STAPSK;
#define POWER_PIN 17 // ESP32 pin GIOP17 connected to sensor's VCC pin
#define SIGNAL_PIN 34 // ESP32 pin GIOP36 (ADC0) connected to sensor's signal pin
char tmp[50];
char sensorvalue[50];
//int sensorvalue = 0; // variable to store the sensor value
//int sensorvalue = 0;
char time_value[20];
int pinvalue = 0; // variable to store the sensor value
unsigned long int currentMillis = millis();
const unsigned long period = 50000; // führe nach x nochmals ein Event aus
void connectToMQTT() {
client.setServer(mqtt_server, mqtt_port);//MQTT Server, - Port
if (client.connect(clientId , mqtt_user, mqtt_password)) {
Serial.println("connected");
}
}
void setup(void) {
Serial.begin(115200); // For debug
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
/* Explicitly set the ESP32 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.hostname(newHostname.c_str());
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//Serial.begin(9600);
pinMode(POWER_PIN, OUTPUT); // configure pin as an OUTPUT
digitalWrite(POWER_PIN, LOW); // turn the sensor OFF
// Aktuelle Uhrzeit über den Zeitserver ermitteln
configTime(2 * 3600, 0, "0.de.pool.ntp.org", "ptbtime1.ptb.de");
delay(1000);
time_t now = time(nullptr);
String time = String(ctime(&now));
time.trim();
time.substring(0,19).toCharArray(time_value, 20);
connectToMQTT();
long rssi = WiFi.RSSI();
itoa(rssi,tmp,10);
client.publish("Kriechkeller/rssi", tmp);
client.publish("Kriechkeller/waterlevel", sensorvalue);
client.publish("Kriechkeller/time", time_value);
tft.init();
tft.setRotation(3);
tft.fillScreen(TFT_BLACK);
updateTime = millis(); // Next update time
}
void gettime(){
configTime(2 * 3600, 0, 0, "192.53.103.108");
// delay(1000);
time_t now = time(nullptr);
String time = String(ctime(&now));
time.trim();
time.substring(0,19).toCharArray(time_value, 20);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distanceCm = duration * SOUND_SPEED/2;
Wasser = ((distanceCm * -1) + distanceOffset + WasserHoehe);
dtostrf(Wasser, 6, 2, sensorvalue); // Leave room for too large numbers!
client.publish("Kriechkeller/rssi", tmp);
client.publish("Kriechkeller/waterlevel", sensorvalue);
client.publish("Kriechkeller/Message", "Wasser im Kriechkeller");
client.publish("Kriechkeller/time", time_value);
//TFT Section
tft.setCursor(0, 0);
// Set the font colour to be white with a black background, set text size multiplier to 1
if (Wasser <= 0)
{
client.publish("Kriechkeller/rssi", tmp);
client.publish("Kriechkeller/waterlevel", sensorvalue);
client.publish("Kriechkeller/Message", "Kriechkeller ist trocken");
client.publish("Kriechkeller/time", time_value);
//TFT Section
// Set the font colour to be white with a black background, set text size multiplier to 1
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_YELLOW);
tft.setTextSize(2);
//tft.setTextFont(2);
// We can now plot text on screen using the "print" class
tft.println("");
tft.println("Kriechkeller ist");
tft.println("trocken");
tft.println("");
tft.print("Zeit: ");
tft.println(time_value);
}
else {
tft.fillScreen(ST7735_BLUE);
tft.setTextColor(TFT_WHITE);
tft.setTextSize(2);
//tft.setTextFont(2);
// We can now plot text on screen using the "print" class
tft.println("WASSER im");
tft.println("Kriechkeller");
tft.println("");
tft.println("Stand: ");
tft.print(((distanceCm - distanceOffset) - WasserHoehe) * -1);
tft.println("cm");
}
delay(1000);
}
Kann sein, das noch Codereste vom erste Versuch mit einem Water Sensor drin enthalten sind, aber die stören die Funktion nicht
VG