From 25da1ac04bd8dc4b6e7390a666e4158cd618d597 Mon Sep 17 00:00:00 2001 From: Lutz Lang Date: Fri, 25 Apr 2025 21:59:51 +0200 Subject: [PATCH] feat: add MQTT support and configuration documentation --- MQTT.md | 51 ++++++++++++++++++++++++++++++++++++++++++ client/bin/Cargo.toml | 1 + client/bin/src/main.rs | 4 ++++ 3 files changed, 56 insertions(+) create mode 100644 MQTT.md diff --git a/MQTT.md b/MQTT.md new file mode 100644 index 0000000..1bebd37 --- /dev/null +++ b/MQTT.md @@ -0,0 +1,51 @@ + # MQTT Configuration + + This project can publish weather and public transport data to an MQTT broker. + To enable MQTT, follow these steps: + + ## 1. Install dependencies + Ensure you have Rust and Cargo installed. The MQTT support uses the Paho MQTT client crate. + Run: + ```bash + cargo update + ``` + + ## 2. Set the MQTT_BROKER environment variable + Before running the client, define `MQTT_BROKER` to your broker address. + - Without URI scheme (defaults to TCP): + ```bash + export MQTT_BROKER=localhost:1883 + ``` + - With URI scheme: + ```bash + export MQTT_BROKER=tcp://broker.example.com:1883 + ``` + + ## 3. Run the LED board client + Pass the LED board IP address as the only argument: + ```bash + export MQTT_BROKER=localhost:1883 + cargo run --bin ledboard_client -- 192.168.1.50 + ``` + + ## Topics and Payloads + The client publishes two topics: + + ### weather + JSON payload with fields: + - `dt`: timestamp (Unix seconds) + - `temp`: temperature in °C + - `weather`: object with `main`, `description`, `icon` + - `rain`: rain volume in last 3h (optional) + - `pop`: probability of precipitation + - `wind`: object with `speed`, `deg`, `gust` + + ### straba + JSON payload with fields: + - `outbound_station`: name of outbound station + - `outbound_diff`: seconds until outbound departure + - `inbound_station`: name of inbound station + - `inbound_diff`: seconds until inbound departure + + ## Customization + You can adjust MQTT topics, QoS, and message formats in `client/bin/src/main.rs` under the `publish_to_mqtt` function. \ No newline at end of file diff --git a/client/bin/Cargo.toml b/client/bin/Cargo.toml index 51bbb76..1dbfeaa 100644 --- a/client/bin/Cargo.toml +++ b/client/bin/Cargo.toml @@ -21,4 +21,5 @@ serde = "1.0" serde_derive = "1.0" serde_json = "1.0" # end of web stuff +paho-mqtt = "0.13.2" ping = "0.4.1" diff --git a/client/bin/src/main.rs b/client/bin/src/main.rs index 4a8b134..bb80194 100644 --- a/client/bin/src/main.rs +++ b/client/bin/src/main.rs @@ -414,6 +414,10 @@ fn main() -> ExitCode { if device_online == true { // Render new image send_package(ip.to_string(), &last_data, &straba_res); + // Publish data to MQTT + if let Some(ref client) = mqtt_client { + publish_to_mqtt(client, &last_data, &straba_res); + } } } }