 
 void getSensorData()
 {
-	dht.temperature().getEvent(&sensorTemperature);
-	dht.humidity().getEvent(&sensorHumidity);
+	sensors_event_t localTemperature;
+	sensors_event_t localHumidity;
+	dht.temperature().getEvent(&localTemperature);
+	dht.humidity().getEvent(&localHumidity);
+	if (isnan(localTemperature.temperature) || isnan(localHumidity.relative_humidity))
+	{
+		DEBUG_PRINTLN("Error reading temperature or humidity!");
+		lastCollectionOk = false;
+	} else
+	{
+		DEBUG_PRINT("Temperature: ");
+		DEBUG_PRINT(localTemperature.temperature);
+		DEBUG_PRINT("°C , Humidity: ");
+		DEBUG_PRINT(localHumidity.relative_humidity);
+		DEBUG_PRINTLN("%");
+		sensorTemperature[sampleIndex] = localTemperature.temperature;
+		sensorHumidity[sampleIndex]    = localHumidity.relative_humidity;
+		sampleIndex                    = (sampleIndex + 1) % NUM_LAST_SAMPLES;
+		lastCollectionOk               = true;
+	}
 }
 
 void postData()
 {
-	postStatus = false;
+	lastPostOk = false;
 
 	if (!wifiClient.connect(API_HOSTNAME, API_PORT))
 	{

		return;
	}
	// Verify validity of server's certificate
#if !defined(API_NO_HTTPS)
	bool verified = wifiClientSecure.verifyCertChain(API_HOSTNAME);
	if (verified)
	{
		DEBUG_PRINTLN("Server certificate verified");

	drawScreen();

	synchronizeTime();
#if !(API_NO_HTTPS)
	wifiClient.setCACert_P(LECertRoot, LECertRootSize);
#endif

	lastMillis = millis();
}

extern "C" void esp_schedule();
