5 #if defined(USE_ESP32_VARIANT_ESP32) 10 #elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || \ 11 defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32H2) || \ 12 defined(USE_ESP32_VARIANT_ESP32C2) 13 #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) 14 #include "driver/temp_sensor.h" 16 #include "driver/temperature_sensor.h" 17 #endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) 18 #endif // USE_ESP32_VARIANT 30 namespace internal_temperature {
32 static const char *
const TAG =
"internal_temperature";
34 #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)) && \ 35 (defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S2) || \ 36 defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32H2) || defined(USE_ESP32_VARIANT_ESP32C2)) 37 static temperature_sensor_handle_t tsensNew = NULL;
38 #endif // ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && USE_ESP32_VARIANT 45 #if defined(USE_ESP32_VARIANT_ESP32) 47 ESP_LOGV(TAG,
"Raw temperature value: %d", raw);
48 temperature = (raw - 32) / 1.8f;
49 success = (raw != 128);
50 #elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || \ 51 defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32H2) || \ 52 defined(USE_ESP32_VARIANT_ESP32C2) 53 #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) 54 temp_sensor_config_t tsens = TSENS_CONFIG_DEFAULT();
55 temp_sensor_set_config(tsens);
57 #if defined(USE_ESP32_VARIANT_ESP32S3) && (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 3)) 59 "ESP32-S3 internal temperature sensor requires ESP IDF V4.4.3 or higher. See https://github.com/esphome/issues/issues/4271" 61 esp_err_t result = temp_sensor_read_celsius(&temperature);
63 success = (result == ESP_OK);
65 esp_err_t result = temperature_sensor_get_celsius(tsensNew, &temperature);
66 success = (result == ESP_OK);
68 ESP_LOGE(TAG,
"Failed to get temperature: %d", result);
70 #endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) 71 #endif // USE_ESP32_VARIANT 74 temperature = analogReadTemp();
75 success = (temperature != 0.0f);
80 success = (result == 0);
81 #if defined(USE_LIBRETINY_VARIANT_BK7231N) 82 temperature = raw * -0.38f + 156.0f;
83 #elif defined(USE_LIBRETINY_VARIANT_BK7231T) 84 temperature = raw * 0.04f;
85 #else // USE_LIBRETINY_VARIANT 86 temperature = raw * 0.128f;
87 #endif // USE_LIBRETINY_VARIANT 89 if (success && std::isfinite(temperature)) {
92 ESP_LOGD(TAG,
"Ignoring invalid temperature (success=%d, value=%.1f)", success, temperature);
101 #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)) && \ 102 (defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S2) || \ 103 defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32H2) || defined(USE_ESP32_VARIANT_ESP32C2)) 104 ESP_LOGCONFIG(TAG,
"Setting up temperature sensor...");
106 temperature_sensor_config_t tsens_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80);
108 esp_err_t result = temperature_sensor_install(&tsens_config, &tsensNew);
109 if (result != ESP_OK) {
110 ESP_LOGE(TAG,
"Failed to install temperature sensor: %d", result);
115 result = temperature_sensor_enable(tsensNew);
116 if (result != ESP_OK) {
117 ESP_LOGE(TAG,
"Failed to enable temperature sensor: %d", result);
121 #endif // ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && USE_ESP32_VARIANT
uint8_t temprature_sens_read()
void dump_config() override
void publish_state(float state)
Publish a new state to the front-end.
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet...
uint32_t temp_single_get_current_temperature(uint32_t *temp_value)