Seperated mqtt messages into seperate topics and made topic configurable

This commit is contained in:
Dreaded_X 2023-09-07 00:55:25 +02:00
parent a7a052d4ce
commit 0a7edc50e8
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
3 changed files with 37 additions and 9 deletions

21
Cargo.lock generated
View File

@ -265,6 +265,26 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28c122c3980598d243d63d9a704629a2d748d101f278052ff068be5a4423ab6f" checksum = "28c122c3980598d243d63d9a704629a2d748d101f278052ff068be5a4423ab6f"
[[package]]
name = "const_format"
version = "0.2.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c990efc7a285731f9a4378d81aff2f0e85a2c8781a05ef0f8baa8dac54d0ff48"
dependencies = [
"const_format_proc_macros",
]
[[package]]
name = "const_format_proc_macros"
version = "0.2.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e026b6ce194a874cb9cf32cd5772d1ef9767cc8fcb5765948d74f37a9d8b2bf6"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]] [[package]]
name = "cortex-m" name = "cortex-m"
version = "0.7.7" version = "0.7.7"
@ -1710,6 +1730,7 @@ name = "rp"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"const_format",
"cortex-m", "cortex-m",
"cortex-m-rt", "cortex-m-rt",
"crc16", "crc16",

View File

@ -82,6 +82,7 @@ smoltcp = { version = "0.10.0", default-features = false, features = [
nourl = { version = "0.1.1", features = ["defmt"] } nourl = { version = "0.1.1", features = ["defmt"] }
reqwless = { version = "0.5.0", features = ["defmt"] } reqwless = { version = "0.5.0", features = ["defmt"] }
embedded-storage = "0.3.0" embedded-storage = "0.3.0"
const_format = "0.2.31"
[patch.crates-io] [patch.crates-io]
embassy-executor = { git = "https://github.com/embassy-rs/embassy" } embassy-executor = { git = "https://github.com/embassy-rs/embassy" }

View File

@ -52,6 +52,7 @@ use rust_mqtt::{
use serde::Deserialize; use serde::Deserialize;
use static_cell::make_static; use static_cell::make_static;
use const_format::formatcp;
use defmt::{debug, error, info, warn, Debug2Format, Format}; use defmt::{debug, error, info, warn, Debug2Format, Format};
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
@ -61,6 +62,11 @@ bind_interrupts!(struct Irqs {
PIO0_IRQ_0 => pio::InterruptHandler<PIO0>; PIO0_IRQ_0 => pio::InterruptHandler<PIO0>;
}); });
const ID: &str = env!("ID");
const TOPIC_BASE: &str = formatcp!("pico/{}", ID);
const TOPIC_STATUS: &str = formatcp!("{}/status", TOPIC_BASE);
const TOPIC_UPDATE: &str = formatcp!("{}/update", TOPIC_BASE);
#[derive(Format, Deserialize)] #[derive(Format, Deserialize)]
struct UpdateMessage<'a> { struct UpdateMessage<'a> {
url: &'a str, url: &'a str,
@ -281,9 +287,9 @@ async fn main(spawner: Spawner) {
config.add_username(env!("MQTT_USERNAME")); config.add_username(env!("MQTT_USERNAME"));
config.add_password(env!("MQTT_PASSWORD")); config.add_password(env!("MQTT_PASSWORD"));
config.add_max_subscribe_qos(QualityOfService::QoS1); config.add_max_subscribe_qos(QualityOfService::QoS1);
config.add_client_id("pico"); config.add_client_id(ID);
// Leads to InsufficientBufferSize error // Leads to InsufficientBufferSize error
config.add_will("pico/test", b"disconnected", true); config.add_will(TOPIC_STATUS, b"disconnected", true);
let mut recv_buffer = [0; 1024]; let mut recv_buffer = [0; 1024];
let mut write_buffer = [0; 4096]; let mut write_buffer = [0; 4096];
@ -308,11 +314,11 @@ async fn main(spawner: Spawner) {
updater.mark_booted().unwrap(); updater.mark_booted().unwrap();
client client
.send_message("pico/test", b"connected", QualityOfService::QoS1, true) .send_message(TOPIC_STATUS, b"connected", QualityOfService::QoS1, true)
.await .await
.unwrap(); .unwrap();
client.subscribe_to_topic("pico/test/update").await.unwrap(); client.subscribe_to_topic(TOPIC_UPDATE).await.unwrap();
// Turn LED off when connected // Turn LED off when connected
control.gpio_set(0, false).await; control.gpio_set(0, false).await;
@ -337,7 +343,7 @@ async fn main(spawner: Spawner) {
.expect("The buffer should be large enough to contain all the data"); .expect("The buffer should be large enough to contain all the data");
client client
.send_message("pico/test", &msg, QualityOfService::QoS1, false) .send_message(TOPIC_BASE, &msg, QualityOfService::QoS1, false)
.await .await
.unwrap(); .unwrap();
} }
@ -419,7 +425,7 @@ where
debug!("Preparing updater..."); debug!("Preparing updater...");
client client
.send_message("pico/test", b"preparing", QualityOfService::QoS1, false) .send_message(TOPIC_STATUS, b"preparing", QualityOfService::QoS1, false)
.await .await
.unwrap(); .unwrap();
@ -430,7 +436,7 @@ where
debug!("Updater prepared!"); debug!("Updater prepared!");
client client
.send_message("pico/test", b"prepared", QualityOfService::QoS1, false) .send_message(TOPIC_STATUS, b"prepared", QualityOfService::QoS1, false)
.await .await
.unwrap(); .unwrap();
@ -445,7 +451,7 @@ where
offset += read as u32; offset += read as u32;
} }
client client
.send_message("pico/test", b"written", QualityOfService::QoS1, false) .send_message(TOPIC_STATUS, b"written", QualityOfService::QoS1, false)
.await .await
.unwrap(); .unwrap();
debug!("Total size: {}", offset); debug!("Total size: {}", offset);
@ -453,7 +459,7 @@ where
updater.mark_updated().unwrap(); updater.mark_updated().unwrap();
client client
.send_message("pico/test", b"restarting", QualityOfService::QoS1, false) .send_message(TOPIC_STATUS, b"restarting", QualityOfService::QoS1, false)
.await .await
.unwrap(); .unwrap();