diff --git a/Cargo.lock b/Cargo.lock index 147b72b..a6047d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1103,6 +1103,13 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43794a0ace135be66a25d3ae77d41b91615fb68ae937f904090203e81f755b65" +[[package]] +name = "nourl" +version = "0.1.1" +dependencies = [ + "defmt", +] + [[package]] name = "num-traits" version = "0.2.16" @@ -1416,11 +1423,13 @@ dependencies = [ "embedded-io-async", "heapless", "log", + "nourl", "panic-probe", "rand", "rust-mqtt", "serde", "serde-json-core", + "smoltcp", "static_cell", ] diff --git a/Cargo.toml b/Cargo.toml index 94d85ba..264210d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ embassy-net = { version = "0.1", features = [ "nightly", "medium-ethernet", "defmt", + "dns", ] } embassy-sync = { version = "0.2", features = ["defmt"] } embassy-futures = { version = "0.1", features = ["defmt"] } @@ -70,6 +71,13 @@ rand = { version = "0.8.5", features = [ serde-json-core = "0.5.1" serde = { version = "1.0.188", default-features = false, features = ["derive"] } cfg-if = "1.0.0" +# Embassy harfcodes a max of 6 dns servers, if there are more it crashes. This is a workaround +# Ideally embassy returns an error instead of crashing... +# Interestingly though, I only get 2 DNS servers... +smoltcp = { version = "0.10.0", default-features = false, features = [ + "dns-max-server-count-4", +] } +nourl = { version = "0.1.1", features = ["defmt"] } [patch.crates-io] embassy-executor = { git = "https://github.com/embassy-rs/embassy" } @@ -83,6 +91,7 @@ embassy-futures = { git = "https://github.com/embassy-rs/embassy" } embassy-boot-rp = { git = "https://github.com/embassy-rs/embassy" } rust-mqtt = { path = "../rust-mqtt" } +nourl = { path = "../nourl" } [build-dependencies] dotenvy = "0.15" diff --git a/src/main.rs b/src/main.rs index b0ba646..3e6369d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,40 +2,37 @@ #![no_main] #![feature(type_alias_impl_trait)] -use core::str::FromStr; - -use cyw43::PowerManagementMode; -use cyw43_pio::PioSpi; -use defmt::{debug, error, info, warn, Format}; -use dsmr5::Readout; -use embassy_executor::Spawner; -use embassy_futures::{ - select::{select3, Either3}, - yield_now, -}; -use embassy_net::{tcp::TcpSocket, Config, IpEndpoint, Stack, StackResources}; -use embassy_rp::{ - bind_interrupts, - clocks::RoscRng, - gpio, - peripherals::{DMA_CH0, PIN_23, PIN_25, PIO0, UART0}, - pio::{self, Pio}, - uart::{self, BufferedUartRx, Parity}, -}; -use embassy_time::{Duration, Ticker, Timer}; - -use embedded_io_async::Read; - -use embassy_sync::{ - blocking_mutex::raw::NoopRawMutex, - channel::{Channel, Sender}, -}; -use gpio::{Level, Output}; use heapless::Vec; use rand::{ rngs::{SmallRng, StdRng}, RngCore, SeedableRng, }; + +use cyw43::PowerManagementMode; +use cyw43_pio::PioSpi; +use embassy_executor::Spawner; +use embassy_futures::{ + select::{select3, Either3}, + yield_now, +}; +use embassy_net::{dns::DnsQueryType, tcp::TcpSocket, Config, Stack, StackResources}; +use embassy_rp::{ + bind_interrupts, + clocks::RoscRng, + gpio::{Level, Output}, + peripherals::{DMA_CH0, PIN_23, PIN_25, PIO0, UART0}, + pio::{self, Pio}, + uart::{self, BufferedUartRx, Parity}, +}; +use embassy_sync::{ + blocking_mutex::raw::NoopRawMutex, + channel::{Channel, Sender}, +}; +use embassy_time::{Duration, Ticker, Timer}; +use embedded_io_async::Read; + +use dsmr5::Readout; +use nourl::Url; use rust_mqtt::{ client::{ client::MqttClient, @@ -46,6 +43,8 @@ use rust_mqtt::{ use serde::Deserialize; use static_cell::make_static; +use defmt::{debug, error, info, trace, warn, Format}; + use {defmt_rtt as _, panic_probe as _}; bind_interrupts!(struct Irqs { @@ -217,7 +216,7 @@ async fn main(spawner: Spawner) { let stack = make_static!(Stack::new( net_device, config, - make_static!(StackResources::<2>::new()), + make_static!(StackResources::<6>::new()), rng.next_u64(), )); @@ -245,7 +244,11 @@ async fn main(spawner: Spawner) { let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); // socket.set_timeout(Some(Duration::from_secs(10))); - let addr = IpEndpoint::from_str(env!("MQTT_ADDRESS")).unwrap(); + let url = Url::parse(env!("MQTT_ADDRESS")).unwrap(); + debug!("MQTT URL: {}", url); + let ip = stack.dns_query(url.host(), DnsQueryType::A).await.unwrap()[0]; + let addr = (ip, url.port_or_default()); + debug!("MQTT ADDR: {}", addr); while let Err(e) = socket.connect(addr).await { warn!("Connect error: {:?}", e); @@ -329,7 +332,12 @@ async fn main(spawner: Spawner) { } }; - info!("{}", message); + trace!("UpdateMessage: {}", message); + + let url = Url::parse(message.url).unwrap(); + let ip = stack.dns_query(url.host(), DnsQueryType::A).await; + + debug!("Update IP: {}", ip); } } }