MCPcopy Create free account
hub / github.com/ImplFerris/esp32-projects / http_loop

Function http_loop

wifi-webfetch/src/bin/main.rs:171–211  ·  view source on GitHub ↗
(
    mut socket: blocking_network_stack::Socket<'_, '_, esp_radio::wifi::WifiDevice<'_>>,
)

Source from the content-addressed store, hash-verified

169}
170
171fn http_loop(
172 mut socket: blocking_network_stack::Socket<'_, '_, esp_radio::wifi::WifiDevice<'_>>,
173) -> ! {
174 println!("Starting HTTP client loop");
175 let delay = Delay::new();
176 loop {
177 println!("Making HTTP request");
178 socket.work();
179
180 let remote_addr = IpAddress::v4(142, 250, 185, 115);
181 socket.open(remote_addr, 80).unwrap();
182 socket
183 .write(b"GET / HTTP/1.0\r\nHost: www.mobile-j.de\r\n\r\n")
184 .unwrap();
185 socket.flush().unwrap();
186
187 let deadline = Instant::now() + Duration::from_secs(20);
188 let mut buffer = [0u8; 512];
189 while let Ok(len) = socket.read(&mut buffer) {
190 // let text = unsafe { core::str::from_utf8_unchecked(&buffer[..len]) };
191 let Ok(text) = core::str::from_utf8(&buffer[..len]) else {
192 panic!("Invalid UTF-8 sequence encountered");
193 };
194
195 println!("{}", text);
196
197 if Instant::now() > deadline {
198 println!("Timeout");
199 break;
200 }
201 }
202
203 socket.disconnect();
204 let deadline = Instant::now() + Duration::from_secs(5);
205 while Instant::now() < deadline {
206 socket.work();
207 }
208
209 delay.delay_millis(1000);
210 }
211}

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected