//! Template project for Rust on ESP32-S3 (`no_std`) based on [`esp-hal`](https://github.com/esp-rs/esp-hal)
//!
//! Useful resources:
//! - [The Rust on ESP Book](https://docs.esp-rs.org/book/)
//! - [Embedded Rust (no_std) on Espressif](https://docs.esp-rs.org/no_std-training/)
//! - [Matrix channel](https://matrix.to/#/#esp-rs:matrix.org)
#![no_std]
#![no_main]
use log::info;
use esp_hal::{
//adc::{Adc, AdcConfig, Attenuation},
analog::adc::{Adc, AdcConfig},
//clock::ClockControl, // aux clock
clock::CpuClock // primary clock
delay::Delay,
gpio::IO,
peripherals::Peripherals,
prelude::*,
system::SystemControl,
// main
};
// use esp_backtrace as _; // Required to handle panics
// This creates a default app-descriptor required by the esp-idf bootloader.
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
// esp_bootloader_esp_idf::esp_app_desc!();
// Steinhart-Hart coefficients for a 10k NTC thermistor
const A: f32 = 0.001129148;
const B: f32 = 0.000234125;
const C: f32 = 0.0000000876741;
#[entry]
fn main() -> ! {
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let _peripherals = esp_hal::init(config); // primary peripheral initialization
let delay = Delay::new(); // primary delay initialization
// esp_println::logger::init_logger_from_env();
info!("Hello world!");
//let peripherals = Peripherals::take(); // aux peripheral initialization
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); // Setup and freeze clocks
//let mut delay = Delay::new(&clocks); // aux delay initialization
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let analog_pin = io.pins.gpio4.into_analog(); // Connect your NTC to GPIO4
let mut adc = Adc::new(peripherals.SENS, AdcConfig::new());
loop {
// Read raw ADC value (0–4095)
let raw_adc = adc.read(&analog_pin).unwrap();
// Convert ADC value to resistance of the thermistor
let voltage_ratio = 4095.0 / raw_adc as f32 - 1.0;
let resistance = 10_000.0 * voltage_ratio;
// Apply Steinhart-Hart to get temperature in Kelvin, then Celsius
let ln_r = resistance.ln();
let temp_kelvin = 1.0 / (A + B * ln_r + C * ln_r.powi(3));
let temp_celsius = temp_kelvin - 273.15;
info!("its workin, temp is {}", temp_celsius);
delay.delay_ms(2000u32);
}
}