 void loop() {
 	check();
 	if (!mqtt->isConnected()) {
-		// Not connected
+		// Connecting
 		// Clean buffers and give some time for network
 		mqtt->yield();
 		// Connect

#define MQTT_MAX_PAYLOAD_SIZE						96
#define MQTT_MAX_MESSAGE_HANDLERS					1
#define MQTT_COMMAND_TIMEOUT_MS						(8*1000L)
#define MQTT_KEEP_ALIVE_INTERVAL_SEC				(publishPeriodMs/1000L*3)
#define MQTT_CLIENT_ID								SENSOR_ID
#define MQTT_SUBSCRIBE_QOS							MqttClient::QOS0
#define MQTT_SUBSCRIBE_TOPIC_CFG					"test/sensor/" SENSOR_ID "/config"

			networkWakeUp(NETWORK_WAKE_UP_DELAY_MS);
		} else {
			connectCounter = 0;
			// Update configuration immediately after reconnect
			firstUpdateConfig = true;
			// Do the publish immediately after reconnect
			firstPublish = true;
		}
	} else if (firstUpdateConfig || isTimePassed(updateConfigTs, MQTT_UPDATE_CONFIG_PERIOD_MS)) {
		// Time to Update configuration
		// Clean buffers and give some time for network
		mqtt->yield();
		if (updateConfig()) {
			updateConfigTs = time.millis();
			firstUpdateConfig = false;
		}
	} else if (firstPublish || isTimePassed(publishTs, publishPeriodMs)) {
		// Time to Publish
		const int bufferSize = MQTT_MAX_PAYLOAD_SIZE;
		char buffer[bufferSize];
		memset(buffer, 0, bufferSize);
		// Build message payload
		buildMessagePayload(buffer, bufferSize);
		// Build message
		MqttClient::Message message;
		message.qos = MQTT_PUBLISH_QOS;
		message.retained = false;
		message.dup = false;
		message.payload = (void*) buffer;
		message.payloadLen = strlen(buffer) + 1;
		// Publish
		MqttClient::Error::type rc = mqtt->publish(MQTT_PUBLISH_TOPIC, message);
		if (rc != MqttClient::Error::SUCCESS) {
			LOG_PRINTFLN("ERROR, Publish, rc:%i", rc);
		} else {
			publishTs = time.millis();
			firstPublish = false;
		}
	} else {
		// Idle until next event
		unsigned long nextEventDelay = min(
			timeLeft(publishTs, publishPeriodMs),
			timeLeft(updateConfigTs, MQTT_UPDATE_CONFIG_PERIOD_MS)
		);
		LOG_PRINTFLN("Idle for %lu ms", min(nextEventDelay, mqtt->getIdleInterval()));
		// Sleep
		networkHibernate(NETWORK_HIBERNATE_DELAY_MS);
		Lpm::idle(min(nextEventDelay, mqtt->getIdleInterval()));
		networkWakeUp(NETWORK_WAKE_UP_DELAY_MS);
