#include "EspMQTTClient.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//#include "OneButton.h"
int Taster = 14;
int Taster2 = 16;
int Relay = 27;
int Pause = 1000;
int ASensor1 = 34;
int ASensor2 = 35;
float Liter = 0;
boolean aLedEnabled = false;
boolean aButtonPressed = false;
//OneButton button(buttonPin, false);
unsigned long previousMillis = 0;
const long interval = 10000;
EspMQTTClient client(
"*****",
"*******",
"192.*********", // MQTT Broker server ip
"MQTTUsername", // Can be omitted if not needed
"MQTTPassword", // Can be omitted if not needed
"TestClient", // Client name that uniquely identify your device
1883 // The MQTT port, default to 1883. this line can be omitted
);
void setup()
{
Serial.begin(115200);
// Optionnal functionnalities of EspMQTTClient :
client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password").
client.enableLastWillMessage("TestClient/lastwill", "I am going offline"); // You can activate the retain flag by setting the third parameter to true
}
// This function is called once everything is connected (Wifi and MQTT)
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
void onConnectionEstablished()
{
// Subscribe to "mytopic/test" and display received message to Serial
client.subscribe("mytopic/test", [](const String & payload) {
Serial.println(payload);
});
// Subscribe to "mytopic/wildcardtest/#" and display received message to Serial
client.subscribe("mytopic/wildcardtest/#", [](const String & topic, const String & payload) {
Serial.println("(From wildcard) topic: " + topic + ", payload: " + payload);
});
// Publish a message to "mytopic/test"
client.publish("mytopic/test", "This is a message"); // You can activate the retain flag by setting the third parameter to true
// Execute delayed instructions
client.executeDelayed(5 * 1000, []() {
client.publish("mytopic/wildcardtest/test123", "This is a message sent 5 seconds later");
});
pinMode(27, OUTPUT);
pinMode(Taster, INPUT_PULLUP );
pinMode(Taster2, INPUT_PULLUP); // Taster PIN = Eingang
pinMode(ASensor1, INPUT);
pinMode(ASensor2, INPUT);
lcd.init(); // initialize the lcd
lcd.backlight();
delay(100);
}
void loop()
{
client.loop();
if (digitalRead(Taster) == HIGH)
{
clickedIt();
}
delay(100);
if (digitalRead(Taster2) == HIGH)
{
doubleClickedIt();
}
delay(50);
if (Liter <= 0)Liter = 0;
int mittelwert = mittelwertBerechnen();
// Display wird komplett geleert
int mittel = map(mittelwert, 774, 2500, 100, 0);
lcd.setCursor(0, 0); // Position ab wo wir etwas überschreiben möchten
lcd.print(Liter);
lcd.setCursor(6, 0);
lcd.print("liter");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(map(mittelwert, 774, 2500, 100, 0));
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
String stringone = String(mittel);
client.publish("Gieskanne/mittelwert", stringone);
// function2
}
if (mittel <= 44)
{
//
Giesen();
}
}
void clickedIt() {
Liter = Liter + 0.25;
}
void doubleClickedIt() {
Liter = Liter - 0.25;
}
int mittelwertBerechnen()
{
int Sensor1 = 0;
int Sensor2 = 0;
int Sensor3 = 0;
int Sensor4 = 0;
int ergebnis = 0;
for (int i = 0; i < 10; i++)
{
Sensor1 = Sensor1 + analogRead(ASensor1);
Sensor2 = Sensor2 + analogRead(ASensor2);
//Sensor3 = Sensor3 + analogRead(A2);
//Sensor4 = Sensor4 + analogRead(A3);
delay(10);
}
Sensor1 = Sensor1 / 10;
Sensor2 = Sensor2 / 10;
ergebnis = Sensor1 + Sensor2 / 2;
delay(100);
Serial.println(ergebnis);
Serial.println("Sensor1:");
Serial.println(Sensor1);
delay(1000);
Serial.println(",");
Serial.println("Sensor2:");
Serial.println(Sensor2);
Serial.println(",");
delay(1000);
return ergebnis;
}
void Giesen()
{
Serial.println("Giesen");
digitalWrite(27, HIGH);
delay(100);
client.publish("Gieskanne/Fertig", "Giesse");
long Dauer = 1800000 * Liter;
Serial.println(Dauer);
delay(Dauer);
Serial.println("Fertig");
digitalWrite(27, LOW);
client.publish("Gieskanne/Fertig", "Fertig");
delay(100);
}```