initial code

This commit is contained in:
Empire 2024-05-09 00:09:03 +02:00
parent 82027caed8
commit 17eb4f3912
3 changed files with 205 additions and 71 deletions

View File

@ -1,17 +1,41 @@
[package] [package]
name = "rust" name = "plant-ctrl2"
version = "0.1.0" version = "0.1.0"
authors = ["Empire <empirephoenix@yahoo.de>"] authors = ["Empire Phoenix"]
edition = "2021" edition = "2021"
resolver = "2" resolver = "2"
rust-version = "1.71" rust-version = "1.71"
[profile.release] [profile.dev]
# Explicitly disable LTO which the Xtensa codegen backend has issues
lto = false
strip = false
debug = true
overflow-checks = true
panic = "abort"
incremental = true
opt-level = "s" opt-level = "s"
[profile.dev] [profile.dev.build-override]
debug = true # Symbols are nice and they don't increase the size on Flash opt-level = 1
opt-level = "z" incremental = true
[package.metadata.cargo_runner]
# The string `$TARGET_FILE` will be replaced with the path from cargo.
command = [
"cargo",
"espflash",
"save-image",
"--partition-table",
"partitions.csv",
"--chip",
"esp32c6",
"image.bin"
]
[package.metadata.espflash]
partition_table = "partitions.csv"
[features] [features]
default = ["std", "embassy", "esp-idf-svc/native"] default = ["std", "embassy", "esp-idf-svc/native"]
@ -25,11 +49,36 @@ embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-
[dependencies] [dependencies]
log = { version = "0.4", default-features = false } log = { version = "0.4", default-features = false }
esp-idf-svc = { version = "0.48", default-features = false } serde = { version = "1.0.192", features = ["derive"] }
average = { version = "0.14.1" , features = ["std"] }
#esp32 = "0.28.0"
bit_field = "0.10.2"
ds18b20 = "0.1.1"
embedded-svc = { version = "0.27.0", features = ["experimental"] } embedded-svc = { version = "0.27.0", features = ["experimental"] }
esp-idf-hal = "0.43.0" esp-idf-hal = "0.43.0"
esp-idf-sys = { version = "0.34.0", features = ["binstart", "native"] } esp-idf-sys = { version = "0.34.0", features = ["binstart", "native"] }
esp-idf-svc = { version = "0.48.0", default-features = false }
esp_idf_build = "0.1.3" esp_idf_build = "0.1.3"
chrono = { version = "0.4.23", default-features = false , features = ["iana-time-zone" , "alloc"] }
chrono-tz = {version="0.8.0", default-features = false , features = [ "filter-by-regex" ]}
embedded-hal = "1.0.0"
one-wire-bus = "0.1.1"
anyhow = { version = "1.0.75", features = ["std", "backtrace"] }
schemars = "0.8.16"
heapless = { version = "0.8", features = ["serde"] }
serde_json = "1.0.108"
strum = { version = "0.26.1", features = ["derive"] }
once_cell = "1.19.0"
measurements = "0.11.0"
bq34z100 = "0.2.1"
[patch.crates-io]
#esp-idf-hal = { git = "https://github.com/esp-rs/esp-idf-hal.git" }
esp-idf-hal = { git = "https://github.com/empirephoenix/esp-idf-hal.git" }
#esp-idf-sys = { git = "https://github.com/empirephoenix/esp-idf-sys.git" }
#esp-idf-sys = { git = "https://github.com/esp-rs/esp-idf-sys.git" }
#esp-idf-svc = { git = "https://github.com/esp-rs/esp-idf-svc.git" }
[build-dependencies] [build-dependencies]
embuild = "0.31.3" embuild = "0.31.3"
vergen = { version = "8.2.6", features = ["build", "git", "gitcl"] }

View File

