//MQTT Broker with DoorbellButton input at IO4 and OTA //For the first time you must program via COMx: //Afterwards you can chage the program port to the network adress wich //is reported at line 38 to the serial monitor #include #include #include #include //OTA Includes and definitions ############################ #include const char* ssid = "SSID"; const char* password = "PASSWORD"; //OTA ##################################################### PicoMQTT::Server mqtt; unsigned long timeout = millis(); int greeting_number = 1; const int BellButton = 4; const int LedPin = 2; int BellState = LOW; int LastBellState = LOW; //-------------------------------------------------------------------- void setup() { // Usual setup Serial.begin(115200); delay(2000); // Serial.println("\nProgram started."); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); } Serial.printf("WiFi connected, IP: %s\n", WiFi.localIP().toString().c_str()); pinMode(BellButton, INPUT); pinMode(LedPin, OUTPUT); BellState = digitalRead(BellButton); Serial.println(); Serial.print("Bell input pin:"); Serial.print(digitalRead(BellButton)); Serial.print("\tStatus:"); Serial.println(BellState); // Subscribe to a topic pattern and attach a callback // In this example the broker subscribes to all topics. // The '#' indicates this. (#=wildcard) // If you want a special topic change the string '#' to what you need. // Example: // mqtt.subscribe("tele/time", [](const char * topic, const char * payload); // mqtt.subscribe("#", [](const char * topic, const char * payload) { // payload might be binary, but PicoMQTT guarantees that it's zero-terminated Serial.printf("Received message in topic '%s': %s\n", topic, payload); } ); //Start the mqtt broker ---------------------- mqtt.begin(); //-------------------------------------------- //OTA Setup ############################################### ArduinoOTA.setHostname("myESP32"); ArduinoOTA.begin(); Serial.println("OTA Ready"); //OTA ##################################################### } //-------------------------------------------------------------------- void loop() { // This will automatically handle client connections. By default, all clients are accepted. mqtt.loop(); if(BellState == HIGH) { digitalWrite(LedPin, HIGH); if(LastBellState==LOW) { String topic = "tele/Bell2MQTT/SENSOR"; //MQTT message. Change as needed. ------------------------------ String message = "Doorbell rings #" + String(greeting_number++); //MQTT message. End. ------------------------------------------- Serial.printf("Publishing message in topic '%s': %s\n", topic.c_str(), message.c_str()); mqtt.publish(topic, message); LastBellState = HIGH; } if(millis() - timeout > 6000) { BellState = LOW; LastBellState = LOW; digitalWrite(LedPin, LOW); } } else { timeout=millis(); BellState = digitalRead(BellButton); } //OTA Loop procedure ###################################### ArduinoOTA.handle(); //OTA ##################################################### } //-------------------------------------------------------------------- //--------------------------------------------------------------------