-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathdhcp.rs
179 lines (147 loc) · 5.21 KB
/
dhcp.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#![no_std]
#![no_main]
#![feature(c_variadic)]
#![feature(const_mut_refs)]
#[cfg(feature = "esp32")]
use esp32_hal as hal;
#[cfg(feature = "esp32c3")]
use esp32c3_hal as hal;
#[cfg(feature = "esp32s2")]
use esp32s2_hal as hal;
#[cfg(feature = "esp32s3")]
use esp32s3_hal as hal;
use embedded_io::blocking::*;
use embedded_svc::wifi::{
AccessPointInfo, ClientConfiguration, ClientConnectionStatus, ClientIpStatus, ClientStatus,
Configuration, Status, Wifi,
};
use esp_backtrace as _;
use esp_println::logger::init_logger;
use esp_println::{print, println};
use esp_wifi::wifi::utils::create_network_interface;
use esp_wifi::wifi_interface::{timestamp, Network, WifiError};
use esp_wifi::{create_network_stack_storage, network_stack_storage};
use esp_wifi::{current_millis, initialize};
use hal::clock::{ClockControl, CpuClock};
use hal::{pac::Peripherals, prelude::*, Rtc};
use smoltcp::wire::Ipv4Address;
#[cfg(any(feature = "esp32c3"))]
use hal::system::SystemExt;
#[cfg(feature = "esp32c3")]
use riscv_rt::entry;
#[cfg(any(feature = "esp32", feature = "esp32s3", feature = "esp32s2"))]
use xtensa_lx_rt::entry;
const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");
#[entry]
fn main() -> ! {
init_logger(log::LevelFilter::Info);
esp_wifi::init_heap();
let peripherals = Peripherals::take().unwrap();
#[cfg(not(feature = "esp32"))]
let system = peripherals.SYSTEM.split();
#[cfg(feature = "esp32")]
let system = peripherals.DPORT.split();
#[cfg(feature = "esp32c3")]
let clocks = ClockControl::configure(system.clock_control, CpuClock::Clock160MHz).freeze();
#[cfg(any(feature = "esp32", feature = "esp32s3", feature = "esp32s2"))]
let clocks = ClockControl::configure(system.clock_control, CpuClock::Clock240MHz).freeze();
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
// Disable watchdog timers
#[cfg(not(any(feature = "esp32", feature = "esp32s2")))]
rtc.swd.disable();
rtc.rwdt.disable();
let mut storage = create_network_stack_storage!(3, 8, 1);
let ethernet = create_network_interface(network_stack_storage!(storage));
let mut wifi_interface = esp_wifi::wifi_interface::Wifi::new(ethernet);
#[cfg(feature = "esp32c3")]
{
use hal::systimer::SystemTimer;
let syst = SystemTimer::new(peripherals.SYSTIMER);
initialize(syst.alarm0, peripherals.RNG, &clocks).unwrap();
}
#[cfg(any(feature = "esp32", feature = "esp32s3", feature = "esp32s2"))]
{
use hal::timer::TimerGroup;
let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks);
initialize(timg1.timer0, peripherals.RNG, &clocks).unwrap();
}
println!("{:?}", wifi_interface.get_status());
println!("Start Wifi Scan");
let res: Result<(heapless::Vec<AccessPointInfo, 10>, usize), WifiError> =
wifi_interface.scan_n();
if let Ok((res, _count)) = res {
for ap in res {
println!("{:?}", ap);
}
}
println!("Call wifi_connect");
let client_config = Configuration::Client(ClientConfiguration {
ssid: SSID.into(),
password: PASSWORD.into(),
..Default::default()
});
let res = wifi_interface.set_configuration(&client_config);
println!("wifi_connect returned {:?}", res);
println!("{:?}", wifi_interface.get_capabilities());
println!("{:?}", wifi_interface.get_status());
// wait to get connected
println!("Wait to get connected");
loop {
if let Status(ClientStatus::Started(_), _) = wifi_interface.get_status() {
break;
}
}
println!("{:?}", wifi_interface.get_status());
// wait for getting an ip address
println!("Wait to get an ip address");
loop {
wifi_interface.poll_dhcp().unwrap();
wifi_interface
.network_interface()
.poll(timestamp())
.unwrap();
if let Status(
ClientStatus::Started(ClientConnectionStatus::Connected(ClientIpStatus::Done(config))),
_,
) = wifi_interface.get_status()
{
println!("got ip {:?}", config);
break;
}
}
println!("Start busy loop on main");
let mut network = Network::new(wifi_interface, current_millis);
let mut socket = network.get_socket();
loop {
println!("Making HTTP request");
socket.work();
socket
.open(Ipv4Address::new(142, 250, 185, 115), 80)
.unwrap();
socket
.write(b"GET / HTTP/1.0\r\nHost: www.mobile-j.de\r\n\r\n")
.unwrap();
socket.flush().unwrap();
let wait_end = current_millis() + 2 * 1000;
loop {
let mut buffer = [0u8; 512];
if let Ok(len) = socket.read(&mut buffer) {
let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..len]) };
print!("{}", to_print);
} else {
break;
}
if current_millis() > wait_end {
println!("Timeout");
break;
}
}
println!();
socket.disconnect();
let wait_end = current_millis() + 5 * 1000;
while current_millis() < wait_end {
socket.work();
}
}
}