diff --git a/Cargo.lock b/Cargo.lock index 401f1f5e4b67222f0ba5a3764633c36b5227c326..571e3fae037cd8a44d875e4a087bf0bab125763d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -229,6 +229,7 @@ dependencies = [ "cortex-m-rt", "cortex-m-semihosting", "heapless", + "nb 1.1.0", "panic-halt", "stm32l4xx-hal", "usb-device", diff --git a/Cargo.toml b/Cargo.toml index fb2d40099f9916a738a17421ec5b84ce894d23dc..98ab6af011ea32c18f2ec5e3cfe75dc7f65c1bf4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ version = "0.1.0" cortex-m = "0.7.7" cortex-m-rt = "0.7.3" cortex-m-semihosting = "0.5.0" +nb = "1.1.0" panic-halt = "0.2.0" [dependencies.stm32l4xx-hal] diff --git a/src/main.rs b/src/main.rs index 47862b097abe2db1b0ec112561fd99a637574789..f88f45196f8fb0821b4f07f3ae52470e4cfc8c36 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,12 +13,10 @@ mod rn_131rg; use stm32l4xx_hal::{gpio::Speed, prelude::*}; -// use stm32l4xx_hal::delay::Delay; - struct Data { pression: f32, temperature: f32, - luminosity: u8, + luminosity: i32, } #[entry] @@ -30,7 +28,6 @@ fn main() -> ! { // **** Setup Peripherals **** let periphs = stm32l4xx_hal::stm32::Peripherals::take().unwrap(); - // let c_periphs = cortex_m::Peripherals::take().unwrap(); let mut flash = periphs.FLASH.constrain(); let mut rcc = periphs.RCC.constrain(); let mut pwr = periphs.PWR.constrain(&mut rcc.apb1r1); @@ -39,8 +36,6 @@ fn main() -> ! { let mut gpioa = periphs.GPIOA.split(&mut rcc.ahb2); let clocks = rcc.cfgr.freeze(&mut flash.acr, &mut pwr); - // let mut timer = Delay::new(c_periphs.SYST, clocks); - /* **** Setup pb200_286 (luminosity) **** @@ -148,52 +143,54 @@ fn main() -> ! { luminosity: 0, }; - const BUFFER_SIZE: usize = 1024; - let mut buffer: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE]; - loop { - rn_131rg::wait_http(&mut rn_con, &mut buffer, BUFFER_SIZE); + rn_131rg::wait_http_instant(&mut rn_con); // Get Data mpl115_a2::get_data(&mut mp_con, &mut data.pression, &mut data.temperature); pb200_286::get_data(&mut pb200_286_conn, &mut pb_cs, &mut data.luminosity); - + rn_131rg::send_fmt( &mut rn_con, format_args!( - "HTTP/1.1 200 OK -Content-Length: 286 + r##"HTTP/1.1 200 OK +Content-Length: 5138 Content-Type: text/html Connection: close -<!DOCTYPE html> -<html lang='en'> -<head> - <meta charset='UTF-8'> - <meta name='viewport' content='width=device-width, initial-scale=1.0'> - <title>Document</title> -</head> -<body> -<h1>Ayo</h1> -<p>Pression: {:.3}</p> -<p>Température: {:.3}</p> -<p>Luminosité: {:0>3}</p> -</body> -</html> - -", data.pression, data.temperature, data.luminosity +<!DOCTYPE html><html><head> <meta charset='UTF-8'> <link rel='icon' href="data:image/svg+xml,%3Csvg version='1.1' viewBox='0 0 512 512' xmlns='http://www.w3.org/2000/svg'%3E%3Cg%3E%3Cpath d='m148.3 484.5c-29.29-5.395-53.49-24.09-65.85-50.87-19.85-43-1.512-95.05 41.01-116.4 12.68-6.37 21.61-8.488 38.01-9.006l15.01-0.4743 3.401-4.934c12.22-17.73 33.87-33.98 55.22-41.46 9.268-3.244 25.23-6.38 32.47-6.38 5.418 0 5.69-0.1398 12.81-6.592 20.26-18.35 42.9-29.81 69.31-35.09 13.25-2.647 38.47-2.374 52.23 0.5663 54.37 11.62 95.49 52.74 107.2 107.2 3.206 14.95 3.206 39.97 0 54.91-11.61 54.11-52.07 94.78-106.6 107.2l-10 2.271-118.5 0.2286c-94.57 0.1824-119.9-0.0377-125.7-1.09z' fill='%23fff'/%3E%3Cg fill='%23fdff09'%3E%3Cpath d='m213.1 104.2c-1.886-0.9616-4.361-3.018-5.5-4.57-1.969-2.682-2.085-4.29-2.348-32.5-0.3134-33.61-0.1357-34.55 7.381-38.97 6.756-3.973 14.51-1.95 19.83 5.174 1.953 2.614 2.077 4.277 2.354 31.45 0.2887 28.35 0.2644 28.74-2.025 33.07-3.943 7.457-12.26 10.13-19.7 6.341z'/%3E%3Cpath d='m321.3 "## + ), + ); + + rn_131rg::send_fmt( + &mut rn_con, + format_args!( + r##"149c-5.59-3.409-8.064-9.127-6.835-15.8 0.5846-3.175 3.828-6.899 21.36-24.53 21.91-22.03 24.23-23.68 31.47-22.32 9.303 1.745 14.71 13.02 10.27 21.4-1.719 3.239-39.13 40.51-42.25 42.09-3.439 1.742-10.46 1.317-14.01-0.8495z'/%3E%3Cpath d='m101.4 148.1c-2.145-1.587-11.84-10.92-21.55-20.75-19.72-19.95-21.31-22.52-18.93-30.49 1.567-5.232 4.728-8.392 9.959-9.959 7.963-2.386 10.53-0.7899 30.49 18.93 22.15 21.89 23.64 23.81 23.63 30.55-0.0116 8.947-5.646 14.58-14.59 14.59-4.06 5e-3 -5.895-0.5818-9-2.878z'/%3E%3Cpath d='m9.45 259.6c-8.845-3.853-11.96-14.86-6.404-22.66 3.984-5.595 6.105-5.934 37.1-5.933 25.01 1e-3 28.75 0.2073 31.78 1.75 4.777 2.436 7.278 6.273 7.772 11.92 0.5571 6.371-2 11.03-7.714 14.05-4.181 2.211-5.08 2.274-31.89 2.236-20.67-0.0293-28.36-0.3723-30.64-1.367z'/%3E%3Cpath d='m112.2 281.8c-5.438-16.58-7.153-38.4-4.305-54.8 6.166-35.49 29.23-66.43 61.78-82.86 25.26-12.75 58.85-14.97 86.15-5.681 16.85 5.731 31.76 15.03 44.12 27.53 6.903 6.977 18 21.61 18 23.74 0 0.4964-2.588 1.957-5.75 3.245-15.24 "## + ), + ); + + rn_131rg::send_fmt( + &mut rn_con, + format_args!( + r##"6.209-32.68 16.61-44.86 26.77-6.046 5.041-6.224 5.111-18.33 7.167-30.49 5.179-56.95 19.01-79.06 41.34-9.394 9.485-9.779 9.75-14.2 9.75-9.371 0-27.6 4.308-39.9 9.43-1.26 0.5246-2.027-0.6569-3.659-5.633z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E%0A""> <title>Rust Weather Station</title></head><style>body{{background: radial-gradient(100% 200% at 50% 150%, rgb(255, 255, 255) 0%, rgb(0, 212, 255) 100%); height: 100vh; display: flex; flex-flow: column; justify-content: center; align-items: center; font-family: monospace; font-size: 1rem; margin: 0;}}div{{background: white; max-width: 400px; width: 100%; padding: 1rem; border-radius: 0.3rem; box-shadow: 10px 10px 0 black; border: 1px solid black;}}svg{{width: 200px; filter: drop-shadow(10px 10px 0);}}</style><body> <svg version=" 1.1" viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg"> <g> <path d="m148.3 484.5c-29.29-5.395-53.49-24.09-65.85-50.87-19.85-43-1.512-95.05 41.01-116.4 12.68-6.37 21.61-8.488 38.01-9.006l15.01-0.4743 3.401-4.934c12.22-17.73 33.87-33.98 "## ), ); - // Display Data - // hprintln!("{}[2J", 27 as char); - // hprintln!( - // "Pression: {} [kPa]\nTempérature: {} [°C] (±5 °C)\nLuminosité: {} / 125", - // data.pression, - // data.temperature, - // data.luminosity - // ); + rn_131rg::send_fmt( + &mut rn_con, + format_args!( + r##"55.22-41.46 9.268-3.244 25.23-6.38 32.47-6.38 5.418 0 5.69-0.1398 12.81-6.592 20.26-18.35 42.9-29.81 69.31-35.09 13.25-2.647 38.47-2.374 52.23 0.5663 54.37 11.62 95.49 52.74 107.2 107.2 3.206 14.95 3.206 39.97 0 54.91-11.61 54.11-52.07 94.78-106.6 107.2l-10 2.271-118.5 0.2286c-94.57 0.1824-119.9-0.0377-125.7-1.09z" fill="#fff"/> <g fill="#fdff09"> <path d="m213.1 104.2c-1.886-0.9616-4.361-3.018-5.5-4.57-1.969-2.682-2.085-4.29-2.348-32.5-0.3134-33.61-0.1357-34.55 7.381-38.97 6.756-3.973 14.51-1.95 19.83 5.174 1.953 2.614 2.077 4.277 2.354 31.45 0.2887 28.35 0.2644 28.74-2.025 33.07-3.943 7.457-12.26 10.13-19.7 6.341z"/> <path d="m321.3 149c-5.59-3.409-8.064-9.127-6.835-15.8 0.5846-3.175 3.828-6.899 21.36-24.53 21.91-22.03 24.23-23.68 31.47-22.32 9.303 1.745 14.71 13.02 10.27 21.4-1.719 3.239-39.13 40.51-42.25 42.09-3.439 1.742-10.46 1.317-14.01-0.8495z"/> <path d="m101.4 148.1c-2.145-1.587-11.84-10.92-21.55-20.75-19.72-19.95-21.31-22.52-18.93-30.49 1.567-5.232 4.728-8.392 9.959-9.959 7.963-2.386 10.53-0.7899 "## + ), + ); - // Wait + rn_131rg::send_fmt( + &mut rn_con, + format_args!( + r##"30.49 18.93 22.15 21.89 23.64 23.81 23.63 30.55-0.0116 8.947-5.646 14.58-14.59 14.59-4.06 5e-3 -5.895-0.5818-9-2.878z"/> <path d="m9.45 259.6c-8.845-3.853-11.96-14.86-6.404-22.66 3.984-5.595 6.105-5.934 37.1-5.933 25.01 1e-3 28.75 0.2073 31.78 1.75 4.777 2.436 7.278 6.273 7.772 11.92 0.5571 6.371-2 11.03-7.714 14.05-4.181 2.211-5.08 2.274-31.89 2.236-20.67-0.0293-28.36-0.3723-30.64-1.367z"/> <path d="m112.2 281.8c-5.438-16.58-7.153-38.4-4.305-54.8 6.166-35.49 29.23-66.43 61.78-82.86 25.26-12.75 58.85-14.97 86.15-5.681 16.85 5.731 31.76 15.03 44.12 27.53 6.903 6.977 18 21.61 18 23.74 0 0.4964-2.588 1.957-5.75 3.245-15.24 6.209-32.68 16.61-44.86 26.77-6.046 5.041-6.224 5.111-18.33 7.167-30.49 5.179-56.95 19.01-79.06 41.34-9.394 9.485-9.779 9.75-14.2 9.75-9.371 0-27.6 4.308-39.9 9.43-1.26 0.5246-2.027-0.6569-3.659-5.633z"/> </g> </g> </svg> <h1>Rust Weather Station</h1> <div> <p>Pression: {:.3} [kPa]</p><p>Température: {:.3} [°C] (±5 °C)</p><p>Luminosité: {:0>3}/125 %</p></div></body> <script>setTimeout(()=>{{location.reload()}}, 10000); </script></html> + + "##, + data.pression, data.temperature, data.luminosity + ), + ); } } diff --git a/src/pb200_286.rs b/src/pb200_286.rs index c51ebc9054968651662aeb8a60ce42bee9b1c63f..dd6f88cffe292efa48eb0d718ce2e156bc86699c 100644 --- a/src/pb200_286.rs +++ b/src/pb200_286.rs @@ -59,7 +59,7 @@ pub fn get_data( ), >, cs: &mut Pin<Output<PushPull>, L8, 'B', 6>, - luminosity: &mut u8, + luminosity: &mut i32, ) { // Set Chip Select to low to start data exchange cs.set_low(); @@ -76,5 +76,5 @@ pub fn get_data( lux |= buffer[1] >> 4; // Store Data in struct - *luminosity = lux; + *luminosity = lux as i32; } diff --git a/src/rn_131rg.rs b/src/rn_131rg.rs index 6f0d85fd0f70259d0eff84f4e647fea360e5e63f..953ebf9b1faeeab60befb4ba68c11290596224aa 100644 --- a/src/rn_131rg.rs +++ b/src/rn_131rg.rs @@ -1,9 +1,9 @@ +use core::fmt::Arguments; use stm32l4xx_hal::gpio::*; +use stm32l4xx_hal::prelude::*; use stm32l4xx_hal::rcc::*; use stm32l4xx_hal::serial::*; use stm32l4xx_hal::stm32::*; -use stm32l4xx_hal::prelude::*; -use core::fmt::Arguments; // use cortex_m_semihosting::hprint; // use core::str::from_utf8; use core::fmt::Write; @@ -20,37 +20,41 @@ pub fn get_connexion( apb1r1: &mut APB1R1, clocks: Clocks, ) -> RnCon { - // create connexion struct let (uart_tx, uart_rx) = Serial::uart4( uart4, (tx, rx), - 57600.bps(),// 9600.bps(), + 38400.bps(), // 9600.bps(), clocks, apb1r1, ) .split(); - let mut uart = RnCon { tx: uart_tx, rx: uart_rx }; - - - // check communication between microcontroller and wifi module + let mut uart = RnCon { + tx: uart_tx, + rx: uart_rx, + }; const BUFFER_SIZE: usize = 3; let mut buffer: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE]; send(&mut uart, "$$$"); + + // check communication between microcontroller and wifi module, and start command mode wait_serial(&mut uart, &mut buffer, BUFFER_SIZE); assert_eq!(buffer, "CMD".as_bytes()); - send(&mut uart, "set comm remote 0\r\n"); // necessary to avoid "*HELLO*" string at connexion start - asm::delay(1000000); - send(&mut uart, "apmode RustWeatherStation 5\r\n"); - asm::delay(1000000); - send(&mut uart, "exit\r\n"); - // + // necessary to avoid "*HELLO*" string at connexion start + send(&mut uart, "set comm remote 0\r\n"); + // create access point + send(&mut uart, "set ip localport 80\r\n"); + send(&mut uart, "join RustWeatherStation\r\n"); + + // stop command mode, making connexion available for data transfer + send(&mut uart, "exit\r\n"); + return uart; } @@ -59,6 +63,7 @@ pub fn send(con: &mut RnCon, s: &str) { } pub fn send_fmt(con: &mut RnCon, args: Arguments<'_>) { + con.tx.write_fmt(args).unwrap(); } @@ -66,9 +71,8 @@ pub fn send_fmt(con: &mut RnCon, args: Arguments<'_>) { // hprint!("{}", from_utf8(buffer).unwrap()); // } - /// read on the uart connexion -/// +/// pub fn wait_serial(con: &mut RnCon, buffer: &mut [u8], buffer_size: usize) { let mut i = 0; // char index @@ -92,21 +96,11 @@ pub fn wait_serial(con: &mut RnCon, buffer: &mut [u8], buffer_size: usize) { } } -pub fn wait_http(con: &mut RnCon, buffer: &mut [u8], buffer_size: usize) { - let mut i = 0; // char index - +pub fn wait_http_instant(con: &mut RnCon) { loop { match con.rx.read() { Ok(x) => { - buffer[i] = x; - - if i > 3 && buffer[i - 3] == 13 && buffer[i] == 10 { - // end of message, we flush - break; - } - i += 1; - - if i >= buffer_size { + if x == b'G' { break; } } @@ -114,3 +108,26 @@ pub fn wait_http(con: &mut RnCon, buffer: &mut [u8], buffer_size: usize) { } } } + +// pub fn wait_http(con: &mut RnCon, buffer: &mut [u8], buffer_size: usize) { +// let mut i = 0; // char index + +// loop { +// match con.rx.read() { +// Ok(x) => { +// buffer[i] = x; + +// if i > 3 && buffer[i - 3] == 13 && buffer[i] == 10 { +// // end of message, we flush +// break; +// } +// i += 1; + +// if i >= buffer_size { +// break; +// } +// } +// Err(_x) => {} +// } +// } +// }