ivmarkov / rust-esp32-std-demo

Rust on ESP32 STD demo app. A demo STD binary crate for the ESP32[XX] and ESP-IDF, which connects to WiFi, Ethernet, drives a small HTTP server and draws on a LED screen.
Apache License 2.0
785 stars 105 forks source link

Time synchronisation example #121

Closed mohs8421 closed 1 year ago

mohs8421 commented 1 year ago

I would like to sync the system clock and get unixtimestamps, but I am unsure how to do it.

I already found out, that I have to start with this:

use esp_idf_svc::sntp::EspSntp;

let sntp = EspSntp::new_default().unwrap();

but how do I access the correct time now?

mohs8421 commented 1 year ago

After some searching, I found a solution, which looks like this:

pub fn time() -> i64 {
    let mut timer: time_t = 0;
    unsafe {
        esp_idf_sys::time(&mut timer);
    }
    timer as i64
}

This might be a nice small addition to the examples.

ivmarkov commented 1 year ago

SysTime from esp-idf-svc.