Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the hardcoded ip address with a dns lookup #1

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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