@ -1,10 +1,10 @@
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K)
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 CONFIG_ESP_MAIN_TASK_STACK_SIZE=25000
# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default).
# This allows to use 1 ms granuality for thread sleeps (10 ms by default). # This allows to use 1 ms granuality for thread sleeps (10 ms by default).
#CONFIG_FREERTOS_HZ=1000 CONFIG_FREERTOS_HZ=1000
# Workaround for https://github.com/espressif/esp-idf/issues/7631 CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n CONFIG_I2C_ENABLE_DEBUG_LOG=y
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n DEBUG_LEVEL=5

View File

@ -1,19 +1,31 @@
use esp_idf_hal::gpio::{AnyInputPin, Gpio10, Gpio6, Gpio5, InputOutput, Level, PinDriver, Pull}; use std::sync::Mutex;
use esp_idf_svc::hal::{peripheral::Peripheral, peripherals::Peripherals};
use esp_idf_sys::{esp, gpio_hold_dis, gpio_hold_en, vTaskDelay, EspError};
use esp_idf_hal::delay::Delay;
use std::time::Duration;
use esp_idf_svc::eventloop::EspSystemEventLoop;
use esp_idf_svc::ipv4::IpInfo;
use esp_idf_svc::mqtt::client::QoS::AtLeastOnce;
use esp_idf_svc::mqtt::client::QoS::ExactlyOnce;
use esp_idf_svc::mqtt::client::{EspMqttClient, LwtConfiguration, MqttClientConfiguration};
use esp_idf_svc::nvs::EspDefaultNvsPartition;
use esp_idf_svc::wifi::config::{ScanConfig, ScanType};
use esp_idf_svc::wifi::EspWifi;
use esp_idf_hal::adc::{attenuation, AdcChannelDriver, AdcDriver};
fn main() { use chrono::{DateTime, Datelike, Timelike};
use chrono_tz::Europe::Berlin;
use esp_idf_hal::delay::Delay;
use esp_idf_sys::
esp_restart
;
use log::error;
use once_cell::sync::Lazy;
use plant_hal::{CreatePlantHal, PlantCtrlBoard, PlantCtrlBoardInteraction, PlantHal};
use serde::{Deserialize, Serialize};
pub mod plant_hal;
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq)]
enum OnlineMode {
Offline,
Wifi,
SnTp,
Online,
}
pub static BOARD_ACCESS: Lazy<Mutex<PlantCtrlBoard>> = Lazy::new(|| PlantHal::create().unwrap());
fn safe_main() -> anyhow::Result<()> {
// It is necessary to call this function once. Otherwise some patches to the runtime // It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_svc::sys::link_patches(); esp_idf_svc::sys::link_patches();
@ -21,54 +33,127 @@ fn main() {
// Bind the log crate to the ESP Logging facilities // Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default(); esp_idf_svc::log::EspLogger::initialize_default();
log::info!("Hello, world!"); if esp_idf_sys::CONFIG_MAIN_TASK_STACK_SIZE < 25000 {
error!(
let mut peripherals = Peripherals::take().unwrap(); "stack too small: {} bail!",
let mut in1 = PinDriver::input_output(peripherals.pins.gpio7).unwrap(); esp_idf_sys::CONFIG_MAIN_TASK_STACK_SIZE
let mut in2 = PinDriver::input_output(peripherals.pins.gpio6).unwrap(); );
return Ok(());
let sys_loop = EspSystemEventLoop::take().unwrap();
let nvs = EspDefaultNvsPartition::take().unwrap();
let mut wifi_driver = EspWifi::new(peripherals.modem, sys_loop, Some(nvs)).unwrap();
wifi_driver.start().unwrap();
wifi_driver.start_scan(
&ScanConfig {
scan_type: ScanType::Passive(Duration::from_secs(1)),
show_hidden: false,
..Default::default()
},
true,
).unwrap();
let sr = wifi_driver.get_scan_result().unwrap();
for r in sr.iter() {
println!("Found wifi {}", r.ssid);
} }
let adc_config = esp_idf_hal::adc::config::Config { log::info!("Startup Rust");
resolution: esp_idf_hal::adc::config::Resolution::Resolution12Bit,
calibration: true, println!("Board hal init");
let mut board: std::sync::MutexGuard<'_, PlantCtrlBoard<'_>> = BOARD_ACCESS.lock().unwrap();
let time = board.time();
let mut cur = match time {
Ok(cur) => cur,
Err(err) => {
log::error!("time error {}", err);
DateTime::from_timestamp_millis(0).unwrap()
}
}; };
let mut tank_driver = AdcDriver::new(peripherals.adc2, &adc_config).unwrap(); //check if we know the time current > 2020
let mut tank_channel: AdcChannelDriver<'_, { attenuation::DB_11 }, Gpio5> = if cur.year() < 2020 {
AdcChannelDriver::new(peripherals.pins.gpio5).unwrap(); //assume TZ safe times ;)
cur = *cur.with_hour(15).get_or_insert(cur);
}
println!("cur is {}", cur);
let mut online_mode: OnlineMode;
println!("attempting to connect wifi");
let mut ssid: heapless::String<32> = heapless::String::new();
let mut password: heapless::String<64> = heapless::String::new();
ssid.push_str("C3MA").unwrap();
password.push_str("penis").unwrap();
let ip_address: Option<String>;
match board.wifi(ssid, Option::Some(password), 10000) {
Ok(ip_info) => {
ip_address = Some(ip_info.ip.to_string());
online_mode = OnlineMode::Wifi;
}
Err(_) => {
panic!("wifi down");
}
}
if online_mode == OnlineMode::Wifi {
match board.sntp(1000 * 5) {
Ok(new_time) => {
cur = new_time;
online_mode = OnlineMode::SnTp;
}
Err(err) => {
println!("Sntp error {}", err);
online_mode = OnlineMode::SnTp;
}
}
}
println!("Running logic at utc {}", cur);
let europe_time = cur.with_timezone(&Berlin);
println!("Running logic at europe/berlin {}", europe_time);
//do mqtt before config check, as mqtt might configure
if online_mode == OnlineMode::SnTp {
match board.mqtt() {
Ok(_) => {
println!("Mqtt connection ready");
online_mode = OnlineMode::Online;
}
Err(err) => {
panic!("Could not connect mqtt due to {}", err);
}
}
}
if online_mode == OnlineMode::Online {
match ip_address {
Some(add_some) => {
let _ = board.mqtt_publish("/firmware/address", add_some.as_bytes());
}
None => {
let _ = board.mqtt_publish("/firmware/address", "N/A?".as_bytes());
}
}
let _ = board.mqtt_publish(
"/firmware/last_online",
europe_time.to_rfc3339().as_bytes(),
);
let _ = board.mqtt_publish("/state", "online".as_bytes());
}
drop(board);
loop { loop {
//wait till earth ends
let mut delay = Delay::new_default(); Delay::new(1000).delay_ms(1000);
let mut lock = BOARD_ACCESS.lock().unwrap();
let position = lock.get_position();
in1.set_low().unwrap(); lock.mqtt_publish("/position", format!("{}", position).as_bytes()).unwrap();
in2.set_high().unwrap();
for i in 0..50 {
delay.delay_ms(100);
let value_up = tank_driver.read(&mut tank_channel).unwrap();
println!("up {}", value_up);
}
let value_down = tank_driver.read(&mut tank_channel).unwrap();
println!("down {}", value_down);
in1.set_low().unwrap();
in2.set_low().unwrap();
delay.delay_ms(5000);
} }
} }
fn main() {
let result = safe_main();
match result {
Ok(_) => {
println!("Main app finished, restarting");
unsafe { esp_restart() };
}
Err(err) => {
panic!("Failed main {}", err);
}
}
}
//error codes
//error_reading_config_after_upgrade
//error_no_config_after_upgrade
//error_tank_sensor_fault