From 0f6b444eedd0cf20f12c4b0f6cc8fefea3c53ae2 Mon Sep 17 00:00:00 2001 From: Dimitris Date: Fri, 12 Jun 2026 12:25:49 +0200 Subject: [PATCH] .env --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 15 +++++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ba69ffd..47677e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -239,6 +239,12 @@ dependencies = [ "syn", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "dtoa" version = "1.0.11" @@ -2065,6 +2071,7 @@ name = "weather" version = "0.1.0" dependencies = [ "chrono", + "dotenv", "reqwest 0.13.4", "rumqttc", "scraper 0.27.0", diff --git a/Cargo.toml b/Cargo.toml index 46c0672..bdeff47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ serde = { version = "1.0.228", features = ["derive"] } serde_derive = "1.0.228" serde_json = "1.0.149" chrono = "0.4.45" +dotenv = "0.15.0" # Raspberrypi # cross build --release --target aarch64-unknown-linux-gnu diff --git a/src/main.rs b/src/main.rs index 6a1a2cd..50da31e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,8 @@ use rumqttc::{AsyncClient, EventLoop, MqttOptions, QoS}; use scraper::{Html as HtmlScraper, Selector}; use std::time::Duration; use tokio::{task, time}; +use dotenv::dotenv; +use std::env; const METEO_MUNICH_API_URL: &str = "https://www.meteorologie.lmu.de/~quicklooks/aktuelle_messwerte/messwerte_stadt.html"; @@ -15,6 +17,9 @@ const LONGITUDE_MUC: f64 = 11.542922; #[tokio::main(flavor = "current_thread")] async fn main() { + + dotenv().ok(); + let (client, mut eventloop) = create_conn(); match meteo_weather().await { @@ -157,11 +162,17 @@ async fn publish_topic(client: AsyncClient, topic: &str, value: String) { } fn create_conn() -> (AsyncClient, EventLoop) { - let mut mqttoptions = MqttOptions::new("rust-mqqt", "192.168.1.37", 1883); + let mqtt_client = env::var("MQTT_CLIENT"); + let mqtt_broker = env::var("MQTT_BROKER"); + let mqtt_user = env::var("MQTT_USER"); + let mqtt_password = env::var("MQTT_PASSWORD"); + + let mut mqttoptions = MqttOptions::new(mqtt_client.unwrap(), mqtt_broker.unwrap(), 1883); mqttoptions .set_keep_alive(Duration::from_secs(5)) .set_manual_acks(false) - .set_credentials("mqtt", "delta32#") + .set_credentials(mqtt_user.unwrap(), mqtt_password.unwrap()) .set_clean_session(false); AsyncClient::new(mqttoptions, 10) } +