-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttps_client.rs
More file actions
79 lines (67 loc) · 2.54 KB
/
Copy pathhttps_client.rs
File metadata and controls
79 lines (67 loc) · 2.54 KB
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
//! Example on how to connect to wifi as a client and then using a HttpClient to perform an
//! HTTP GET request to the website http://ifconfig.net/ and then read the answer that
//! should contain the ip address of the device.
//! Note: Change SSID & PASSWORD values before running the example.
use esp_idf_svc::hal::task::block_on;
use esp_idf_svc::{
eventloop::EspSystemEventLoop,
hal::{delay::FreeRtos, prelude::Peripherals},
http::{
client::{Configuration, EspHttpConnection},
Method,
},
nvs::EspDefaultNvsPartition,
timer::EspTaskTimerService,
wifi::{
AsyncWifi, AuthMethod, ClientConfiguration, Configuration as WifiConfiguration, EspWifi,
},
};
const SSID: &str = "Iphone 8 Diego New";
const PASSWORD: &str = "diegocivini";
const URI: &str = "https://dog.ceo/api/breeds/image/random";
fn main() {
esp_idf_svc::sys::link_patches();
// WIFI connection
let peripherals = Peripherals::take().unwrap();
let sys_loop = EspSystemEventLoop::take().unwrap();
let nvs = EspDefaultNvsPartition::take().unwrap();
let timer_service = EspTaskTimerService::new().unwrap();
let mut wifi = AsyncWifi::wrap(
EspWifi::new(peripherals.modem, sys_loop.clone(), Some(nvs)).unwrap(),
sys_loop,
timer_service,
)
.unwrap();
let wifi_configuration: WifiConfiguration = WifiConfiguration::Client(ClientConfiguration {
ssid: SSID.try_into().unwrap(),
bssid: None,
auth_method: AuthMethod::WPAWPA2Personal,
password: PASSWORD.try_into().unwrap(),
channel: None,
..Default::default()
});
wifi.set_configuration(&wifi_configuration).unwrap();
block_on(async {
wifi.start().await.unwrap();
wifi.connect().await.unwrap();
wifi.wait_netif_up().await.unwrap();
});
// HTTP
let mut buf = [0u8; 1024];
let config: &Configuration = &Configuration {
use_global_ca_store: true,
crt_bundle_attach: Some(esp_idf_svc::sys::esp_crt_bundle_attach),
..Default::default()
};
let mut client = EspHttpConnection::new(config).unwrap();
let headers = [("accept", "text/plain")];
client.initiate_request(Method::Get, URI, &headers).unwrap();
client.initiate_response().unwrap();
let bytes_read = client.read(&mut buf).unwrap();
match std::str::from_utf8(&buf[0..bytes_read]) {
Ok(res) => println!("The answer was: {:?}", res),
Err(_) => println!("Error in parse"),
};
println!("End of example");
FreeRtos::delay_ms(u32::MAX);
}