Add sntp, sync the time on startup

This commit is contained in:
decentclock
2022-08-17 12:26:47 -06:00
parent 4e006b0b16
commit e759eebd6f
3 changed files with 24 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
pub mod wifi;
pub mod http;
pub mod mqtt;
pub mod sntp;
mod html;

View File

@@ -0,0 +1,13 @@
use esp_idf_svc::sntp::EspSntp;
use esp_idf_svc::sntp::SyncStatus::Completed;
use std::time::Duration;
use std::thread;
pub fn sync_time() {
let sntp = EspSntp::new_default().unwrap();
println!("SNTP initialized");
while sntp.get_sync_status() != Completed {
println!("Waiting for sntp sync...");
thread::sleep(Duration::from_secs(1));
}
}

View File

@@ -12,6 +12,7 @@ use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, alway
use std::sync::{mpsc, Arc};
use std::thread;
use std::time::Duration;
use std::time::SystemTime;
use embedded_svc::storage::RawStorage;
use esp_idf_hal::peripherals::Peripherals;
@@ -74,6 +75,15 @@ fn main() -> Result<()> {
}
};
conn::sntp::sync_time();
let now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
println!(
"Completed the time sync, here is the UNIX time: {}",
now.as_secs(),
);
led_tx.send(Status::ConnectingToMqtt).unwrap();
// _conn needs to stay in scope or its dropped
loop {