From 473eff74c81b7ca48adb99e249cb7419dec0296f Mon Sep 17 00:00:00 2001 From: JP Stringham Date: Wed, 4 Feb 2026 11:39:14 -0500 Subject: [PATCH] Initial commit --- .gitignore | 5 + Cargo.lock | 284 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 7 ++ src/err.rs | 6 ++ src/main.rs | 102 +++++++++++++++++++ 5 files changed, 404 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/err.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8af2ebc --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/target + +/logs/*.csv + +.DS_Store \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..35abdfa --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,284 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "bumpalo" +version = "3.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510" + +[[package]] +name = "cc" +version = "1.2.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b26a0954ae34af09b50f0de26458fa95369a0d478d8236d3f93082b219bd29" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "chrono" +version = "0.4.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fac4744fb15ae8337dc853fee7fb3f4e48c0fbaa23d0afe49c447b4fab126118" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-link", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "find-msvc-tools" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" + +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "js-sys" +version = "0.3.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c942ebf8e95485ca0d52d97da7c5a2c387d0e7f0ba4c35e93bfcaee045955b3" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.180" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" + +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "syn" +version = "2.0.114" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "talkin_to_pico_w" +version = "0.1.0" +dependencies = [ + "chrono", +] + +[[package]] +name = "unicode-ident" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" + +[[package]] +name = "wasm-bindgen" +version = "0.2.108" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64024a30ec1e37399cf85a7ffefebdb72205ca1c972291c51512360d90bd8566" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.108" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "008b239d9c740232e71bd39e8ef6429d27097518b6b30bdf9086833bd5b6d608" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.108" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5256bae2d58f54820e6490f9839c49780dff84c65aeab9e772f15d5f0e913a55" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.108" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f01b580c9ac74c8d8f0c0e4afb04eeef2acf145458e52c03845ee9cd23e3d12" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..56127c2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "talkin_to_pico_w" +version = "0.1.0" +edition = "2024" + +[dependencies] +chrono = "0.4.43" diff --git a/src/err.rs b/src/err.rs new file mode 100644 index 0000000..a612c3d --- /dev/null +++ b/src/err.rs @@ -0,0 +1,6 @@ +pub enum RemotePicoError { + ConnectionRefused, + StreamWriteError, + StreamReadError, + CorruptReadError, +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..0f7ff0c --- /dev/null +++ b/src/main.rs @@ -0,0 +1,102 @@ +mod err; + +use std::{ + fs, + io::{Read, Write}, + net::{SocketAddr, TcpStream}, + time::{self, Duration, Instant}, +}; + +use chrono::Utc; + +fn main() { + let cur = Utc::now().format("%s"); + let mut log_file = fs::File::create(format!("logs/log_{}.csv", cur)).unwrap(); + writeln!(log_file, "Unix TS, Temp, Read, Hi, Lo").unwrap(); + let sleep_time = time::Duration::from_millis(100); + + loop { + let last_check = Instant::now(); + while last_check.elapsed().as_secs() < 5 { + std::thread::sleep(sleep_time); + } + + write!(log_file, "{}, ", Utc::now().format("%s")).unwrap(); + match get_pico_update() { + Ok(vals) => { + let vals_str: Vec = vals.iter().map(|v| format!("{v}")).collect(); + + writeln!(log_file, "{}", vals_str.join(", ")).unwrap(); + } + Err(_) => writeln!(log_file, "E, E, E, E").unwrap(), + } + } +} + +fn get_pico_update() -> Result<[u16; 4], err::RemotePicoError> { + let socket_addr: SocketAddr = "192.168.1.201:1234".parse().unwrap(); + let Ok(mut stream) = TcpStream::connect_timeout(&socket_addr, Duration::from_secs_f32(1.5)) + else { + return Err(err::RemotePicoError::ConnectionRefused); + }; + + match stream.write("Hello Pico!".as_bytes()) { + Err(_) => return Err(err::RemotePicoError::StreamWriteError), + _ => (), + } + + let mut buf = [0u8; 4096]; + match stream.read(&mut buf) { + Err(_) => return Err(err::RemotePicoError::StreamReadError), + _ => (), + } + let buf = buf; + + let mut vals = [0u16; 4]; + + for (i, v) in vals.iter_mut().enumerate() { + let mut u16_buf = [0u8; 2]; + u16_buf.copy_from_slice(&buf[i * 2..i * 2 + 2]); + *v = u16::from_le_bytes(u16_buf); + } + + match buf[vals.len() * 2] { + b'E' => (), + _ => return Err(err::RemotePicoError::CorruptReadError), + } + + println!("Values: {vals:?}"); + let t = vals[0]; + println!("Int Temp: {}°C", convert_to_celsius(t)); + let mx = vals[2]; + println!("3V Ref: {}", mx); + let mn = vals[3]; + println!("GND Ref: {}", mn); + + let mv = convert_to_mv(vals[1], mn, mx); + println!("Read {mv}mV"); + + let v_divider_inv = convert_voltage_div(mv); + println!("{}mV", v_divider_inv); + + return Ok(vals); +} + +fn convert_voltage_div(mv: u32) -> f32 { + let r1 = 22300.; + let r2 = 3300.; + + mv as f32 / (r2 / (r1 + r2)) +} + +fn convert_to_mv(raw_adc_val: u16, adc_min: u16, adc_max: u16) -> u32 { + (raw_adc_val.saturating_sub(adc_min)) as u32 * 3000 / adc_max as u32 +} + +fn convert_to_celsius(raw_temp: u16) -> f32 { + // According to chapter 4.9.5. Temperature Sensor in RP2040 datasheet + let temp = 27.0 - (raw_temp as f32 * 3. / 4096.0 - 0.706) / 0.001721; + let sign = if temp < 0.0 { -1.0 } else { 1.0 }; + let rounded_temp_x10: i16 = ((temp * 10.0) + 0.5 * sign) as i16; + (rounded_temp_x10 as f32) / 10.0 +}