/* ESP32 MQTT-Beispiel mit 1 Teilnehmer und Node-Red
 *  Benötigt die Bibliothek MQTT (by Joel Gaeswiller)
 *  Der ESP verbindet sich mit einem Broker, der Port kann hier nicht angegeben werden
 *  
 *  Per MQTT werden verschiedenen Nachrichten zum roker geschickt. Node-Red verbindet mehrere ESPs
 *  Verbesserte Version: Gerätename und MQTT-Topic werden als String zusammen-
 *  gesetzt, der Client-Name enthält die MAC-Adresse als "zufälligen" Wert.
 *  
 *  Autor: Spi 
 *  Version: 1.0
 *  Datum: 2024-02-29
 ***************************************************************************/  
#include <WiFi.h>
#include <MQTT.h>

// Update these with values suitable for your network.

const char* ssid = "Wokwi-GUEST";
const char* password = "";
const byte  channel = 6;    // wifi channel only for Wokwi
//#include <credentials.h>

//***** MQTT-Topics hier zentral anpassen
String HOST_NAME = "esp1";
String MQTT_TOPIC = "wvss/esp32/" + HOST_NAME;

//const char* mqtt_server = "192.168.1.1";
const char* mqtt_server = "broker.hivemq.com";

const int LED_rot=32, LED_gruen=33, Taster2=2, Taster4=4;

WiFiClient net;
MQTTClient client;

unsigned long lastMillis = 0, lastToggle = 0;
bool Zustand, Taster2_neu, Taster2_alt, Taster4_neu, Taster4_alt;
char msg[50];

void connect() {
  Serial.print("Suche WLAN...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }

  Serial.print("\nverbinde als Client ");
  // generate client-name from ESP32 MAC address
  String Client_Name = HOST_NAME + "::" + WiFi.macAddress();
  Serial.println(Client_Name);
  while (!client.connect(Client_Name.c_str(), "try", "try")) {
    Serial.print(".");
    delay(1000);
  }

  Serial.println("\nverbunden!");
  // subscribe topics
  client.subscribe(MQTT_TOPIC+"/#");  
  // client.unsubscribe("hello/");
}

void messageReceived(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);

  // Switch the LED correct topic was received  
  // and check the first character 
  if (topic == MQTT_TOPIC + "/in") {
    Serial.println("MQTT-Befehl erhalten");
    if (payload == "ON") {
      digitalWrite(LED_rot, LOW);   // turn the LED on (low active)
    } else if (payload == "OFF"){
      digitalWrite(LED_rot, HIGH);  // turn the LED off 
    }
  }
  
}

void setup() {
  Serial.begin(115200);
  //WiFi.begin(ssid, password);   // automatic selsctrion of wifi-channel => use with local AP
  WiFi.begin(ssid, password, channel);
  pinMode(LED_rot, OUTPUT);
  pinMode(LED_gruen, OUTPUT);
  pinMode(Taster2, INPUT_PULLUP);
  pinMode(Taster4, INPUT_PULLUP);
  
  Zustand = true;

  // start MQTT connection to server
  client.begin(mqtt_server, net);
  // callback-function for received messages
  client.onMessage(messageReceived);

  connect();

  delay(1000);
  // send test message 
  client.publish("wvss/esp32/chat", "hello world from " + MQTT_TOPIC);
}

void loop() {
  client.loop();  // handle incoming MQTT messages
  
  if (!client.connected()) { // lost MQTT-connection? Reconnect 
    connect();
  }

  // read a button and publish a message on change
  if (millis() - lastMillis > 100) {   // for debouncing
    lastMillis = millis();
    Taster2_neu = digitalRead(Taster2);
    if ( !Taster2_neu && Taster2_alt) {
      client.publish(MQTT_TOPIC + "/out", "ON");  
    }
    else if ( Taster2_neu && !Taster2_alt) {
      client.publish(MQTT_TOPIC + "/out", "OFF");  
    }
    Taster2_alt = Taster2_neu;
  }

}
Taster2
Taster4
LED32
LED33
ESP32 Schulboard mit LEDs und Tastern, I2C-Bus mit Standard-Belegung
SSD1306 Display
WS2812 an Pin26