Arduino fail to sync time correctly

Hi,

I have set up my esp32s3 with tutorial. Here is my code:

void WIFI_Connect(const char* ID,const char* PW){
  WiFi.disconnect();
  // Setup wifi
  WiFi.mode(WIFI_STA);
  wifiMulti.addAP(ID, PW);
  Serial.print("Connecting to wifi");
  while (wifiMulti.run() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("\nDevice has connected to " + String(ID) + "(IP: " + String(WiFi.localIP()) + ")");

  // Accurate time is necessary for certificate validation and writing in batches
  // We use the NTP servers in your area as provided by: https://www.pool.ntp.org/zone/
  // Syncing progress and the time will be printed to Serial.
  timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
  configTime(gmtOffset_sec, 0, "pool.ntp.org");

  // Check server connection
  if (client.validateConnection()) {
    Serial.print("Connected to InfluxDB: ");
    Serial.println(client.getServerUrl());
  } else {
    Serial.print("InfluxDB connection failed: ");
    Serial.println(client.getLastErrorMessage());
  }
}

However, for some esp32s3 from seeed(not all of them), I always meet this problem(the wrong time stamp, 1969):

Syncing time........................................
Synchronized time: Wed Dec 31 16:10:41 1969

InfluxDB connection failed: connection refused

I cannot solve it at all. This error will make influxdb refuse to connect and there will not be any point wrote into influxdb. I wonder is it a hardware problem or software problem. If there is any wrong with my code, plz tell me.

Thank you.

Tips: If sync time only has a small bias with real time, 2 days earlier for example, it will work perfectly.

Hello @RRZ,
Usually that means that the unix timestamp wasn’t at the correct precision. I’d try printing the timestamps and go from there. Here’s the script I tried a while ago:

#if defined(ESP32)
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#define DEVICE "ESP32"
#elif defined(ESP8266)
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
#define DEVICE "ESP8266"
#endif
#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define WIFI_SSID "xxx"
#define WIFI_PASSWORD "xxx"
#define INFLUXDB_URL "xxx"
#define INFLUXDB_TOKEN "xxx"
#define INFLUXDB_ORG "xxx"
#define INFLUXDB_BUCKET "xxx"
#define TZ_INFO "GMT+0BST-1,M3.5.0/01:00:00,M10.5.0/02:00:00"
#define DHTPIN 12 // D7 on node mcu.  Other pins get set HIGH/LOW during boot and seem to stop the sensor working until replugged.
#define DHTTYPE DHT22
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
Point influx_sensor("dht22"); // Set up Influx data point for sensor
DHT_Unified dht(DHTPIN, DHTTYPE); // create dht temperature sensor
void setup() {
  Serial.begin(115200);
  while(!Serial) { };
  // Setup wifi
  WiFi.mode(WIFI_STA);
  wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("\n\nConnecting to wifi");
  while (wifiMulti.run() != WL_CONNECTED) {
    Serial.print(".");
    delay(250);
  }
  Serial.println(F("Wifi Started"));
  Serial.print(F("IP Address: "));
  Serial.println(WiFi.localIP());
  Serial.println(F("-------------------------------------"));
  timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
  //client.validateConnection(); // test to see if it fires an exception
  // https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/issues/86
  // Check server connection //  This was throwing exceptions all over the place for me
//  if (client.validateConnection()) {
//    Serial.print("Connected to InfluxDB: ");
//    Serial.println(client.getServerUrl());
//  } else {
//    Serial.print("InfluxDB connection failed: ");
//    Serial.println(client.getLastErrorMessage());
//  }
  dht.begin();
  sensor_t temp_sensor;
  dht.temperature().getSensor(&temp_sensor);
  Serial.println(F("------------------------------------"));
  Serial.println(F("Temperature Sensor"));
  Serial.print  (F("Sensor Type: ")); Serial.println(temp_sensor.name);
  Serial.print  (F("Driver Ver:  ")); Serial.println(temp_sensor.version);
  Serial.print  (F("Unique ID:   ")); Serial.println(temp_sensor.sensor_id);
  Serial.print  (F("Max Value:   ")); Serial.print(temp_sensor.max_value); Serial.println(F("°C"));
  Serial.print  (F("Min Value:   ")); Serial.print(temp_sensor.min_value); Serial.println(F("°C"));
  Serial.print  (F("Resolution:  ")); Serial.print(temp_sensor.resolution); Serial.println(F("°C"));
  Serial.println(F("------------------------------------"));
  dht.humidity().getSensor(&temp_sensor);
  Serial.println(F("Humidity Sensor"));
  Serial.print  (F("Sensor Type: ")); Serial.println(temp_sensor.name);
  Serial.print  (F("Driver Ver:  ")); Serial.println(temp_sensor.version);
  Serial.print  (F("Unique ID:   ")); Serial.println(temp_sensor.sensor_id);
  Serial.print  (F("Max Value:   ")); Serial.print(temp_sensor.max_value); Serial.println(F("%"));
  Serial.print  (F("Min Value:   ")); Serial.print(temp_sensor.min_value); Serial.println(F("%"));
  Serial.print  (F("Resolution:  ")); Serial.print(temp_sensor.resolution); Serial.println(F("%"));
  Serial.println(F("------------------------------------"));  
  influx_sensor.addTag("user", "cool_plant_lady");
}
void loop() {  
  Serial.println("Wait 10s");
  delay(10000);
  sensors_event_t event; // DHT temperature sensor
  dht.temperature().getEvent(&event);
  if(isnan(event.temperature)) {
    Serial.println(F("Error reading temperature!"));
    } else {
      Serial.print(F("Temperature: "));
      Serial.print(event.temperature);
      Serial.println(F("°C"));
      influx_sensor.addField("temperature", event.temperature); // I dont like using "temp" for temperature
    }
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  }
  else {
    Serial.print(F("Humidity: "));
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));
    influx_sensor.addField("humidity", event.temperature);
  }
  // Print what are we exactly writing
  Serial.print("Writing: ");
  Serial.println(influx_sensor.toLineProtocol());
  // If no Wifi signal, try to reconnect it
  if ((WiFi.RSSI() == 0) && (wifiMulti.run() != WL_CONNECTED)) {
    Serial.println("Wifi connection lost");
  }
  // Write point
  Serial.println("XX");
  if (!client.writePoint(influx_sensor)) {
    Serial.print("InfluxDB write failed: ");
    Serial.println(client.getLastErrorMessage());
  }
influx_sensor.clearFields();
}

Hi @Anaisdg,

Thanks a lot for your code. I try your code but the timestamp is still wrong. I guess this could be a hardware issue because some of my chips works but some not.

Wifi Started
IP Address: xxx
-------------------------------------
Syncing time........................................
Synchronized time: Thu Jan  1 00:04:00 1970

I mention that once the timestamp goes wrong, it will take a longer time to sync time than correct timestamp.

1 Like

@RRZ,
Do you mind telling me more about the project that you’re working on? I love to learn what community members are using InfluxDB for.

Hi @Anaisdg,

I am working on an air monitoring system for practice. These system will record temperature, air pressure, humidity, VOC with bosch sensor.

@RRZ do you have any interest in writing a little blog post about your project? I can help you ghost write it. We love to showcase what community is doing with InfluxDB.

@Anaisdg sure. I mean I am senior student next fall semester, I’d love to share, but may be busy with my own stuff. I guess I may take a long time to finish a blog even it is a little one. :slightly_smiling_face: Thank u for inviting me.

1 Like