Skip to content

Commit

Permalink
Replace the hardcoded ip address with a dns lookup (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
MabezDev authored Jul 20, 2023
1 parent d6f074b commit 0fa97b3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ heapless = "0.7.16"
esp-backtrace = { version = "0.7.0", features = ["esp32c3", "panic-handler", "exception-handler", "print-uart"] }
esp-println = { version = "0.5.0", features = ["esp32c3","log"] }
embedded-svc = { version = "0.25.0", default-features = false}
embassy-net = { version = "0.1.0", features = ["nightly", "tcp", "udp", "dhcpv4", "medium-ethernet", "proto-ipv6", "log"] }
embassy-net = { version = "0.1.0", features = ["nightly", "tcp", "udp", "dhcpv4", "medium-ethernet", "proto-ipv6", "log", "dns"] }
embassy-executor = { version = "0.2.0", features = ["nightly", "integrated-timers", "arch-riscv32", "executor-thread"] }
embassy-time = { version = "0.1.1", features = ["nightly"] }
embedded-hal = { version = "0.2.7", features = ["unproven"] }
Expand Down
12 changes: 10 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use embassy_time::{Duration, Timer};

use embassy_executor::_export::StaticCell;
use embassy_net::tcp::TcpSocket;
use embassy_net::{Config, Ipv4Address, Stack, StackResources};
use embassy_net::{Config, Stack, StackResources, dns::DnsQueryType};

use heapless::String;
use core::fmt::Write;
Expand Down Expand Up @@ -124,7 +124,15 @@ async fn task(stack: &'static Stack<WifiDevice<'static>>, i2c: I2C<'static, I2C0

socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));

let remote_endpoint = (Ipv4Address::new(52, 57, 158, 144), 1883);
let address = match stack.dns_query("broker.hivemq.com", DnsQueryType::A).await.map(|a| a[0]) {
Ok(address) => address,
Err(e) => {
println!("DNS lookup error: {e:?}");
continue
}
};

let remote_endpoint = (address, 1883);
println!("connecting...");
let connection = socket.connect(remote_endpoint).await;
if let Err(e) = connection {
Expand Down

0 comments on commit 0fa97b3

Please sign in to comment.