Compare commits
6 Commits
feature/mq
...
0b386b0ca3
| Author | SHA1 | Date | |
|---|---|---|---|
|
0b386b0ca3
|
|||
|
0ab1ea3635
|
|||
|
ae73f12d1c
|
|||
|
cfe1c2c6d8
|
|||
|
578379c0d9
|
|||
|
2ff219a1cb
|
9
.gitignore
vendored
9
.gitignore
vendored
@@ -8,6 +8,15 @@ target
|
|||||||
Cargo.lock
|
Cargo.lock
|
||||||
node_modules/
|
node_modules/
|
||||||
rust/src/webserver/bundle.js
|
rust/src/webserver/bundle.js
|
||||||
|
rust/src/webserver/bundle.js.gz
|
||||||
rust/src/webserver/index.html
|
rust/src/webserver/index.html
|
||||||
|
rust/src/webserver/index.html.gz
|
||||||
|
rust/src_webpack/bundle.js
|
||||||
|
rust/src_webpack/bundle.js.gz
|
||||||
|
rust/src_webpack/index.html
|
||||||
|
rust/src_webpack/index.html.gz
|
||||||
rust/build/
|
rust/build/
|
||||||
rust/image.bin
|
rust/image.bin
|
||||||
|
rust/target/
|
||||||
|
rust/Cargo.lock
|
||||||
|
rust/src_webpack/node_modules/
|
||||||
|
|||||||
22
rust/all.sh
Executable file
22
rust/all.sh
Executable file
@@ -0,0 +1,22 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
|
||||||
|
"${SCRIPT_DIR}/build_website.sh"
|
||||||
|
|
||||||
|
cargo build --release
|
||||||
|
espflash save-image \
|
||||||
|
--bootloader "${SCRIPT_DIR}/bootloader.bin" \
|
||||||
|
--partition-table "${SCRIPT_DIR}/partitions.csv" \
|
||||||
|
--chip esp32c6 \
|
||||||
|
target/riscv32imac-unknown-none-elf/release/plant-ctrl2 \
|
||||||
|
"${SCRIPT_DIR}/image.bin"
|
||||||
|
|
||||||
|
espflash flash --monitor \
|
||||||
|
--bootloader "${SCRIPT_DIR}/bootloader.bin" \
|
||||||
|
--chip esp32c6 \
|
||||||
|
--baud 921600 \
|
||||||
|
--partition-table "${SCRIPT_DIR}/partitions.csv" \
|
||||||
|
target/riscv32imac-unknown-none-elf/release/plant-ctrl2
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
use std::process::Command;
|
|
||||||
|
|
||||||
use vergen::EmitBuilder;
|
use vergen::EmitBuilder;
|
||||||
|
|
||||||
fn linker_be_nice() {
|
fn linker_be_nice() {
|
||||||
@@ -50,72 +48,9 @@ fn linker_be_nice() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
webpack();
|
|
||||||
linker_be_nice();
|
linker_be_nice();
|
||||||
|
// Non-existent path causes Cargo to always re-run this script,
|
||||||
|
// keeping VERGEN_BUILD_TIMESTAMP fresh on every build.
|
||||||
|
println!("cargo:rerun-if-changed=ALWAYS_REBUILD_SENTINEL");
|
||||||
let _ = EmitBuilder::builder().all_git().all_build().emit();
|
let _ = EmitBuilder::builder().all_git().all_build().emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn webpack() {
|
|
||||||
//println!("cargo:rerun-if-changed=./src/src_webpack");
|
|
||||||
Command::new("rm")
|
|
||||||
.arg("./src/webserver/bundle.js.gz")
|
|
||||||
.output()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
match Command::new("cmd").spawn() {
|
|
||||||
Ok(_) => {
|
|
||||||
println!("Assuming build on windows");
|
|
||||||
let output = Command::new("cmd")
|
|
||||||
.arg("/K")
|
|
||||||
.arg("npx")
|
|
||||||
.arg("webpack")
|
|
||||||
.current_dir("./src_webpack")
|
|
||||||
.output()
|
|
||||||
.unwrap();
|
|
||||||
println!("status: {}", output.status);
|
|
||||||
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
|
||||||
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
|
||||||
assert!(output.status.success());
|
|
||||||
|
|
||||||
// move webpack results to rust webserver src
|
|
||||||
let _ = Command::new("cmd")
|
|
||||||
.arg("/K")
|
|
||||||
.arg("move")
|
|
||||||
.arg("./src_webpack/bundle.js.gz")
|
|
||||||
.arg("./src/webserver")
|
|
||||||
.output()
|
|
||||||
.unwrap();
|
|
||||||
let _ = Command::new("cmd")
|
|
||||||
.arg("/K")
|
|
||||||
.arg("move")
|
|
||||||
.arg("./src_webpack/index.html.gz")
|
|
||||||
.arg("./src/webserver")
|
|
||||||
.output()
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
println!("Assuming build on linux");
|
|
||||||
let output = Command::new("npx")
|
|
||||||
.arg("webpack")
|
|
||||||
.current_dir("./src_webpack")
|
|
||||||
.output()
|
|
||||||
.unwrap();
|
|
||||||
println!("status: {}", output.status);
|
|
||||||
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
|
||||||
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
|
||||||
assert!(output.status.success());
|
|
||||||
|
|
||||||
// move webpack results to rust webserver src
|
|
||||||
let _ = Command::new("mv")
|
|
||||||
.arg("./src_webpack/bundle.js.gz")
|
|
||||||
.arg("./src/webserver")
|
|
||||||
.output()
|
|
||||||
.unwrap();
|
|
||||||
let _ = Command::new("mv")
|
|
||||||
.arg("./src_webpack/index.html.gz")
|
|
||||||
.arg("./src/webserver")
|
|
||||||
.output()
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
21
rust/build_website.sh
Executable file
21
rust/build_website.sh
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
WEBPACK_DIR="${SCRIPT_DIR}/src_webpack"
|
||||||
|
WEBSERVER_DIR="${SCRIPT_DIR}/src/webserver"
|
||||||
|
|
||||||
|
rm -f "${WEBSERVER_DIR}/index.html.gz"
|
||||||
|
rm -f "${WEBSERVER_DIR}/bundle.js.gz"
|
||||||
|
rm -f "${WEBPACK_DIR}/index.html.gz"
|
||||||
|
rm -f "${WEBPACK_DIR}/bundle.js.gz"
|
||||||
|
rm -f "${WEBPACK_DIR}/index.html"
|
||||||
|
rm -f "${WEBPACK_DIR}/bundle.js"
|
||||||
|
|
||||||
|
pushd "${WEBPACK_DIR}"
|
||||||
|
npm install
|
||||||
|
npx webpack build
|
||||||
|
cp index.html.gz "${WEBSERVER_DIR}/index.html.gz"
|
||||||
|
cp bundle.js.gz "${WEBSERVER_DIR}/bundle.js.gz"
|
||||||
|
popd
|
||||||
7
rust/erase_ota.sh
Executable file
7
rust/erase_ota.sh
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
|
||||||
|
cargo espflash erase-parts otadata --partition-table "${SCRIPT_DIR}/partitions.csv"
|
||||||
15
rust/flash.sh
Executable file
15
rust/flash.sh
Executable file
@@ -0,0 +1,15 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
|
||||||
|
"${SCRIPT_DIR}/build_website.sh"
|
||||||
|
|
||||||
|
cargo build --release
|
||||||
|
espflash flash --monitor \
|
||||||
|
--bootloader "${SCRIPT_DIR}/bootloader.bin" \
|
||||||
|
--chip esp32c6 \
|
||||||
|
--baud 921600 \
|
||||||
|
--partition-table "${SCRIPT_DIR}/partitions.csv" \
|
||||||
|
target/riscv32imac-unknown-none-elf/release/plant-ctrl2
|
||||||
17
rust/image_build.sh
Executable file
17
rust/image_build.sh
Executable file
@@ -0,0 +1,17 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
|
||||||
|
rm -f "${SCRIPT_DIR}/image.bin"
|
||||||
|
|
||||||
|
"${SCRIPT_DIR}/build_website.sh"
|
||||||
|
|
||||||
|
cargo build --release
|
||||||
|
espflash save-image \
|
||||||
|
--bootloader "${SCRIPT_DIR}/bootloader.bin" \
|
||||||
|
--partition-table "${SCRIPT_DIR}/partitions.csv" \
|
||||||
|
--chip esp32c6 \
|
||||||
|
target/riscv32imac-unknown-none-elf/release/plant-ctrl2 \
|
||||||
|
"${SCRIPT_DIR}/image.bin"
|
||||||
@@ -268,7 +268,6 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
|||||||
publish_firmware_info(&mut board, version, ip_address, &timezone_time.to_rfc3339()).await;
|
publish_firmware_info(&mut board, version, ip_address, &timezone_time.to_rfc3339()).await;
|
||||||
publish_battery_state(&mut board).await;
|
publish_battery_state(&mut board).await;
|
||||||
let _ = publish_mppt_state(&mut board).await;
|
let _ = publish_mppt_state(&mut board).await;
|
||||||
publish_config(&mut board).await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log(
|
log(
|
||||||
@@ -805,21 +804,6 @@ async fn publish_battery_state(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn publish_config(
|
|
||||||
board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'_>>,
|
|
||||||
) {
|
|
||||||
let config = board.board_hal.get_config();
|
|
||||||
match serde_json::to_string(&config) {
|
|
||||||
Ok(serialized) => {
|
|
||||||
let _ = mqtt::publish("/config", &serialized).await;
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
info!("Error serializing config: {}", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async fn wait_infinity(
|
async fn wait_infinity(
|
||||||
board: MutexGuard<'_, CriticalSectionRawMutex, HAL<'static>>,
|
board: MutexGuard<'_, CriticalSectionRawMutex, HAL<'static>>,
|
||||||
wait_type: WaitType,
|
wait_type: WaitType,
|
||||||
|
|||||||
@@ -4,12 +4,10 @@ use crate::fat_error::{ContextExt, FatError, FatResult};
|
|||||||
use crate::hal::PlantHal;
|
use crate::hal::PlantHal;
|
||||||
use crate::log::{log, LogMessage};
|
use crate::log::{log, LogMessage};
|
||||||
use alloc::string::String;
|
use alloc::string::String;
|
||||||
use alloc::{format, string::ToString, vec::Vec};
|
use alloc::{format, string::ToString};
|
||||||
use core::sync::atomic::Ordering;
|
use core::sync::atomic::Ordering;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_net::Stack;
|
use embassy_net::Stack;
|
||||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
|
||||||
use embassy_sync::mutex::Mutex;
|
|
||||||
use embassy_sync::once_lock::OnceLock;
|
use embassy_sync::once_lock::OnceLock;
|
||||||
use embassy_time::{Duration, Timer, WithTimeout};
|
use embassy_time::{Duration, Timer, WithTimeout};
|
||||||
use log::info;
|
use log::info;
|
||||||
@@ -39,7 +37,6 @@ static MQTT_CONNECTED_EVENT_RECEIVED: AtomicBool = AtomicBool::new(false);
|
|||||||
static MQTT_ROUND_TRIP_RECEIVED: AtomicBool = AtomicBool::new(false);
|
static MQTT_ROUND_TRIP_RECEIVED: AtomicBool = AtomicBool::new(false);
|
||||||
pub static MQTT_STAY_ALIVE: AtomicBool = AtomicBool::new(false);
|
pub static MQTT_STAY_ALIVE: AtomicBool = AtomicBool::new(false);
|
||||||
static MQTT_BASE_TOPIC: OnceLock<String> = OnceLock::new();
|
static MQTT_BASE_TOPIC: OnceLock<String> = OnceLock::new();
|
||||||
static MQTT_CONFIG_UPDATE_PAYLOAD: Mutex<CriticalSectionRawMutex, Option<String>> = Mutex::new(None);
|
|
||||||
|
|
||||||
pub fn is_stay_alive() -> bool {
|
pub fn is_stay_alive() -> bool {
|
||||||
MQTT_STAY_ALIVE.load(Ordering::Relaxed)
|
MQTT_STAY_ALIVE.load(Ordering::Relaxed)
|
||||||
@@ -144,8 +141,6 @@ pub async fn mqtt_init(
|
|||||||
let last_will_topic = format!("{base_topic}/state");
|
let last_will_topic = format!("{base_topic}/state");
|
||||||
let round_trip_topic = format!("{base_topic}/internal/roundtrip");
|
let round_trip_topic = format!("{base_topic}/internal/roundtrip");
|
||||||
let stay_alive_topic = format!("{base_topic}/stay_alive");
|
let stay_alive_topic = format!("{base_topic}/stay_alive");
|
||||||
let config_update_payload_topic = format!("{base_topic}/config/update_payload");
|
|
||||||
let config_update_topic = format!("{base_topic}/config/update");
|
|
||||||
|
|
||||||
let mut builder: McutieBuilder<'_, String, PublishDisplay<String, &str>, 0> =
|
let mut builder: McutieBuilder<'_, String, PublishDisplay<String, &str>, 0> =
|
||||||
McutieBuilder::new(stack, "plant ctrl", mqtt_url);
|
McutieBuilder::new(stack, "plant ctrl", mqtt_url);
|
||||||
@@ -164,12 +159,10 @@ pub async fn mqtt_init(
|
|||||||
//TODO make configurable
|
//TODO make configurable
|
||||||
builder = builder.with_device_id("plantctrl");
|
builder = builder.with_device_id("plantctrl");
|
||||||
|
|
||||||
let builder: McutieBuilder<'_, String, PublishDisplay<String, &str>, 4> = builder
|
let builder: McutieBuilder<'_, String, PublishDisplay<String, &str>, 2> = builder
|
||||||
.with_subscriptions([
|
.with_subscriptions([
|
||||||
Topic::General(round_trip_topic.clone()),
|
Topic::General(round_trip_topic.clone()),
|
||||||
Topic::General(stay_alive_topic.clone()),
|
Topic::General(stay_alive_topic.clone()),
|
||||||
Topic::General(config_update_payload_topic.clone()),
|
|
||||||
Topic::General(config_update_topic.clone()),
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let keep_alive = Duration::from_secs(60 * 60 * 2).as_secs() as u16;
|
let keep_alive = Duration::from_secs(60 * 60 * 2).as_secs() as u16;
|
||||||
@@ -179,8 +172,6 @@ pub async fn mqtt_init(
|
|||||||
receiver,
|
receiver,
|
||||||
round_trip_topic.clone(),
|
round_trip_topic.clone(),
|
||||||
stay_alive_topic.clone(),
|
stay_alive_topic.clone(),
|
||||||
config_update_payload_topic.clone(),
|
|
||||||
config_update_topic.clone(),
|
|
||||||
)?);
|
)?);
|
||||||
spawner.spawn(mqtt_runner(task)?);
|
spawner.spawn(mqtt_runner(task)?);
|
||||||
|
|
||||||
@@ -227,7 +218,7 @@ pub async fn mqtt_init(
|
|||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
async fn mqtt_runner(
|
async fn mqtt_runner(
|
||||||
task: McutieTask<'static, String, PublishDisplay<'static, String, &'static str>, 4>,
|
task: McutieTask<'static, String, PublishDisplay<'static, String, &'static str>, 2>,
|
||||||
) {
|
) {
|
||||||
task.run().await;
|
task.run().await;
|
||||||
}
|
}
|
||||||
@@ -237,8 +228,6 @@ async fn mqtt_incoming_task(
|
|||||||
receiver: McutieReceiver,
|
receiver: McutieReceiver,
|
||||||
round_trip_topic: String,
|
round_trip_topic: String,
|
||||||
stay_alive_topic: String,
|
stay_alive_topic: String,
|
||||||
config_update_payload_topic: String,
|
|
||||||
config_update_topic: String,
|
|
||||||
) {
|
) {
|
||||||
loop {
|
loop {
|
||||||
let message = receiver.receive().await;
|
let message = receiver.receive().await;
|
||||||
@@ -264,43 +253,6 @@ async fn mqtt_incoming_task(
|
|||||||
};
|
};
|
||||||
log(LogMessage::MqttStayAliveRec, a, 0, "", "");
|
log(LogMessage::MqttStayAliveRec, a, 0, "", "");
|
||||||
MQTT_STAY_ALIVE.store(value, Ordering::Relaxed);
|
MQTT_STAY_ALIVE.store(value, Ordering::Relaxed);
|
||||||
} else if subtopic.eq(config_update_payload_topic.as_str()) {
|
|
||||||
let payload_str = String::from_utf8_lossy(&payload[..]).to_string();
|
|
||||||
let mut buffer = MQTT_CONFIG_UPDATE_PAYLOAD.lock().await;
|
|
||||||
*buffer = Some(payload_str);
|
|
||||||
info!("MQTT config update payload received");
|
|
||||||
} else if subtopic.eq(config_update_topic.as_str()) {
|
|
||||||
let update_requested = payload.eq_ignore_ascii_case("true".as_ref())
|
|
||||||
|| payload.eq_ignore_ascii_case("1".as_ref());
|
|
||||||
if update_requested {
|
|
||||||
info!("MQTT config update requested");
|
|
||||||
let payload_lock = MQTT_CONFIG_UPDATE_PAYLOAD.lock().await;
|
|
||||||
if let Some(payload_str) = payload_lock.as_ref() {
|
|
||||||
match serde_json::from_str::<crate::config::PlantControllerConfig>(payload_str) {
|
|
||||||
Ok(config) => {
|
|
||||||
info!("Deserialized config, applying...");
|
|
||||||
let board_mutex = crate::BOARD_ACCESS.get().await;
|
|
||||||
let mut board = board_mutex.lock().await;
|
|
||||||
if let Err(e) = board.board_hal.get_esp().save_config(payload_str.as_bytes().to_vec()).await {
|
|
||||||
info!("Error saving config to flash: {}", e);
|
|
||||||
let _ = publish("/config/update", "false").await;
|
|
||||||
} else {
|
|
||||||
board.board_hal.set_config(config);
|
|
||||||
info!("Config applied, rebooting");
|
|
||||||
let _ = publish("/config/update", "false").await;
|
|
||||||
board.board_hal.get_esp().deep_sleep_ms(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
info!("Error deserializing config: {}", e);
|
|
||||||
let _ = publish("/config/update", "false").await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
info!("No config update payload available");
|
|
||||||
let _ = publish("/config/update", "false").await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
log(LogMessage::UnknownTopic, 0, 0, "", &topic);
|
log(LogMessage::UnknownTopic, 0, 0, "", &topic);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user