Hallo Ihr Lieben,
ich habe das Problem mit meinem Programm, das nach ein paar Stunden Laufzeit immer komische Symbolketten auf dem Display angezeigt werden. Nach einem Neustart läuft alles wieder ganz normal.
Hiermal der Code.
#include "EspMQTTClient.h"
#include <Adafruit_ADS1X15.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
Adafruit_ADS1115 ads;
int Taster = 2;//minus Min
int Taster2 = 15;//plus Min
int Taster3 = 16;//minus Max
int Taster4 = 5;//plus MaX
int sTaster = 0;
int sTaster2 = 0;
int sTaster3 = 0;
int sTaster4 = 0;
int Relay = 25;
int Min = 40;
int Max = 60;
unsigned long previousMillis = 0;
const long interval = 10000; //60000 * 10;
EspMQTTClient client(
"*",
"*",
"*", // 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);
pinMode(Relay, OUTPUT);
pinMode(Taster, INPUT_PULLDOWN);
pinMode(Taster2, INPUT_PULLUP); // Taster PIN = Eingang
pinMode(Taster3, INPUT);
pinMode(Taster4, INPUT_PULLUP);
ads.setGain(GAIN_ONE);
if (!ads.begin()) {
Serial.println("Failed to initialize ADS.");
//while (1);
}
lcd.init();
lcd.backlight();
delay(10);
// 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");
});
}
void loop()
{
sTaster = digitalRead(Taster);
if (sTaster == HIGH)
{
Min = Min - 5;
}
sTaster2 = digitalRead(Taster2);
if (sTaster2 == LOW)
{
Min = Min + 5;
}
sTaster3 = digitalRead(Taster3);
if (sTaster3 == HIGH)
{
Max = Max - 5;
}
sTaster4 = digitalRead(Taster4);
if (sTaster4 == LOW)
{
Max = Max + 5;
}
int mittelwert = mittelwertBerechnen();
int mittel = map(mittelwert, 13950, 54290, 100, 0);
lcd.setCursor(12, 1);
lcd.print(mittel); //hier mal Var Mittel Probieren
lcd.setCursor(14, 1);
lcd.print("%");
lcd.setCursor(0, 0);
lcd.print(Min);
lcd.setCursor(2, 0);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print(Max);
lcd.setCursor(2, 1);
lcd.print("%");
// Messen
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
String stringone = String(mittel);
client.publish("Gieskanne/mittelwert", stringone);
if (mittel <= Min)
{
Serial.println("Giesen");
digitalWrite(Relay, HIGH);
delay(10);
lcd.setCursor(12, 0);
lcd.print("*");
}
if (mittel >= Max)
{
Serial.println("Fertig");
digitalWrite(Relay, LOW);
delay(10);
lcd.setCursor(12, 0);
lcd.print(" ");
}
}
int sRelay;
sRelay = digitalRead(Relay);
Serial.println(sRelay);
}
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 + ads.readADC_SingleEnded(0);
Sensor2 = Sensor2 + ads.readADC_SingleEnded(1);
Sensor3 = Sensor3 + ads.readADC_SingleEnded(2);
Sensor4 = Sensor4 + ads.readADC_SingleEnded(3);
delay(10);
}
Sensor1 = Sensor1 / 10;
Sensor2 = Sensor2 / 10;
Sensor3 = Sensor3 / 10;
Sensor4 = Sensor4 / 10;
ergebnis = Sensor1 + Sensor2 + Sensor3 + Sensor4 / 4;
delay(100);
Serial.println(Sensor1);
Serial.println(Sensor2);
Serial.println(Sensor3);
Serial.println(Sensor4);
Serial.println("Ergebnis");
Serial.println(ergebnis);
Serial.println("");
return ergebnis;
}
Kann jemand einen Fehler entdecken?
Gruß
Florian