BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Blynk edgent time sync

[31656] Connecting to WiFi: TEST RIG
[54013] Using Dynamic IP: 192.168.43.69
[54013] CONNECTING_NET => CONNECTING_CLOUD
[54426] Current time: Wed Sep  1 12:44:09 2021
[54426] Connecting to blynk.cloud:443
[55571] Ready (ping: 12ms).
[55780] CONNECTING_CLOUD => RUNNING

[54426] Current time: Wed Sep 1 12:44:09 2021 Day, Month, Week and Year are correct. But the time is not right. How to I set the time zone ? Or should I define NTP server ? How is the code determining(on what basis) the time that is printed on the serial monitor ?

I went through the header files but couldn’t make out what is printing the date and time.

Blynk.sendInternal("rtc", "sync"); this did not help.

BLYNK_CONNECTED() {
    // Send requests for different internal data
    // Request what is actually needed for your use-case
    Blynk.sendInternal("utc", "tz_name");   // Name of timezone
    Blynk.sendInternal("utc", "iso");       // ISO-8601 formatted time
    Blynk.sendInternal("utc", "time");      // Unix timestamp (with msecs)
    Blynk.sendInternal("utc", "tz");        // Timezone and DST offsets
    Blynk.sendInternal("utc", "tz_rule");   // POSIX TZ rule
    Blynk.sendInternal("utc", "dst_next");  // Up to 2 next time offset changes (due to DST)
}

// Receive UTC data
BLYNK_WRITE(InternalPinUTC) {
    String cmd = param[0].asStr();
    if (cmd == "time") {
        uint64_t utc_time = param[1].asLongLong();
        Serial.print("Unix time (UTC): "); Serial.println(utc_time);
        
    } else if (cmd == "iso") {
        String iso_time = param[1].asStr();
        Serial.print("ISO-8601 time:   "); Serial.println(iso_time);
        
    } else if (cmd == "tz") {
        long tz_offset = param[1].asInt();
        long dst_offset = param[2].asInt();
        Serial.print("TZ offset:       "); Serial.print(tz_offset);  Serial.println(" minutes");
        Serial.print("DST offset:      "); Serial.print(dst_offset); Serial.println(" minutes");
    
    } else if (cmd == "tz_name") {
        String tz_name = param[1].asStr();
        Serial.print("Timezone:        "); Serial.println(tz_name);
    
    } else if (cmd == "tz_rule") {
        String tz_rule = param[1].asStr(); 
        Serial.print("POSIX TZ rule:   "); Serial.println(tz_rule);
   
    } else if (cmd == "dst_next") {
        uint32_t next1_ts  = param[1].asLong();
        int next1_off       = param[2].asInt();

        uint32_t next2_ts  = param[3].asLong();
        int next2_off       = param[4].asInt();
        
        Serial.print("Next offset changes: ");
        Serial.print(next1_off); Serial.print("min. on "); Serial.print(next1_ts);
        Serial.print(", ");
        Serial.print(next2_off); Serial.print("min. on "); Serial.print(next2_ts);
        Serial.println();
    }
}

I think you’ll find that the time returned is UTC (GMT or Zulu time).

You can use the Timezone library to convert it to your own time zone, and this will take into account changes for daylight saving too.

Pete.

1 Like