PubSubClient mqtt_client(wifi); long mqtt_last_reconnect_attempt = 0; void mqtt_callback(char* topic, byte* payload, unsigned int length) { payload[length] = '\0'; for (int i=0; i 5000) { mqtt_last_reconnect_attempt = now; if (mqtt_connect()) { mqtt_last_reconnect_attempt = 0; } } } else { mqtt_client.loop(); } }