vielen Dank dir, ich habe mir das "Blink without delay'' Beispiel angeguckt und auch gleich mal als Ersatz für die Verzögerung beim Lesen der Taster eingebaut.
Es scheint gefühlt schon besser zu laufen, aber trotzdem bricht irgendwie regelmäßig die Verbindung zu mqtt ab und dann hängt das Skript in der reconnect Funktion und reagiert kurzzeitig nicht auf die Tastendrücke.
Auf jeden Fall gut zu wissen dass es an dem Code liegt und nicht an irgendwelchen schlechten Lötstellen, hab wirklich keine Lust dass nochmal auseinander zu bauen 
Hier nochmal der angepasste Code ( musste viel loeschen da ich hier so eine Fehlermeldung bekomme da wohl zu lang, ganze Datei im anhang)
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <LiquidCrystal_I2C.h>
#include <MCP3008.h>
#define CS_PIN 15
#define CLOCK_PIN 14
#define MOSI_PIN 13
#define MISO_PIN 12
MCP3008 adc(CLOCK_PIN, MOSI_PIN, MISO_PIN, CS_PIN);
int tempValSwitch = 0;
boolean backlightON = true;
boolean modeONE;
unsigned long previousMillis = 0; // will store last time buttons read
const long interval = 500; // interval at which to read buttons
const char *SSID = "xxx"
const char *PSK = "xxx"
const char *MQTT_BROKER = "192.168.0.xxx";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, 20, 4);
// ...
//void printLcd(int line, int tab, String msg, boolean clear = true)...
//void printButtons(int val)..
void setup_wifi()
{
delay(10);
//Serial.println();
//Serial.print("Connecting to ");
//Serial.println(SSID);
WiFi.begin(SSID, PSK);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.print("wifi verbunden.");
//Serial.println("");
//Serial.println("WiFi connected");
//Serial.println("IP address: ");
//Serial.println(WiFi.localIP());
}
void callback(char *topic, byte *payload, unsigned int length)
{
//Serial.print("Received message [");
//Serial.print(topic);
//Serial.print("] ");
char msg[length];
String nachricht = "";
for (int i = 0; i < length; i++)
{
msg[i] = (char)payload[i];
nachricht += msg[i];
}
// control backlight via mqtt:
if (nachricht.charAt(0) == '#' && nachricht.charAt(1) == '&')
{
Serial.print("Befehl empfangen:");
Serial.println(nachricht);
if (nachricht.charAt(2) == 'b')
{
if (nachricht.charAt(3) == '0')
{
lcd.noBacklight();
backlightON = false;
//Serial.println("Backlight switch off (via mqtt)");
}
else
{
lcd.backlight();
backlightON = true;
//Serial.println("Backlight switched on (via mqtt)");
}
}
}
// print on LCD via mqtt:
else if (nachricht.length() > 3)
{ // if no command send (message string starts not with #&) and at least 4 chars
// (last 3 are in use for tab, line, clear(true/false))
Serial.print("nachricht empfangen:");
Serial.println(nachricht);
// get line and tab for setting the cursor (always last two characters of the String)
int line = ((int)nachricht.charAt(nachricht.length() - 2)) - ((int)'0');
int tab = ((int)nachricht.charAt(nachricht.length() - 1)) - ((int)'0');
bool clearLine = (nachricht.charAt(nachricht.length() - 3) == '1');
// $ write custom char if message string starts with $
if (nachricht.charAt(0) == '
Hast Du oder jmd anders vielleicht noch einen Vorschlag wie man das zusammenspiel von mqtt und den Tastern besser gestalten kann?
Und irgendwie braucht der ESP teilweise Minuten um am Start die Wlan Verbindung herzustellen, ist dieses setup_wifi wie ich es da benutze so in Ordnung?
main_ohne_delay.cpp (9.03 KB))
{
lcd.setCursor(tab, line);
//Serial.println(((int)nachricht.charAt(1)) - ((int)'0'));
lcd.write(byte(((int)nachricht.charAt(1)) - ((int)'0'))); // $000 write custom char 0 at line 0 tab 0..
}
else
{
// remove last 3 characters of message (clear,line,tabs)
nachricht = nachricht.substring(0, nachricht.length() - 3);
printLcd(line, tab, nachricht, clearLine);
}
}
}
void reconnect()
{
while (!client.connected())
{
Serial.println("Reconnecting MQTT...");
if (!client.connect("ESP8266Client"))
{
//Serial.print("failed, rc=");
//Serial.print(client.state());
//Serial.println(" retrying in 1 seconds");
delay(1000);
}
}
client.subscribe("/home/LCD/inputESP");
Serial.println("MQTT Connected...");
client.publish("/home/LCD/outputToRaspy", "READY!");
}
//boolean switchOpen(int val)...
//boolean buttonPressed(int indexButton)..
void setup()
{
Serial.begin(115200);
setup_wifi();
client.setServer(MQTT_BROKER, 1883);
client.setCallback(callback);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("booting");
printLcd(1, 0, "setup wifi..");
printLcd(2, 0, "connected!");
lcd.clear();
lcd.createChar(0, customCharPLAY);
lcd.createChar(1, customCharSPEAKER);
lcd.createChar(2, customCharSTOP);
lcd.createChar(3, customCharPAUSE);
lcd.createChar(4, customCharHaken);
modeONE = switchOpen(adc.readADC(0));
lcd.print("ready!");
if (modeONE)
printButtons(1);
else
printButtons(2);
}
void loop()
{
if (!client.connected())
{
reconnect();
}
//client.loop();
// delay for reading buttons
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
// save the last time taster gelesen
previousMillis = currentMillis;
tempValSwitch = adc.readADC(0);
if (switchOpen(tempValSwitch) && !modeONE)
{
printButtons(1);
modeONE = true;
}
else if (!switchOpen(tempValSwitch) && modeONE)
{
printButtons(2);
modeONE = false;
}
if (buttonPressed(2))
{
if (switchOpen(tempValSwitch))
{
client.publish("/home/LCD/outputToRaspy", "4a");
Serial.println("button4a pressed");
printAction(" vol down ");
}
else
{
client.publish("/home/LCD/outputToRaspy", "4b");
printAction(" vol down ");
}
}
else if (buttonPressed(3))
{
if (switchOpen(tempValSwitch))
{
Serial.println("button3a pressed");
client.publish("/home/LCD/outputToRaspy", "3a");
printAction(" vol up ");
}
else
{
Serial.println("button3b pressed");
client.publish("/home/LCD/outputToRaspy", "3b");
printAction(" vol up ");
}
}
else if (buttonPressed(4))
{
if (switchOpen(tempValSwitch))
{
client.publish("/home/LCD/outputToRaspy", "2a");
Serial.println("button2 pressed");
printAction(" skipping ");
}
else
{
client.publish("/home/LCD/outputToRaspy", "2b");
printAction("skip channel");
}
}
else if (buttonPressed(5))
{
if (switchOpen(tempValSwitch))
{
client.publish("/home/LCD/outputToRaspy", "1a");
Serial.println("button1a pressed");
printAction(" play/pause ");
}
else
{
client.publish("/home/LCD/outputToRaspy", "1b");
printAction(" radio pl/ps");
}
}
}
}
Hast Du oder jmd anders vielleicht noch einen Vorschlag wie man das zusammenspiel von mqtt und den Tastern besser gestalten kann?
Und irgendwie braucht der ESP teilweise Minuten um am Start die Wlan Verbindung herzustellen, ist dieses setup_wifi wie ich es da benutze so in Ordnung?
[main_ohne_delay.cpp|attachment](upload://lFMQI04kdq52rRZEo92WezpJefg.cpp) (9.03 KB)