EspTemperature provides ESP32-C6 internal temperature sensor functionality with comprehensive
monitoring capabilities.
It implements the BaseTemperature interface with hardware-specific optimizations for accurate
temperature measurement,
threshold monitoring, and continuous monitoring.
Features
Internal Temperature Sensor - ESP32-C6 built-in temperature sensor support
Multiple Measurement Ranges - Different accuracy levels for various use cases
Hardware Calibration - Automatic offset compensation and calibration
Threshold Monitoring - Configurable high/low temperature thresholds with callbacks
Continuous Monitoring - Timer-based sampling with configurable intervals
Power Management - Sleep/wake modes for power efficiency
Thread Safety - Mutex-protected operations for multi-threaded access
Comprehensive Diagnostics - Health monitoring and statistics tracking
classEspTemperature:publicBaseTemperature{public:// Constructor with configurationexplicitEspTemperature(consthf_temperature_config_t&config)noexcept;// Destructor with proper cleanup~EspTemperature()noexceptoverride;// BaseTemperature interface implementationboolInitialize()noexceptoverride;boolDeinitialize()noexceptoverride;// Temperature reading operationshf_temperature_err_tReadTemperature(float&temperature_c)noexceptoverride;hf_temperature_err_tReadTemperatureWithRange(float&temperature_c,hf_temperature_range_trange)noexceptoverride;// Threshold monitoringhf_temperature_err_tSetThreshold(hf_temperature_threshold_tthreshold_type,floatthreshold_value_c)noexceptoverride;hf_temperature_err_tSetThresholdCallback(hf_temperature_threshold_callback_tcallback,void*user_data=nullptr)noexceptoverride;// Continuous monitoringhf_temperature_err_tStartContinuousMonitoring(uint32_tinterval_ms)noexceptoverride;hf_temperature_err_tStopContinuousMonitoring()noexceptoverride;boolIsContinuousMonitoringActive()constnoexceptoverride;// Power managementhf_temperature_err_tEnterSleepMode()noexceptoverride;hf_temperature_err_tExitSleepMode()noexceptoverride;// Diagnosticshf_temperature_err_tGetStatistics(hf_temperature_statistics_t&statistics)noexceptoverride;hf_temperature_err_tGetDiagnostics(hf_temperature_diagnostics_t&diagnostics)noexceptoverride;hf_temperature_err_tResetStatistics()noexceptoverride;// Self-test and health monitoringhf_temperature_err_tRunSelfTest()noexceptoverride;boolIsHealthy()constnoexceptoverride;};
Configuration Structure
Temperature Configuration
1
2
3
4
5
6
7
8
9
10
structhf_temperature_config_t{hf_temperature_range_tdefault_range;// Default measurement rangeboolenable_calibration;// Enable hardware calibrationboolenable_continuous_monitoring;// Enable continuous monitoringuint32_tcontinuous_interval_ms;// Continuous monitoring intervalfloathigh_threshold_c;// High temperature thresholdfloatlow_threshold_c;// Low temperature thresholdhf_temperature_threshold_callback_tcallback;// Threshold callback functionvoid*user_data;// User data for callbacks};
#include"mcu/esp32/EspTemperature.h"// Configure temperature sensorhf_temperature_config_tconfig={};config.default_range=hf_temperature_range_t::RANGE_HIGH_ACCURACY;config.enable_calibration=true;// Create and initialize temperature sensorEspTemperaturetemp_sensor(config);if(!temp_sensor.EnsureInitialized()){printf("Failed to initialize temperature sensor\n");return;}// Read current temperaturefloattemperature;hf_temperature_err_tresult=temp_sensor.ReadTemperature(temperature);if(result==hf_temperature_err_t::TEMPERATURE_SUCCESS){printf("Current temperature: %.2fยฐC\n",temperature);}
// Threshold callback functionvoidtemperature_threshold_callback(hf_temperature_threshold_tthreshold_type,floattemperature_c,void*user_data){if(threshold_type==hf_temperature_threshold_t::HIGH_THRESHOLD){printf("High temperature warning: %.2fยฐC\n",temperature_c);}else{printf("Low temperature warning: %.2fยฐC\n",temperature_c);}}// Configure with thresholdshf_temperature_config_tconfig={};config.high_threshold_c=80.0f;// 80ยฐC high thresholdconfig.low_threshold_c=0.0f;// 0ยฐC low thresholdconfig.callback=temperature_threshold_callback;EspTemperaturetemp_sensor(config);temp_sensor.EnsureInitialized();// Set threshold callbacktemp_sensor.SetThresholdCallback(temperature_threshold_callback,nullptr);
// Configure continuous monitoringhf_temperature_config_tconfig={};config.enable_continuous_monitoring=true;config.continuous_interval_ms=1000;// 1 second intervalsEspTemperaturetemp_sensor(config);temp_sensor.EnsureInitialized();// Start continuous monitoringhf_temperature_err_tresult=temp_sensor.StartContinuousMonitoring(1000);if(result==hf_temperature_err_t::TEMPERATURE_SUCCESS){printf("Continuous monitoring started\n");}// Check if monitoring is activeif(temp_sensor.IsContinuousMonitoringActive()){printf("Temperature monitoring is active\n");}// Stop monitoring when donetemp_sensor.StopContinuousMonitoring();
Power Management
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
EspTemperaturetemp_sensor(config);temp_sensor.EnsureInitialized();// Enter sleep mode to save powerhf_temperature_err_tresult=temp_sensor.EnterSleepMode();if(result==hf_temperature_err_t::TEMPERATURE_SUCCESS){printf("Temperature sensor in sleep mode\n");}// Exit sleep mode for measurementstemp_sensor.ExitSleepMode();// Read temperature after waking upfloattemperature;temp_sensor.ReadTemperature(temperature);