for one week I am searching for a solution at my ESP32 project (development over Arduino IDE): In my project I execute the following step:
- Read Temperature via BME280
- Ask time at NTP server (via time.h)
- Write data so string
- Send String via mqtt to mqtt broker (via PubSubClient)
All works fine but only if I not add the timestamp like this (without a timestamp):
String jsonString="{\"temperature\":"+String(bme.readTemperature())+",\"humidity\":"+String(bme.readHumidity())+",\"pressure\":"+String(bme.readAltitude(SEALEVELPRESSURE_HPA))+",\"deviceID\":\""+deviceID+"\",\"@timestamp\":\"\"}";
But if I add the timestamp the PubSubClient don't send the data.:
String jsonString="{\"temperature\":"+String(bme.readTemperature())+",\"humidity\":"+String(bme.readHumidity())+",\"pressure\":"+String(bme.readAltitude(SEALEVELPRESSURE_HPA))+",\"deviceID\":\""+deviceID+"\",\"@timestamp\":\""+printFormattedTime()+"\"}";
The function printFormattedTime():
String printFormattedTime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return String("Failed to obtain time");
}
char timeStringBuff[50]; //50 chars should be enough
strftime(timeStringBuff, sizeof(timeStringBuff), "%Y-%B-%d %H:%M:%S", &timeinfo);
//print like "const char*"
Serial.println(timeStringBuff);
return String(timeStringBuff);
}
As I said: all works fine until I add the function that returns the String with time format.
I also tested another NTPClient (NTPClient.h) but without success.
Any idea?
Thanks