logging and pcb adjustment

This commit is contained in:
2025-02-14 23:50:19 +01:00
parent 1741bb0b53
commit aad1dbd458
15 changed files with 389 additions and 119 deletions

View File

@@ -2,6 +2,7 @@ use bq34z100::{Bq34Z100Error, Bq34z100g1, Bq34z100g1Driver};
use chrono_tz::Tz;
use ds323x::{DateTimeAccess, Ds323x};
use crate::log::{init, LogMessage};
use eeprom24x::{Eeprom24x, Eeprom24xTrait, SlaveAddr};
use embedded_hal_bus::i2c::MutexDevice;
@@ -22,6 +23,7 @@ use esp_idf_svc::mqtt::client::{EspMqttClient, LwtConfiguration, MqttClientConfi
use esp_idf_svc::nvs::EspDefaultNvsPartition;
use esp_idf_svc::wifi::config::{ScanConfig, ScanType};
use esp_idf_svc::wifi::EspWifi;
use log::logger;
use measurements::Temperature;
use once_cell::sync::Lazy;
use plant_ctrl2::sipo::ShiftRegister40;
@@ -46,7 +48,7 @@ use std::time::Duration;
use embedded_hal::digital::OutputPin;
use esp_idf_hal::delay::Delay;
use esp_idf_hal::gpio::{AnyInputPin, Gpio18, Gpio5, Gpio7, Gpio8, IOPin, InputOutput, Level, PinDriver, Pull};
use esp_idf_hal::gpio::{AnyInputPin, Gpio18, Gpio5, IOPin, InputOutput, Level, PinDriver, Pull};
use esp_idf_hal::pcnt::{
PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex,
};
@@ -54,11 +56,12 @@ use esp_idf_hal::prelude::Peripherals;
use esp_idf_hal::reset::ResetReason;
use esp_idf_svc::sntp::{self, SyncStatus};
use esp_idf_svc::systime::EspSystemTime;
use esp_idf_sys::{esp, esp_spiffs_check, gpio_hold_dis, gpio_hold_en, vTaskDelay, EspError};
use esp_idf_sys::{gpio_hold_dis, gpio_hold_en, vTaskDelay, EspError};
use one_wire_bus::OneWire;
use crate::config::{self, PlantControllerConfig};
use crate::espota::mark_app_valid;
use crate::log::log;
use crate::{plant_hal, to_string, STAY_ALIVE};
//Only support for 8 right now!
@@ -80,14 +83,15 @@ const PUMP5_BIT: usize = 5;
const PUMP6_BIT: usize = 6;
const PUMP7_BIT: usize = 7;
const MS_0: usize = 8;
const MS_4: usize = 9;
const MS_2: usize = 10;
const MS_3: usize = 11;
const SENSOR_ON: usize = 12;
const MS_1: usize = 13;
//unused 14
//unused 15
const CHARGING: usize = 14;
const AWAKE: usize = 15;
const FAULT_3: usize = 16;
const FAULT_8: usize = 17;
@@ -128,10 +132,6 @@ static mut LOW_VOLTAGE_DETECTED: bool = false;
#[link_section = ".rtc.data"]
static mut RESTART_TO_CONF: bool = false;
const BUFFER_SIZE:usize = 120;
const ENTRY_SIZE:usize = 120;
#[link_section = ".rtc.data"]
static mut BUFFER:ConstGenericRingBuffer::<heapless::String<ENTRY_SIZE>, BUFFER_SIZE> = ConstGenericRingBuffer::<heapless::String<ENTRY_SIZE>, BUFFER_SIZE>::new();
pub struct FileSystemSizeInfo {
@@ -266,9 +266,7 @@ impl PlantCtrlBoard<'_> {
OkStd(_) => {},
Err(err) => bail!("Error reading eeprom header {:?}", err),
};
println!("Raw header is {:?} with size {}", header_page_buffer , store);
let header:BackupHeader = bincode::deserialize(&header_page_buffer)?;
println!("Reading eeprom header {header:?}");
let data_start_address = 1*self.eeprom.page_size() as u32;
let mut data_buffer = vec![0_u8; header.size];
@@ -282,19 +280,6 @@ impl PlantCtrlBoard<'_> {
bail!("Invalid checksum, got {} but expected {}", checksum, header.crc16 );
}
unsafe {
let value = heapless::String::try_from("dummy").unwrap();
BUFFER.push(value);
}
unsafe {
for entry in BUFFER.iter() {
let test = entry.as_bytes().to_owned();
for p in test {
data_buffer.push(p);
}
}
}
Ok(data_buffer)
}
@@ -317,9 +302,7 @@ impl PlantCtrlBoard<'_> {
bail!("Size limit reached header is {}, but firest page is only {}",encoded.len(), page_size)
}
let as_u8:&[u8] = &encoded;
println!("Raw header is {:?} with size {}", as_u8 , as_u8.len());
match self.eeprom.write_page(0, as_u8) {
OkStd(_) => {},
Err(err) => bail!("Error writing eeprom {:?}", err),
@@ -375,13 +358,11 @@ impl PlantCtrlBoard<'_> {
let mut iter_error = None;
let mut result = Vec::new();
println!("Filename {filename}");
let filepath = Path::new(BASE_PATH);
let read_dir = fs::read_dir(filepath);
match read_dir {
OkStd(read_dir) => {
for item in read_dir {
println!("start loop");
match item {
OkStd(file) => {
let f = FileInfo {
@@ -392,7 +373,6 @@ impl PlantCtrlBoard<'_> {
.unwrap_or_default()
as usize,
};
println!("fileinfo {f:?}");
result.push(f);
}
Err(err) => {
@@ -503,8 +483,8 @@ impl PlantCtrlBoard<'_> {
let r2 = median * 50.0 / (3.3 - median);
let mut percent = r2 / 190_f32 * 100_f32;
percent = percent.clamp(0.0, 100.0);
log(LogMessage::sensor_tank_raw, median as u32, percent as u32, "","");
println!("Tank sensor raw {} percent {}", median, percent);
return Ok(percent as u16);
}
@@ -620,7 +600,7 @@ impl PlantCtrlBoard<'_> {
self.time()
}
pub fn measure_moisture_hz(&mut self, plant: usize, sensor: Sensor) -> Result<i32> {
pub fn measure_moisture_hz(&mut self, plant: usize, sensor: Sensor) -> Result<u32> {
let sensor_channel = match sensor {
Sensor::A => match plant {
0 => SENSOR_A_1,
@@ -646,7 +626,7 @@ impl PlantCtrlBoard<'_> {
},
};
let mut results = [0; REPEAT_MOIST_MEASURE];
let mut results = [0_u32; REPEAT_MOIST_MEASURE];
for repeat in 0..REPEAT_MOIST_MEASURE {
self.signal_counter.counter_pause()?;
self.signal_counter.counter_clear()?;
@@ -675,13 +655,9 @@ impl PlantCtrlBoard<'_> {
.unwrap();
delay.delay_ms(10);
let unscaled = self.signal_counter.get_counter_value()? as i32;
let hz = (unscaled as f32 * factor) as i32;
println!(
"raw measure unscaled {} hz {}, plant {} sensor {:?}",
unscaled, hz, plant, sensor
);
let hz = (unscaled as f32 * factor) as u32;
log(LogMessage::raw_measure, unscaled as u32, hz as u32, &plant.to_string(), &format!("{sensor:?}"));
results[repeat] = hz;
//println!("Measuring {:?} @ {} with {}", sensor, plant, hz);
}
results.sort();
@@ -745,7 +721,6 @@ impl PlantCtrlBoard<'_> {
let delay = Delay::new_default();
let mut counter = 0_u32;
while !self.wifi_driver.is_connected()? {
println!("Waiting for station connection");
delay.delay_ms(250);
counter += 250;
if counter > max_wait {
@@ -758,7 +733,6 @@ impl PlantCtrlBoard<'_> {
println!("Should be connected now");
while !self.wifi_driver.is_up()? {
println!("Waiting for network being up");
delay.delay_ms(250);
counter += 250;
if counter > max_wait {
@@ -770,7 +744,7 @@ impl PlantCtrlBoard<'_> {
}
//update freertos registers ;)
let address = self.wifi_driver.sta_netif().get_ip_info()?;
println!("IP info: {:?}", address);
log(LogMessage::wifi_info, 0 ,0,"", &format!("{address:?}"));
Ok(address)
}
@@ -908,9 +882,16 @@ impl PlantCtrlBoard<'_> {
for plant in 0..PLANT_COUNT {
let a = self.measure_moisture_hz(plant, plant_hal::Sensor::A);
let b = self.measure_moisture_hz(plant, plant_hal::Sensor::B);
print!("P:{} a:{:?} b:{:?}", plant, a, b)
let aa = match a {
OkStd(a) => a as u32,
Err(_) => u32::MAX
};
let bb = match b {
OkStd(b) => b as u32,
Err(_) => u32::MAX
};
log(LogMessage::test_sensor, aa ,bb,&plant.to_string(), "");
}
println!();
Delay::new_default().delay_ms(10);
Ok(())
}
@@ -958,18 +939,15 @@ impl PlantCtrlBoard<'_> {
let round_trip_ok = Arc::new(AtomicBool::new(false));
let round_trip_topic = format!("{}/internal/roundtrip", base_topic);
let stay_alive_topic = format!("{}/stay_alive", base_topic);
println!("Stay alive topic is {}", stay_alive_topic);
log(LogMessage::stay_alive, 0 ,0,"", &stay_alive_topic);
let mqtt_connected_event_received_copy = mqtt_connected_event_received.clone();
let mqtt_connected_event_ok_copy = mqtt_connected_event_ok.clone();
let stay_alive_topic_copy = stay_alive_topic.clone();
let round_trip_topic_copy = round_trip_topic.clone();
let round_trip_ok_copy = round_trip_ok.clone();
println!(
"Connecting mqtt {} with id {}",
mqtt_url,
mqtt_client_config.client_id.unwrap_or("not set")
);
let client_id = mqtt_client_config.client_id.unwrap_or("not set");
log(LogMessage::mqtt_info, 0 ,0,client_id, &mqtt_url);
let mut client = EspMqttClient::new_cb(&mqtt_url, &mqtt_client_config, move |event| {
let payload = event.payload();
match payload {
@@ -987,10 +965,10 @@ impl PlantCtrlBoard<'_> {
} else if topic.eq(stay_alive_topic_copy.as_str()) {
let value =
data.eq_ignore_ascii_case("true") || data.eq_ignore_ascii_case("1");
println!("Received stay alive with value {}", value);
log(LogMessage::mqtt_stay_alive_rec, 0 ,0,&data, "");
STAY_ALIVE.store(value, std::sync::atomic::Ordering::Relaxed);
} else {
println!("Unknown topic recieved {}", topic);
log(LogMessage::unknown_topic, 0 ,0,"", &topic);
}
}
}
@@ -1327,6 +1305,38 @@ impl PlantHal {
pub fn create() -> Result<Mutex<PlantCtrlBoard<'static>>> {
let peripherals = Peripherals::take()?;
let mut clock = PinDriver::input_output(peripherals.pins.gpio15.downgrade())?;
clock.set_pull(Pull::Floating).unwrap();
let mut latch = PinDriver::input_output(peripherals.pins.gpio3.downgrade())?;
latch.set_pull(Pull::Floating).unwrap();
let mut data = PinDriver::input_output(peripherals.pins.gpio23.downgrade())?;
data.set_pull(Pull::Floating).unwrap();
let shift_register = ShiftRegister40::new(clock.into(), latch.into(), data.into());
//disable all
for mut pin in shift_register.decompose() {
pin.set_low().unwrap();
}
let awake = &mut shift_register.decompose()[AWAKE];
awake.set_high()?;
let charging = &mut shift_register.decompose()[CHARGING];
charging.set_high()?;
let ms0 = &mut shift_register.decompose()[MS_0];
ms0.set_low()?;
let ms1 = &mut shift_register.decompose()[MS_1];
ms1.set_low()?;
let ms2 = &mut shift_register.decompose()[MS_2];
ms2.set_low()?;
let ms3 = &mut shift_register.decompose()[MS_3];
ms3.set_low()?;
let ms4 = &mut shift_register.decompose()[MS_4];
ms4.set_high()?;
println!("Init battery driver");
let mut battery_driver = Bq34z100g1Driver {
i2c: MutexDevice::new(&I2C_DRIVER),
@@ -1345,6 +1355,10 @@ impl PlantHal {
)
};
let mut one_wire_pin = PinDriver::input_output_od(peripherals.pins.gpio18)?;
one_wire_pin.set_pull(Pull::Floating).unwrap();
let rtc_time = rtc.datetime();
match rtc_time {
OkStd(tt) => {
@@ -1363,33 +1377,6 @@ impl PlantHal {
}
}
let mut clock = PinDriver::input_output(peripherals.pins.gpio15.downgrade())?;
clock.set_pull(Pull::Floating).unwrap();
let mut latch = PinDriver::input_output(peripherals.pins.gpio3.downgrade())?;
latch.set_pull(Pull::Floating).unwrap();
let mut data = PinDriver::input_output(peripherals.pins.gpio23.downgrade())?;
data.set_pull(Pull::Floating).unwrap();
let shift_register = ShiftRegister40::new(clock.into(), latch.into(), data.into());
for mut pin in shift_register.decompose() {
pin.set_low().unwrap();
}
let mut one_wire_pin = PinDriver::input_output_od(peripherals.pins.gpio18)?;
one_wire_pin.set_pull(Pull::Floating).unwrap();
//disable all
let ms0 = &mut shift_register.decompose()[MS_0];
ms0.set_low()?;
let ms1 = &mut shift_register.decompose()[MS_1];
ms1.set_low()?;
let ms2 = &mut shift_register.decompose()[MS_2];
ms2.set_low()?;
let ms3 = &mut shift_register.decompose()[MS_3];
ms3.set_low()?;
let ms4 = &mut shift_register.decompose()[MS_4];
ms4.set_high()?;
//init,reset rtc memory depending on cause
let mut init_rtc_store: bool = false;
let mut to_config_mode: bool = false;
@@ -1430,13 +1417,13 @@ impl PlantHal {
init_rtc_store = true
},
};
println!("Reset due to {:?} requires rtc clear {} and force config mode {}", reasons, init_rtc_store, to_config_mode);
log(LogMessage::reset_reason, init_rtc_store as u32, to_config_mode as u32, "",&format!("{reasons:?}"));
if init_rtc_store {
unsafe {
LAST_WATERING_TIMESTAMP = [0; PLANT_COUNT];
CONSECUTIVE_WATERING_PLANT = [0; PLANT_COUNT];
LOW_VOLTAGE_DETECTED = false;
BUFFER = ConstGenericRingBuffer::<heapless::String<ENTRY_SIZE>, BUFFER_SIZE>::new();
crate::log::init();
RESTART_TO_CONF = to_config_mode;
};
} else {
@@ -1444,12 +1431,8 @@ impl PlantHal {
if to_config_mode{
RESTART_TO_CONF = true;
}
println!("Current restart to conf mode{:?}", RESTART_TO_CONF);
println!(
"Current low voltage detection is {:?}",
LOW_VOLTAGE_DETECTED
);
log(LogMessage::restart_to_config, RESTART_TO_CONF as u32, 0, "","");
log(LogMessage::low_voltage, LOW_VOLTAGE_DETECTED as u32, 0, "","");
for i in 0..PLANT_COUNT {
println!(
"LAST_WATERING_TIMESTAMP[{}] = UTC {}",
@@ -1473,8 +1456,6 @@ impl PlantHal {
Option::<AnyInputPin>::None,
)?;
println!("Channel config start");
counter_unit1.channel_config(
PcntChannel::Channel0,
PinIndex::Pin0,
@@ -1489,8 +1470,6 @@ impl PlantHal {
},
)?;
println!("Wifi start");
let sys_loop = EspSystemEventLoop::take()?;
let nvs = EspDefaultNvsPartition::take()?;
let wifi_driver = EspWifi::new(peripherals.modem, sys_loop, Some(nvs))?;
@@ -1525,15 +1504,15 @@ impl PlantHal {
let one_wire_bus = OneWire::new(one_wire_pin)
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
println!("After stuff");
let status = print_battery(&mut battery_driver);
if status.is_err() {
println!("Error communicating with battery!! {:?}", status.err());
} else {
println!("Managed to comunnicate with battery");
match status {
OkStd(_) => {
},
Err(err) => {
log(LogMessage::battery_communication_error, 0 as u32, 0, "",&format!("{err:?})"));
},
}
let shift_register_enable_invert = PinDriver::output(peripherals.pins.gpio21.downgrade())?;
let rv = Mutex::new(PlantCtrlBoard {