Skip to content

Unexpected system time behaviour #2526

Closed
@AlexTZA

Description

@AlexTZA

Hardware:

Board: Custom ESP32 board and ESP32 WROVER Kit
Core Installation version: 1.0.1
IDE name: Arduino IDE or Visual Studio/vMicro
Flash Frequency: 80Mhz
PSRAM enabled: yes
Upload Speed: 921600
Computer OS: Windows 10

Description:

I have run into some strange behaviour when attempting to get/set the ESP system time using the settimeofday/gettimeofday methods. The main issue is that if the timeofday is set and immediately read back, sometimes the set/read values are not the same or within an expected deviation of one another. This however, only occurs under certain circumstances which I have managed to replicated in the example below. However, uncommenting the marked code "fixes" the issue, at least on two PC's and three different pieces of hardware based on the ESP32 WROVER. Can anyone else reproduce this issue?

Sketch: (leave the backquotes for code formatting)

/*
  ESP8266 mDNS-SD responder and query sample

  This is an example of announcing and finding services.
  
  Instructions:
  - Update WiFi SSID and password as necessary.
  - Flash the sketch to two ESP8266 boards
  - The last one powered on should now find the other.
 */

#include <WiFi.h>
#include <ESPmDNS.h>
#include "time.h"

const char* ssid     = "ssid";
const char* password = "pwd";


void PrintTime()
{
  timeval tVal;
  tVal.tv_sec = 1000;

  settimeofday(&tVal, NULL);

  timeval b;
  gettimeofday(&b, NULL);

  Serial.printf("System time set = %d, System time read = %d\r\n", tVal.tv_sec, b.tv_sec);

  if (tVal.tv_sec != b.tv_sec)
  {
    Serial.printf("***** Unexpected time difference! ******\r\n");
  }  
}

void setup() 
{
  Serial.begin(115200);
  
  PrintTime();

  Serial.print("Connecting to Wifi");
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) 
  {
        delay(250);
        Serial.print(".");
    }

    //***Uncommenting the following lines "fixes" the PrintTime method???*****
    //Serial.println("");
    //Serial.print("Connected to ");
    //Serial.println(ssid);
    //Serial.print("IP address: ");
    //Serial.print("IP address: ");
    //Serial.println(WiFi.localIP());

    //if (!MDNS.begin("ESP32_Browser")) {
    //    Serial.println("Error setting up MDNS responder!");
    //    while(1){
    //       delay(1000);
    //  }
    //}
    //*********
}

void loop() 
{
  //Serial.printf("RAM >> Total Heap[%d], Free Heap[%d], Free PSRAM[%d]\r\n", ESP.getHeapSize(), ESP.getFreeHeap(), ESP.getFreePsram());

  delay(1000);
  
  PrintTime(); 

  //browseService("http", "tcp");
  //delay(1000);
  //browseService("arduino", "tcp");
  //delay(1000);
  //browseService("workstation", "tcp");
  //delay(1000);
  //browseService("smb", "tcp");
  //delay(1000);
  //browseService("afpovertcp", "tcp");
  //delay(1000);
  //browseService("ftp", "tcp");
  //delay(1000);
  //browseService("ipp", "tcp");
  //delay(1000);
  //browseService("printer", "tcp");
  //delay(10000);
}

void browseService(const char * service, const char * proto){
    Serial.printf("Browsing for service _%s._%s.local. ... ", service, proto);
  //the queryService call seems to leak RAM
    int n = MDNS.queryService(service, proto);
    if (n == 0) {
        Serial.println("no services found");
    } else {
        Serial.print(n);
        Serial.println(" service(s) found");
        for (int i = 0; i < n; ++i) {
            // Print details for each service found
            Serial.print("  ");
            Serial.print(i + 1);
            Serial.print(": ");
            Serial.print(MDNS.hostname(i));
            Serial.print(" (");
            Serial.print(MDNS.IP(i));
            Serial.print(":");
            Serial.print(MDNS.port(i));
            Serial.println(")");
        }
    }
    Serial.println();
}

Debug Messages:

ets Jun  8 2016 00:22:57
08:42:32.686 -> 
08:42:32.686 -> rst:0x10 (RTCWDT_RTC_RESET),boot:0x3e (SPI_FAST_FLASH_BOOT)
08:42:32.686 -> configsip: 0, SPIWP:0xee
08:42:32.686 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
08:42:32.686 -> mode:DIO, clock div:1
08:42:32.686 -> load:0x3fff0018,len:4
08:42:32.686 -> load:0x3fff001c,len:1100
08:42:32.686 -> load:0x40078000,len:10088
08:42:32.686 -> load:0x40080400,len:6380
08:42:32.686 -> entry 0x400806a4
[D][esp32-hal-psram.c:47] psramInit(): PSRAM enabled
System time set = 1000, System time read = 2074
08:42:34.387 -> ***** Unexpected time difference! ******
08:42:34.387 -> Connecting to Wifi[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 0 - WIFI_READY
08:42:34.489 -> [D][WiFiGeneric.cpp:342] _eventCallback(): Event: 2 - STA_START
...[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 4 - STA_CONNECTED
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 7 - STA_GOT_IP
08:42:35.476 -> [D][WiFiGeneric.cpp:385] _eventCallback(): STA IP: 192.168.0.87, MASK: 255.255.255.0, GW: 192.168.0.1
.System time set = 1000, System time read = 2073
08:42:36.494 -> ***** Unexpected time difference! ******
System time set = 1000, System time read = 2073
08:42:37.488 -> ***** Unexpected time difference! ******
System time set = 1000, System time read = 2073
08:42:38.504 -> ***** Unexpected time difference! ******
System time set = 1000, System time read = 2073
08:42:39.487 -> ***** Unexpected time difference! ******
System time set = 1000, System time read = 2073
08:42:40.514 -> ***** Unexpected time difference! ******
System time set = 1000, System time read = 2073
08:42:41.497 -> ***** Unexpected time difference! ******
System time set = 1000, System time read = 2073

...etc

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions