started cleanup and moving from std to no_std
This commit is contained in:
@@ -4,30 +4,9 @@ use crate::log::{log, LogMessage};
|
||||
use crate::STAY_ALIVE;
|
||||
use anyhow::{anyhow, bail, Context};
|
||||
use chrono::{DateTime, Utc};
|
||||
use embedded_svc::ipv4::IpInfo;
|
||||
use embedded_svc::mqtt::client::QoS::{AtLeastOnce, ExactlyOnce};
|
||||
use embedded_svc::wifi::{
|
||||
AccessPointConfiguration, AccessPointInfo, AuthMethod, ClientConfiguration, Configuration,
|
||||
};
|
||||
use esp_idf_hal::delay::Delay;
|
||||
use esp_idf_hal::gpio::{Level, PinDriver};
|
||||
use esp_idf_svc::mqtt::client::{EspMqttClient, LwtConfiguration, MqttClientConfiguration};
|
||||
use esp_idf_svc::sntp;
|
||||
use esp_idf_svc::sntp::SyncStatus;
|
||||
use esp_idf_svc::systime::EspSystemTime;
|
||||
use esp_idf_svc::wifi::config::{ScanConfig, ScanType};
|
||||
use esp_idf_svc::wifi::EspWifi;
|
||||
use esp_idf_sys::{esp_spiffs_info, vTaskDelay};
|
||||
use serde::Serialize;
|
||||
use std::ffi::CString;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
use std::result::Result::Ok as OkStd;
|
||||
use std::str::FromStr;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use alloc::{vec::Vec, string::String};
|
||||
|
||||
#[link_section = ".rtc.data"]
|
||||
static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT];
|
||||
@@ -212,7 +191,7 @@ impl Esp<'_> {
|
||||
bail!("Did not manage wifi connection within timeout");
|
||||
}
|
||||
}
|
||||
println!("Should be connected now, waiting for link to be up");
|
||||
log::info!("Should be connected now, waiting for link to be up");
|
||||
|
||||
while !self.wifi_driver.is_up()? {
|
||||
delay.delay_ms(250);
|
||||
@@ -237,23 +216,26 @@ impl Esp<'_> {
|
||||
pub(crate) fn save_config(&mut self, config: &PlantControllerConfig) -> anyhow::Result<()> {
|
||||
let mut cfg = File::create(Self::CONFIG_FILE)?;
|
||||
serde_json::to_writer(&mut cfg, &config)?;
|
||||
println!("Wrote config config {:?}", config);
|
||||
log::info!("Wrote config config {:?}", config);
|
||||
anyhow::Ok(())
|
||||
}
|
||||
pub(crate) fn mount_file_system(&mut self) -> anyhow::Result<()> {
|
||||
log(LogMessage::MountingFilesystem, 0, 0, "", "");
|
||||
let base_path = CString::new("/spiffs")?;
|
||||
let storage = CString::new(Self::SPIFFS_PARTITION_NAME)?;
|
||||
let conf = esp_idf_sys::esp_vfs_spiffs_conf_t {
|
||||
base_path: base_path.as_ptr(),
|
||||
partition_label: storage.as_ptr(),
|
||||
max_files: 5,
|
||||
format_if_mount_failed: true,
|
||||
};
|
||||
let base_path = String::try_from("/spiffs")?;
|
||||
let storage = String::try_from(Self::SPIFFS_PARTITION_NAME)?;
|
||||
let conf = todo!();
|
||||
|
||||
unsafe {
|
||||
esp_idf_sys::esp!(esp_idf_sys::esp_vfs_spiffs_register(&conf))?;
|
||||
}
|
||||
//let conf = esp_idf_sys::esp_vfs_spiffs_conf_t {
|
||||
//base_path: base_path.as_ptr(),
|
||||
//partition_label: storage.as_ptr(),
|
||||
//max_files: 5,
|
||||
//format_if_mount_failed: true,
|
||||
//};
|
||||
|
||||
//TODO
|
||||
//unsafe {
|
||||
//esp_idf_sys::esp!(esp_idf_sys::esp_vfs_spiffs_register(&conf))?;
|
||||
//}
|
||||
|
||||
let free_space = self.file_system_size()?;
|
||||
log(
|
||||
@@ -377,13 +359,13 @@ impl Esp<'_> {
|
||||
"",
|
||||
);
|
||||
for i in 0..PLANT_COUNT {
|
||||
println!(
|
||||
log::info!(
|
||||
"LAST_WATERING_TIMESTAMP[{}] = UTC {}",
|
||||
i, LAST_WATERING_TIMESTAMP[i]
|
||||
);
|
||||
}
|
||||
for i in 0..PLANT_COUNT {
|
||||
println!(
|
||||
log::info!(
|
||||
"CONSECUTIVE_WATERING_PLANT[{}] = {}",
|
||||
i, CONSECUTIVE_WATERING_PLANT[i]
|
||||
);
|
||||
@@ -468,35 +450,35 @@ impl Esp<'_> {
|
||||
mqtt_connected_event_received_copy
|
||||
.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
mqtt_connected_event_ok_copy.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
println!("Mqtt connected");
|
||||
log::info!("Mqtt connected");
|
||||
}
|
||||
esp_idf_svc::mqtt::client::EventPayload::Disconnected => {
|
||||
mqtt_connected_event_received_copy
|
||||
.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
mqtt_connected_event_ok_copy.store(false, std::sync::atomic::Ordering::Relaxed);
|
||||
println!("Mqtt disconnected");
|
||||
log::info!("Mqtt disconnected");
|
||||
}
|
||||
esp_idf_svc::mqtt::client::EventPayload::Error(esp_error) => {
|
||||
println!("EspMqttError reported {:?}", esp_error);
|
||||
log::info!("EspMqttError reported {:?}", esp_error);
|
||||
mqtt_connected_event_received_copy
|
||||
.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
mqtt_connected_event_ok_copy.store(false, std::sync::atomic::Ordering::Relaxed);
|
||||
println!("Mqtt error");
|
||||
log::info!("Mqtt error");
|
||||
}
|
||||
esp_idf_svc::mqtt::client::EventPayload::BeforeConnect => {
|
||||
println!("Mqtt before connect")
|
||||
log::info!("Mqtt before connect")
|
||||
}
|
||||
esp_idf_svc::mqtt::client::EventPayload::Subscribed(_) => {
|
||||
println!("Mqtt subscribed")
|
||||
log::info!("Mqtt subscribed")
|
||||
}
|
||||
esp_idf_svc::mqtt::client::EventPayload::Unsubscribed(_) => {
|
||||
println!("Mqtt unsubscribed")
|
||||
log::info!("Mqtt unsubscribed")
|
||||
}
|
||||
esp_idf_svc::mqtt::client::EventPayload::Published(_) => {
|
||||
println!("Mqtt published")
|
||||
log::info!("Mqtt published")
|
||||
}
|
||||
esp_idf_svc::mqtt::client::EventPayload::Deleted(_) => {
|
||||
println!("Mqtt deleted")
|
||||
log::info!("Mqtt deleted")
|
||||
}
|
||||
}
|
||||
})?;
|
||||
@@ -506,10 +488,10 @@ impl Esp<'_> {
|
||||
wait_for_connections_event += 1;
|
||||
match mqtt_connected_event_received.load(std::sync::atomic::Ordering::Relaxed) {
|
||||
true => {
|
||||
println!("Mqtt connection callback received, progressing");
|
||||
log::info!("Mqtt connection callback received, progressing");
|
||||
match mqtt_connected_event_ok.load(std::sync::atomic::Ordering::Relaxed) {
|
||||
true => {
|
||||
println!("Mqtt did callback as connected, testing with roundtrip now");
|
||||
log::info!("Mqtt did callback as connected, testing with roundtrip now");
|
||||
//subscribe to roundtrip
|
||||
client.subscribe(round_trip_topic.as_str(), ExactlyOnce)?;
|
||||
client.subscribe(stay_alive_topic.as_str(), ExactlyOnce)?;
|
||||
@@ -526,7 +508,7 @@ impl Esp<'_> {
|
||||
wait_for_roundtrip += 1;
|
||||
match round_trip_ok.load(std::sync::atomic::Ordering::Relaxed) {
|
||||
true => {
|
||||
println!("Round trip registered, proceeding");
|
||||
log::info!("Round trip registered, proceeding");
|
||||
self.mqtt_client = Some(MqttClient {
|
||||
mqtt_client: client,
|
||||
base_topic: base_topic_copy,
|
||||
@@ -557,21 +539,21 @@ impl Esp<'_> {
|
||||
return anyhow::Ok(());
|
||||
}
|
||||
if !subtopic.starts_with("/") {
|
||||
println!("Subtopic without / at start {}", subtopic);
|
||||
log::info!("Subtopic without / at start {}", subtopic);
|
||||
bail!("Subtopic without / at start {}", subtopic);
|
||||
}
|
||||
if subtopic.len() > 192 {
|
||||
println!("Subtopic exceeds 192 chars {}", subtopic);
|
||||
log::info!("Subtopic exceeds 192 chars {}", subtopic);
|
||||
bail!("Subtopic exceeds 192 chars {}", subtopic);
|
||||
}
|
||||
let client = self.mqtt_client.as_mut().unwrap();
|
||||
let mut full_topic: heapless::String<256> = heapless::String::new();
|
||||
if full_topic.push_str(client.base_topic.as_str()).is_err() {
|
||||
println!("Some error assembling full_topic 1");
|
||||
log::info!("Some error assembling full_topic 1");
|
||||
bail!("Some error assembling full_topic 1")
|
||||
};
|
||||
if full_topic.push_str(subtopic).is_err() {
|
||||
println!("Some error assembling full_topic 2");
|
||||
log::info!("Some error assembling full_topic 2");
|
||||
bail!("Some error assembling full_topic 2")
|
||||
};
|
||||
let publish = client
|
||||
@@ -580,7 +562,7 @@ impl Esp<'_> {
|
||||
Delay::new(10).delay_ms(50);
|
||||
match publish {
|
||||
OkStd(message_id) => {
|
||||
println!(
|
||||
log::info!(
|
||||
"Published mqtt topic {} with message {:#?} msgid is {:?}",
|
||||
full_topic,
|
||||
String::from_utf8_lossy(message),
|
||||
@@ -589,7 +571,7 @@ impl Esp<'_> {
|
||||
anyhow::Ok(())
|
||||
}
|
||||
Err(err) => {
|
||||
println!(
|
||||
log::info!(
|
||||
"Error during mqtt send on topic {} with message {:#?} error is {:?}",
|
||||
full_topic,
|
||||
String::from_utf8_lossy(message),
|
||||
|
Reference in New Issue
Block a user