src/tasks/mqtt-home-assistant.h
/**
     * Established a connection to Home Assistant MQTT broker.
     * 
     * This task should run continously. It will check if an
     * MQTT connection is active and if so, will sleep for 1
     * minute. If not, a new connection will be established.
     */
Established a connection to Home Assistant MQTT broker.

This task should run continously. It will check if an
MQTT connection is active and if so, will sleep for 1
minute. If not, a new connection will be established.
Established a connection to Home Assistant MQTT broker . This task should run continously . It will check if an MQTT connection is active and if so will sleep for 1 minute . If not a new connection will be established .
void keepHAConnectionAlive(void * parameter){
        for(;;){
            if(HA_mqtt.connected()){
                HA_mqtt.loop();
                vTaskDelay(250 / portTICK_PERIOD_MS);
                continue;
            }
            if(!WiFi.isConnected()){
                vTaskDelay(1000 / portTICK_PERIOD_MS);
                continue;
            }
            serial_println(F("[MQTT] Connecting to HA..."));
            HA_mqtt.begin(HA_ADDRESS, HA_PORT, HA_net);
            long startAttemptTime = millis();
            while (!HA_mqtt.connect(DEVICE_NAME, HA_USER, HA_PASSWORD) &&
                    millis() - startAttemptTime < MQTT_CONNECT_TIMEOUT)
            {
                vTaskDelay(MQTT_CONNECT_DELAY / portTICK_PERIOD_MS);
            }
            if(!HA_mqtt.connected()){
                serial_println(F("[MQTT] HA connection failed. Waiting 30s.."));
                vTaskDelay(30000 / portTICK_PERIOD_MS);
            }
            serial_println(F("[MQTT] HA Connected!"));
        }
    }
void keepHAConnectionAlive ( void * parameter ) { for ( ; ; ) { if ( HA_mqtt . connected ( ) ) { HA_mqtt . loop ( ) ; vTaskDelay ( 250 / portTICK_PERIOD_MS ) ; continue ; } if ( ! WiFi . isConnected ( ) ) { vTaskDelay ( 1000 / portTICK_PERIOD_MS ) ; continue ; } serial_println ( F ( " " ) ) ; HA_mqtt . begin ( HA_ADDRESS , HA_PORT , HA_net ) ; long startAttemptTime = millis ( ) ; while ( ! HA_mqtt . connect ( DEVICE_NAME , HA_USER , HA_PASSWORD ) && millis ( ) - startAttemptTime < MQTT_CONNECT_TIMEOUT ) { vTaskDelay ( MQTT_CONNECT_DELAY / portTICK_PERIOD_MS ) ; } if ( ! HA_mqtt . connected ( ) ) { serial_println ( F ( " " ) ) ; vTaskDelay ( 30000 / portTICK_PERIOD_MS ) ; } serial_println ( F ( " " ) ) ; } }
Established a connection to Home Assistant MQTT broker.
Established a connection to Home Assistant MQTT broker .
// When we are connected, loop the MQTT client and sleep for 0,5s