Skip to content

Commit 0fa97b3

Browse files
authored
Replace the hardcoded ip address with a dns lookup (#1)
1 parent d6f074b commit 0fa97b3

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ heapless = "0.7.16"
1515
esp-backtrace = { version = "0.7.0", features = ["esp32c3", "panic-handler", "exception-handler", "print-uart"] }
1616
esp-println = { version = "0.5.0", features = ["esp32c3","log"] }
1717
embedded-svc = { version = "0.25.0", default-features = false}
18-
embassy-net = { version = "0.1.0", features = ["nightly", "tcp", "udp", "dhcpv4", "medium-ethernet", "proto-ipv6", "log"] }
18+
embassy-net = { version = "0.1.0", features = ["nightly", "tcp", "udp", "dhcpv4", "medium-ethernet", "proto-ipv6", "log", "dns"] }
1919
embassy-executor = { version = "0.2.0", features = ["nightly", "integrated-timers", "arch-riscv32", "executor-thread"] }
2020
embassy-time = { version = "0.1.1", features = ["nightly"] }
2121
embedded-hal = { version = "0.2.7", features = ["unproven"] }

src/main.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use embassy_time::{Duration, Timer};
2828

2929
use embassy_executor::_export::StaticCell;
3030
use embassy_net::tcp::TcpSocket;
31-
use embassy_net::{Config, Ipv4Address, Stack, StackResources};
31+
use embassy_net::{Config, Stack, StackResources, dns::DnsQueryType};
3232

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

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

127-
let remote_endpoint = (Ipv4Address::new(52, 57, 158, 144), 1883);
127+
let address = match stack.dns_query("broker.hivemq.com", DnsQueryType::A).await.map(|a| a[0]) {
128+
Ok(address) => address,
129+
Err(e) => {
130+
println!("DNS lookup error: {e:?}");
131+
continue
132+
}
133+
};
134+
135+
let remote_endpoint = (address, 1883);
128136
println!("connecting...");
129137
let connection = socket.connect(remote_endpoint).await;
130138
if let Err(e) = connection {

0 commit comments

Comments
 (0)