Skip to content

Commit

Permalink
Debug influxdb (#2283)
Browse files Browse the repository at this point in the history
* Fix time offset issues in InfluxDB component. (#2278)

Closes #2273
Closes #2150

* Update interface_influxdb.cpp

* Update interface_influxdb.cpp

* Improve Logging

* Implement TimeSync at beginning

* Update time_sntp.cpp

* Update time_sntp.cpp

* Set Time After WLAN Init

---------

Co-authored-by: Antonin Delpeuch <[email protected]>
  • Loading branch information
jomjol and wetneb authored Apr 2, 2023
1 parent e2cf833 commit 63ac38a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
43 changes: 23 additions & 20 deletions code/components/jomjol_influxdb/interface_influxdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,31 @@ void InfluxDB_V2_Publish(std::string _key, std::string _content, std::string _ti

LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDB_V2_Publish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);

// Format: #define PREVALUE_TIME_FORMAT_OUTPUT "%Y-%m-%dT%H:%M:%S%z"

char nowTimestamp[21];
std::string payload;
char nowTimestamp[21];

if (_timestamp.length() > 0)
{
struct tm tm;
strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime)

struct tm * ptm;
ptm = gmtime ( &t );
time_t utc = mktime(ptm);
utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
// struct tm * ptm;
// ptm = gmtime ( &t );
// time_t utc = mktime(ptm);

LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Use handover timestamp: " + _timestamp + " converted GMT timestamp: " + std::to_string(t));

sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
// utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
// LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "time conversion utc after: " + std::to_string(utc));

sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC

payload = _influxDB_V2_Measurement + " " + _key + "=" + _content + " " + nowTimestamp;
// payload = _influxDB_V2_Measurement + " " + _key + "=774 " + nowTimestamp;
}
else
{
payload = _influxDB_V2_Measurement + " " + _key + "=" + _content;
// payload = _influxDB_V2_Measurement + " " + _key + "=774";
}

payload.shrink_to_fit();
Expand Down Expand Up @@ -157,30 +157,33 @@ void InfluxDBPublish(std::string _key, std::string _content, std::string _timest
http_config.auth_type = HTTP_AUTH_TYPE_BASIC;
}

LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);

char nowTimestamp[21];
std::string payload;
char nowTimestamp[21];

LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);

if (_timestamp.length() > 0)
{
struct tm tm;
strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime)

struct tm * ptm;
ptm = gmtime ( &t );
time_t utc = mktime(ptm);
utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
// struct tm * ptm;
// ptm = gmtime ( &t );
// time_t utc = mktime(ptm);

sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Use handover timestamp: " + _timestamp + " converted GMT timestamp: " + std::to_string(t));

// utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
// LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "time conversion utc after: " + std::to_string(utc));

sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC

payload = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp;
// payload = _influxDBMeasurement + " " + _key + "=774 " + nowTimestamp;
}
else
{
payload = _influxDBMeasurement + " " + _key + "=" + _content;
payload = _influxDB_V2_Measurement + " " + _key + "=" + _content;
}

payload.shrink_to_fit();
Expand Down
31 changes: 28 additions & 3 deletions code/components/jomjol_time_sntp/time_sntp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ void time_sync_notification_cb(struct timeval *tv)
}


bool time_manual_reset_sync(void)
{
sntp_restart();
// sntp_init();
int retry = 0;
const int retry_count = 10;
while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Waiting for system time to be set... " + std::to_string(retry) + "/" + std::to_string(retry_count));
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
if (retry >= retry_count)
return false;

LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Waiting for system time successfull with " + std::to_string(retry) + "/" + std::to_string(retry_count));
return true;
}


void setTimeZone(std::string _tzstring)
{
setenv("TZ", _tzstring.c_str(), 1);
Expand Down Expand Up @@ -220,11 +238,16 @@ bool setupTime() {
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Configuring NTP Client...");
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, timeServer.c_str());
sntp_init();

sntp_set_time_sync_notification_cb(time_sync_notification_cb);

setTimeZone(timeZone);

sntp_init();
/*
if (!wait_for_timesync())
{
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Timesync at startup failed.");
}
*/
}


Expand All @@ -250,3 +273,5 @@ bool setupTime() {

return true;
}


3 changes: 3 additions & 0 deletions code/components/jomjol_time_sntp/time_sntp.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ bool getTimeWasNotSetAtBoot(void);
bool getUseNtp(void);
bool setupTime();

bool time_manual_reset_sync(void);


#endif //TIMESNTP_H
10 changes: 10 additions & 0 deletions code/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ extern "C" void app_main(void)
ESP_LOGD(TAG, "main: sleep for: %ldms", (long) xDelay * CONFIG_FREERTOS_HZ/portTICK_PERIOD_MS);
vTaskDelay( xDelay );


// manual reset the time
// ********************************************
if (!time_manual_reset_sync())
{
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Manual Time Sync failed during startup" );
}



// Set log level for wifi component to WARN level (default: INFO; only relevant for serial console)
// ********************************************
esp_log_level_set("wifi", ESP_LOG_WARN);
Expand Down

0 comments on commit 63ac38a

Please sign in to comment.