sensor and esp and canable call all communicate with each other.

This commit is contained in:
2025-10-18 00:58:12 +02:00
parent ea608dfa6a
commit a446f3ace2
232 changed files with 9387 additions and 3067870 deletions

View File

@@ -1,13 +0,0 @@
use crate::hal::Sensor;
use bincode::{Decode, Encode};
pub(crate) const SENSOR_BASE_ADDRESS: u16 = 1000;
#[derive(Debug, Clone, Copy, Encode, Decode)]
pub(crate) struct AutoDetectRequest {}
#[derive(Debug, Clone, Copy, Encode, Decode)]
pub(crate) struct ResponseMoisture {
pub plant: u8,
pub sensor: Sensor,
pub hz: u32,
}

View File

@@ -11,13 +11,13 @@ use async_trait::async_trait;
use bincode::config;
use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_time::{Instant, Timer, WithTimeout};
use embassy_time::{Duration, Instant, Timer, WithTimeout};
use embedded_can::{Frame, Id};
use esp_hal::gpio::Output;
use esp_hal::i2c::master::I2c;
use esp_hal::pcnt::unit::Unit;
use esp_hal::twai::{EspTwaiFrame, StandardId, Twai, TwaiConfiguration};
use esp_hal::{Blocking};
use esp_hal::{Async, Blocking};
use log::{error, info, warn};
use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface};
@@ -148,52 +148,8 @@ impl SensorImpl {
}
let mut result = DetectionResult::default();
loop {
match as_async.receive_async().with_deadline(Instant::from_millis(100)).await {
Ok(or) => {
match or {
Ok(can_frame) => {
match can_frame.id() {
Id::Standard(id) => {
let rawid = id.as_raw();
match classify(rawid) {
None => {}
Some(msg) => {
if msg.0 == MessageKind::MoistureData {
let plant = msg.1 as usize;
let sensor = msg.2;
match sensor {
SensorSlot::A => {
result.plant[plant].sensor_a = true;
}
SensorSlot::B => {
result.plant[plant].sensor_b = true;
}
}
}
}
}
}
Id::Extended(ext) => {
warn!("Received extended ID: {:?}", ext);
}
}
}
Err(err ) => {
error!("Error receiving CAN message: {:?}", err);
break;
}
}
info!("Received CAN message: {:?}", or);
}
Err(err) => {
error!("Timeout receiving CAN message: {:?}", err);
break;
}
}
}
// Wait for messages to arrive
let _ = Self::wait_for_can_measurements(&mut as_async, &mut result).with_timeout(Duration::from_millis(5000)).await;
let config = as_async.stop().into_blocking();
can_power.set_low();
@@ -205,6 +161,46 @@ impl SensorImpl {
}
}
async fn wait_for_can_measurements(as_async: &mut Twai<'_, Async>, result: &mut DetectionResult) {
loop {
match as_async.receive_async().await {
Ok(can_frame) => {
match can_frame.id() {
Id::Standard(id) => {
info!("Received CAN message: {:?}", id);
let rawid = id.as_raw();
match classify(rawid) {
None => {}
Some(msg) => {
info!("received message of kind {:?} (plant: {}, sensor: {:?})", msg.0, msg.1, msg.2);
if msg.0 == MessageKind::MoistureData {
let plant = msg.1 as usize;
let sensor = msg.2;
match sensor {
SensorSlot::A => {
result.plant[plant].sensor_a = true;
}
SensorSlot::B => {
result.plant[plant].sensor_b = true;
}
}
}
}
}
}
Id::Extended(ext) => {
warn!("Received extended ID: {:?}", ext);
}
}
}
Err(err) => {
error!("Error receiving CAN message: {:?}", err);
break;
}
}
}
}
pub async fn inner_pulse(plant: usize, sensor: Sensor, signal_counter: &mut Unit<'_, 0>, sensor_expander: &mut Pca9535Immediate<I2cDevice<'static, CriticalSectionRawMutex, I2c<'static, Blocking>>>) -> FatResult<f32> {
let mut results = [0_f32; REPEAT_MOIST_MEASURE];

View File

@@ -38,52 +38,6 @@ use embassy_net::Stack;
use embassy_time::Instant;
use embedded_io_async::{Read, Write};
use log::{error, info};
// fn ota(
// request: &mut Request<&mut EspHttpConnection>,
// ) -> Result<Option<std::string::String>, anyhow::Error> {
// let mut board = BOARD_ACCESS.lock().unwrap();
// let mut ota = OtaUpdate::begin()?;
// log::info!("start ota");
//
// //having a larger buffer is not really faster, requires more stack and prevents the progress bar from working ;)
// const BUFFER_SIZE: usize = 512;
// let mut buffer: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE];
// let mut total_read: usize = 0;
// let mut lastiter = 0;
// loop {
// let read = request.read(&mut buffer)?;
// total_read += read;
// let to_write = &buffer[0..read];
// //delay for watchdog and wifi stuff
// board.board_hal.get_esp().delay.delay_ms(1);
//
// let iter = (total_read / 1024) % 8;
// if iter != lastiter {
// board.board_hal.general_fault(iter % 5 == 0);
// for i in 0..PLANT_COUNT {
// let _ = board.board_hal.fault(i, iter == i);
// }
// lastiter = iter;
// }
//
// ota.write(to_write)?;
// if read == 0 {
// break;
// }
// }
// log::info!("wrote bytes ota {total_read}");
// log::info!("finish ota");
// let partition = ota.raw_partition();
// log::info!("finalizing and changing boot partition to {partition:?}");
//
// let mut finalizer = ota.finalize()?;
// log::info!("changing boot partition");
// board.board_hal.get_esp().set_restart_to_conf(true);
// drop(board);
// finalizer.set_as_boot_partition()?;
// anyhow::Ok(None)
// }
//
struct HTTPRequestRouter {
reboot_now: Arc<AtomicBool>,