use rmp-serde to store config in NVS

This commit is contained in:
Evan Feenstra
2022-07-21 11:55:37 -07:00
parent 84f5ddc936
commit b6fdb3f367
2 changed files with 9 additions and 4 deletions

View File

@@ -35,6 +35,7 @@ serde_urlencoded = "0.7.1"
serde = { version = "1.0.137", default-features = false }
serde_json = { version = "1.0.81", default-features = false }
hex = "0.4.3"
rmp-serde = "1.1.0"
[patch.crates-io]
# updates the "rand" create to use esp RNG

View File

@@ -13,7 +13,7 @@ use std::sync::{mpsc, Arc};
use std::thread;
use std::time::Duration;
use embedded_svc::storage::StorageBase;
use embedded_svc::storage::RawStorage;
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_svc::nvs::*;
use esp_idf_svc::nvs_storage::EspNvsStorage;
@@ -49,8 +49,11 @@ fn main() -> Result<()> {
let default_nvs = Arc::new(EspDefaultNvs::new()?);
let mut store =
EspNvsStorage::new_default(default_nvs.clone(), "sphinx", true).expect("no storage");
let existing: Option<Config> = store.get("config").expect("failed");
if let Some(exist) = existing {
let mut buf = [0u8; 250];
// let existing: Option<Config> = store.get_raw("config", buf).expect("failed");
let existing = store.get_raw("config", &mut buf).expect("failed");
if let Some((exist_bytes, _)) = existing {
let exist: Config = rmp_serde::from_slice(exist_bytes).expect("failed to parse Config");
println!(
"=============> START CLIENT NOW <============== {:?}",
exist
@@ -82,8 +85,9 @@ fn main() -> Result<()> {
led_tx.send(Status::WifiAccessPoint).unwrap();
println!("=============> START SERVER NOW AND WAIT <==============");
if let Ok((wifi, config)) = start_config_server_and_wait(default_nvs.clone()) {
let conf = rmp_serde::to_vec(&config).expect("couldnt rmp Config");
store
.put("config", &config)
.put_raw("config", &conf[..])
.expect("could not store config");
println!("CONFIG SAVED");
drop(wifi);