mirror of
https://github.com/stakwork/sphinx-key.git
synced 2025-12-18 07:44:21 +01:00
Add sntp, sync the time on startup
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
pub mod wifi;
|
||||
pub mod http;
|
||||
pub mod mqtt;
|
||||
pub mod sntp;
|
||||
mod html;
|
||||
|
||||
|
||||
|
||||
13
sphinx-key/src/conn/sntp.rs
Normal file
13
sphinx-key/src/conn/sntp.rs
Normal 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));
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user