From f5c27c673138fec7addde081bbc638074f803b11 Mon Sep 17 00:00:00 2001 From: david <david.dasilvam2@hes-so.ch> Date: Fri, 27 Oct 2023 00:42:11 +0200 Subject: [PATCH] final final --- src/main.rs | 74 +++++++++++++++++++---------------------- src/mpl115_a2.rs | 3 +- src/rn_131rg.rs | 86 ++++++++++++++++++++++++++---------------------- 3 files changed, 81 insertions(+), 82 deletions(-) diff --git a/src/main.rs b/src/main.rs index f88f451..fc06a65 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,7 @@ struct Data { #[entry] fn main() -> ! { // Clean terminal and acknowledge the boot + hprint! {"{}[2J", 27 as char}; hprintln! {"Nucleo-l476RG is alive !"}; @@ -144,53 +145,46 @@ fn main() -> ! { }; loop { - rn_131rg::wait_http_instant(&mut rn_con); + // wait on a request, and return the first letter, + // usefull to know if it is POST or GET + let c = rn_131rg::wait_http(&mut rn_con); - // Get Data + // get data from sensors 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!( - r##"HTTP/1.1 200 OK -Content-Length: 5138 + if c == b'P' { + // if POST, send only the sensor + rn_131rg::send_fmt( + &mut rn_con, + format_args!( + "HTTP/1.1 200 OK +Content-Length: 17 +Content-Type: text/html +Connection: close + +{:02.3}|{:02.3}|{:03} + + +", data.pression, data.temperature, data.luminosity + ), + ) + } else { + // if GET, we send full html page + rn_131rg::send_fmt( + &mut rn_con, + format_args!( + r##"HTTP/1.1 200 OK +Content-Length: 5406 Content-Type: text/html Connection: close -<!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 "## - ), - ); - - 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 "## - ), - ); - - 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> +<!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 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 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 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 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: <span id="p">...</span> [kPa]</p><p>Température: <span id="t">...</span> [°C] (±5 °C)</p><p>Luminosité: <span id="l">...</span>/125</p></div></body><script>setInterval(async () => {{let r = await fetch( "192.168.120.103" ,{{method: "POST"}});let d = (await r.text()).split("|");document.querySelector("#p").textContent = d[0];document.querySelector("#t").textContent = d[1];document.querySelector("#l").textContent = d[2];}},3000);</script></html> - "##, - data.pression, data.temperature, data.luminosity - ), - ); +"## + ), + ); + } } } diff --git a/src/mpl115_a2.rs b/src/mpl115_a2.rs index db59020..328bd50 100644 --- a/src/mpl115_a2.rs +++ b/src/mpl115_a2.rs @@ -67,7 +67,7 @@ pub fn get_data( .unwrap(); let coeffs = coeff(buffer_8); - asm::delay(1_000_000); + // asm::delay(1_000); // Start Pressure and Temperature Conversion i2c.write(MPL115A2_ADDR, &[0x12, 0x00]).unwrap(); @@ -78,7 +78,6 @@ pub fn get_data( i2c.write_read(MPL115A2_ADDR, &[0x00], &mut buffer_4) .unwrap(); - asm::delay(1_000_000); // Calculate Pressure Compensation let pcomp = pcomp(buffer_4, coeffs); diff --git a/src/rn_131rg.rs b/src/rn_131rg.rs index 953ebf9..4c444c2 100644 --- a/src/rn_131rg.rs +++ b/src/rn_131rg.rs @@ -4,15 +4,17 @@ use stm32l4xx_hal::prelude::*; use stm32l4xx_hal::rcc::*; use stm32l4xx_hal::serial::*; use stm32l4xx_hal::stm32::*; -// use cortex_m_semihosting::hprint; -// use core::str::from_utf8; + use core::fmt::Write; -use cortex_m::asm; + pub struct RnCon { pub tx: Tx<stm32l4xx_hal::pac::UART4>, pub rx: Rx<stm32l4xx_hal::pac::UART4>, } + +/// Helper function to get a struct used by other function +/// to communicate over UART with the wifi (RN-131g) pub fn get_connexion( tx: Pin<Alternate<PushPull, 8>, L8, 'A', 0>, rx: Pin<Alternate<PushPull, 8>, L8, 'A', 1>, @@ -20,8 +22,8 @@ pub fn get_connexion( apb1r1: &mut APB1R1, clocks: Clocks, ) -> RnCon { - // create connexion struct + // create connexion struct let (uart_tx, uart_rx) = Serial::uart4( uart4, (tx, rx), @@ -39,17 +41,26 @@ pub fn get_connexion( const BUFFER_SIZE: usize = 3; let mut buffer: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE]; + // start command mode send(&mut uart, "$$$"); - // check communication between microcontroller and wifi module, and start command mode + // check communication between microcontroller and wifi module + // (should receive <CMD> and only that) wait_serial(&mut uart, &mut buffer, BUFFER_SIZE); assert_eq!(buffer, "CMD".as_bytes()); - // necessary to avoid "*HELLO*" string at connexion start + // necessary to avoid "*HELLO*" and "*OPEN*" string at connexion start + send(&mut uart, "set comm open 0\r\n"); send(&mut uart, "set comm remote 0\r\n"); + send(&mut uart, "set comm match 1\r\n"); + + + // in case of probleme auto kill tcp connexion to avoid to stay stuck + send(&mut uart, "set comm idle 2\r\n"); - // create access point - send(&mut uart, "set ip localport 80\r\n"); + // set port to avoid having to set it manually in the browser + send(&mut uart, "set ip localport 80\r\n"); + // join wifi send(&mut uart, "join RustWeatherStation\r\n"); // stop command mode, making connexion available for data transfer @@ -58,21 +69,27 @@ pub fn get_connexion( return uart; } + +/// Send plain str over the uart connexion to wifi module pub fn send(con: &mut RnCon, s: &str) { con.tx.write_str(s).unwrap(); } +/// Send str over the uart connexion to wifi module +/// This version allow for formatting through format_args! macro +/// # Examples +/// ``` +/// format_args!( +/// "My name is {}", "David" +/// ), +/// ``` pub fn send_fmt(con: &mut RnCon, args: Arguments<'_>) { con.tx.write_fmt(args).unwrap(); } -// pub fn print(buffer: &[u8]) { -// hprint!("{}", from_utf8(buffer).unwrap()); -// } - -/// read on the uart connexion -/// +/// Read on the uart connexion continuously, +/// stopping on CRLF or buffer filed pub fn wait_serial(con: &mut RnCon, buffer: &mut [u8], buffer_size: usize) { let mut i = 0; // char index @@ -96,38 +113,27 @@ pub fn wait_serial(con: &mut RnCon, buffer: &mut [u8], buffer_size: usize) { } } -pub fn wait_http_instant(con: &mut RnCon) { +/// Read the uart and wait until 'G' or 'P' is received +/// then return if it's 'G' or 'P' that was received +/// Knowing that will allow us to send only the data or +/// the full page based on browser request +pub fn wait_http(con: &mut RnCon) -> u8{ + let c; loop { match con.rx.read() { Ok(x) => { - if x == b'G' { + if x == b'G'{ + c = b'G'; + break; + } + + if x == b'P' { + c = b'P'; break; } } Err(_x) => {} } } -} - -// 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) => {} -// } -// } -// } + return c; +} \ No newline at end of file -- GitLab