diff options
| author | 1-rafael-1 <[email protected]> | 2025-11-15 20:49:07 +0100 |
|---|---|---|
| committer | 1-rafael-1 <[email protected]> | 2025-11-15 20:49:07 +0100 |
| commit | a01e37fbc2800e87eb3b45f1e61f717e48c74127 (patch) | |
| tree | 757c26e6ccaba85b25c73a6d698d785dfb7d9e9d /examples/rp/src/bin | |
| parent | 933e27d99aaa4869aa601d6ac0f68b6e79788d3e (diff) | |
Switch WiFi example to non-TLS httpbin request
Diffstat (limited to 'examples/rp/src/bin')
| -rw-r--r-- | examples/rp/src/bin/wifi_webrequest.rs | 90 |
1 files changed, 59 insertions, 31 deletions
diff --git a/examples/rp/src/bin/wifi_webrequest.rs b/examples/rp/src/bin/wifi_webrequest.rs index b618d2b38..ce85f4b9a 100644 --- a/examples/rp/src/bin/wifi_webrequest.rs +++ b/examples/rp/src/bin/wifi_webrequest.rs | |||
| @@ -1,9 +1,8 @@ | |||
| 1 | //! This example uses the RP Pico W board Wifi chip (cyw43). | 1 | //! This example uses the RP Pico W board Wifi chip (cyw43). |
| 2 | //! Connects to Wifi network and makes a web request to get the current time. | 2 | //! Connects to Wifi network and makes a web request to httpbin.org. |
| 3 | 3 | ||
| 4 | #![no_std] | 4 | #![no_std] |
| 5 | #![no_main] | 5 | #![no_main] |
| 6 | #![allow(async_fn_in_trait)] | ||
| 7 | 6 | ||
| 8 | use core::str::from_utf8; | 7 | use core::str::from_utf8; |
| 9 | 8 | ||
| @@ -20,11 +19,14 @@ use embassy_rp::gpio::{Level, Output}; | |||
| 20 | use embassy_rp::peripherals::{DMA_CH0, PIO0}; | 19 | use embassy_rp::peripherals::{DMA_CH0, PIO0}; |
| 21 | use embassy_rp::pio::{InterruptHandler, Pio}; | 20 | use embassy_rp::pio::{InterruptHandler, Pio}; |
| 22 | use embassy_time::{Duration, Timer}; | 21 | use embassy_time::{Duration, Timer}; |
| 23 | use reqwless::client::{HttpClient, TlsConfig, TlsVerify}; | 22 | use reqwless::client::HttpClient; |
| 23 | // Uncomment these for TLS requests: | ||
| 24 | // use reqwless::client::{HttpClient, TlsConfig, TlsVerify}; | ||
| 24 | use reqwless::request::Method; | 25 | use reqwless::request::Method; |
| 25 | use serde::Deserialize; | 26 | use serde::Deserialize; |
| 27 | use serde_json_core::from_slice; | ||
| 26 | use static_cell::StaticCell; | 28 | use static_cell::StaticCell; |
| 27 | use {defmt_rtt as _, panic_probe as _, serde_json_core}; | 29 | use {defmt_rtt as _, panic_probe as _}; |
| 28 | 30 | ||
| 29 | bind_interrupts!(struct Irqs { | 31 | bind_interrupts!(struct Irqs { |
| 30 | PIO0_IRQ_0 => InterruptHandler<PIO0>; | 32 | PIO0_IRQ_0 => InterruptHandler<PIO0>; |
| @@ -119,64 +121,90 @@ async fn main(spawner: Spawner) { | |||
| 119 | // And now we can use it! | 121 | // And now we can use it! |
| 120 | 122 | ||
| 121 | loop { | 123 | loop { |
| 122 | let mut rx_buffer = [0; 8192]; | 124 | let mut rx_buffer = [0; 4096]; |
| 123 | let mut tls_read_buffer = [0; 16640]; | 125 | // Uncomment these for TLS requests: |
| 124 | let mut tls_write_buffer = [0; 16640]; | 126 | // let mut tls_read_buffer = [0; 16640]; |
| 127 | // let mut tls_write_buffer = [0; 16640]; | ||
| 125 | 128 | ||
| 126 | let client_state = TcpClientState::<1, 1024, 1024>::new(); | 129 | let client_state = TcpClientState::<1, 4096, 4096>::new(); |
| 127 | let tcp_client = TcpClient::new(stack, &client_state); | 130 | let tcp_client = TcpClient::new(stack, &client_state); |
| 128 | let dns_client = DnsSocket::new(stack); | 131 | let dns_client = DnsSocket::new(stack); |
| 129 | let tls_config = TlsConfig::new(seed, &mut tls_read_buffer, &mut tls_write_buffer, TlsVerify::None); | 132 | // Uncomment these for TLS requests: |
| 133 | // let tls_config = TlsConfig::new(seed, &mut tls_read_buffer, &mut tls_write_buffer, TlsVerify::None); | ||
| 130 | 134 | ||
| 131 | let mut http_client = HttpClient::new_with_tls(&tcp_client, &dns_client, tls_config); | 135 | // Using non-TLS HTTP for this example |
| 132 | let url = "https://worldtimeapi.org/api/timezone/Europe/Berlin"; | 136 | let mut http_client = HttpClient::new(&tcp_client, &dns_client); |
| 133 | // for non-TLS requests, use this instead: | 137 | let url = "http://httpbin.org/json"; |
| 134 | // let mut http_client = HttpClient::new(&tcp_client, &dns_client); | 138 | // For TLS requests, use this instead: |
| 135 | // let url = "http://worldtimeapi.org/api/timezone/Europe/Berlin"; | 139 | // let mut http_client = HttpClient::new_with_tls(&tcp_client, &dns_client, tls_config); |
| 140 | // let url = "https://httpbin.org/json"; | ||
| 136 | 141 | ||
| 137 | info!("connecting to {}", &url); | 142 | info!("connecting to {}", &url); |
| 138 | 143 | ||
| 139 | let mut request = match http_client.request(Method::GET, &url).await { | 144 | let mut request = match http_client.request(Method::GET, url).await { |
| 140 | Ok(req) => req, | 145 | Ok(req) => req, |
| 141 | Err(e) => { | 146 | Err(e) => { |
| 142 | error!("Failed to make HTTP request: {:?}", e); | 147 | error!("Failed to make HTTP request: {:?}", e); |
| 143 | return; // handle the error | 148 | Timer::after(Duration::from_secs(5)).await; |
| 149 | continue; | ||
| 144 | } | 150 | } |
| 145 | }; | 151 | }; |
| 146 | 152 | ||
| 147 | let response = match request.send(&mut rx_buffer).await { | 153 | let response = match request.send(&mut rx_buffer).await { |
| 148 | Ok(resp) => resp, | 154 | Ok(resp) => resp, |
| 149 | Err(_e) => { | 155 | Err(e) => { |
| 150 | error!("Failed to send HTTP request"); | 156 | error!("Failed to send HTTP request: {:?}", e); |
| 151 | return; // handle the error; | 157 | Timer::after(Duration::from_secs(5)).await; |
| 158 | continue; | ||
| 152 | } | 159 | } |
| 153 | }; | 160 | }; |
| 154 | 161 | ||
| 155 | let body = match from_utf8(response.body().read_to_end().await.unwrap()) { | 162 | info!("Response status: {}", response.status.0); |
| 163 | |||
| 164 | let body_bytes = match response.body().read_to_end().await { | ||
| 156 | Ok(b) => b, | 165 | Ok(b) => b, |
| 157 | Err(_e) => { | 166 | Err(_e) => { |
| 158 | error!("Failed to read response body"); | 167 | error!("Failed to read response body"); |
| 159 | return; // handle the error | 168 | Timer::after(Duration::from_secs(5)).await; |
| 169 | continue; | ||
| 170 | } | ||
| 171 | }; | ||
| 172 | |||
| 173 | let body = match from_utf8(body_bytes) { | ||
| 174 | Ok(b) => b, | ||
| 175 | Err(_e) => { | ||
| 176 | error!("Failed to parse response body as UTF-8"); | ||
| 177 | Timer::after(Duration::from_secs(5)).await; | ||
| 178 | continue; | ||
| 160 | } | 179 | } |
| 161 | }; | 180 | }; |
| 162 | info!("Response body: {:?}", &body); | 181 | info!("Response body length: {} bytes", body.len()); |
| 163 | 182 | ||
| 164 | // parse the response body and update the RTC | 183 | // Parse the JSON response from httpbin.org/json |
| 184 | #[derive(Deserialize)] | ||
| 185 | struct SlideShow<'a> { | ||
| 186 | author: &'a str, | ||
| 187 | title: &'a str, | ||
| 188 | } | ||
| 165 | 189 | ||
| 166 | #[derive(Deserialize)] | 190 | #[derive(Deserialize)] |
| 167 | struct ApiResponse<'a> { | 191 | struct HttpBinResponse<'a> { |
| 168 | datetime: &'a str, | 192 | #[serde(borrow)] |
| 169 | // other fields as needed | 193 | slideshow: SlideShow<'a>, |
| 170 | } | 194 | } |
| 171 | 195 | ||
| 172 | let bytes = body.as_bytes(); | 196 | let bytes = body.as_bytes(); |
| 173 | match serde_json_core::de::from_slice::<ApiResponse>(bytes) { | 197 | match from_slice::<HttpBinResponse>(bytes) { |
| 174 | Ok((output, _used)) => { | 198 | Ok((output, _used)) => { |
| 175 | info!("Datetime: {:?}", output.datetime); | 199 | info!("Successfully parsed JSON response!"); |
| 200 | info!("Slideshow title: {:?}", output.slideshow.title); | ||
| 201 | info!("Slideshow author: {:?}", output.slideshow.author); | ||
| 176 | } | 202 | } |
| 177 | Err(_e) => { | 203 | Err(e) => { |
| 178 | error!("Failed to parse response body"); | 204 | error!("Failed to parse JSON response: {}", Debug2Format(&e)); |
| 179 | return; // handle the error | 205 | // Log preview of response for debugging |
| 206 | let preview = if body.len() > 200 { &body[..200] } else { body }; | ||
| 207 | info!("Response preview: {:?}", preview); | ||
| 180 | } | 208 | } |
| 181 | } | 209 | } |
| 182 | 210 | ||
