ESPHome  2024.10.2
internal_temperature.cpp
Go to the documentation of this file.
1 #include "internal_temperature.h"
2 #include "esphome/core/log.h"
3 
4 #ifdef USE_ESP32
5 #if defined(USE_ESP32_VARIANT_ESP32)
6 // there is no official API available on the original ESP32
7 extern "C" {
8 uint8_t temprature_sens_read();
9 }
10 #elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
11 #include "driver/temp_sensor.h"
12 #endif // USE_ESP32_VARIANT
13 #endif // USE_ESP32
14 #ifdef USE_RP2040
15 #include "Arduino.h"
16 #endif // USE_RP2040
17 #ifdef USE_BK72XX
18 extern "C" {
19 uint32_t temp_single_get_current_temperature(uint32_t *temp_value);
20 }
21 #endif // USE_BK72XX
22 
23 namespace esphome {
24 namespace internal_temperature {
25 
26 static const char *const TAG = "internal_temperature";
27 
29  float temperature = NAN;
30  bool success = false;
31 #ifdef USE_ESP32
32 #if defined(USE_ESP32_VARIANT_ESP32)
33  uint8_t raw = temprature_sens_read();
34  ESP_LOGV(TAG, "Raw temperature value: %d", raw);
35  temperature = (raw - 32) / 1.8f;
36  success = (raw != 128);
37 #elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
38  temp_sensor_config_t tsens = TSENS_CONFIG_DEFAULT();
39  temp_sensor_set_config(tsens);
40  temp_sensor_start();
41 #if defined(USE_ESP32_VARIANT_ESP32S3) && (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 3))
42 #error \
43  "ESP32-S3 internal temperature sensor requires ESP IDF V4.4.3 or higher. See https://github.com/esphome/issues/issues/4271"
44 #endif
45  esp_err_t result = temp_sensor_read_celsius(&temperature);
46  temp_sensor_stop();
47  success = (result == ESP_OK);
48 #endif // USE_ESP32_VARIANT
49 #endif // USE_ESP32
50 #ifdef USE_RP2040
51  temperature = analogReadTemp();
52  success = (temperature != 0.0f);
53 #endif // USE_RP2040
54 #ifdef USE_BK72XX
55  uint32_t raw, result;
57  success = (result == 0);
58 #if defined(USE_LIBRETINY_VARIANT_BK7231N)
59  temperature = raw * -0.38f + 156.0f;
60 #elif defined(USE_LIBRETINY_VARIANT_BK7231T)
61  temperature = raw * 0.04f;
62 #else // USE_LIBRETINY_VARIANT
63  temperature = raw * 0.128f;
64 #endif // USE_LIBRETINY_VARIANT
65 #endif // USE_BK72XX
66  if (success && std::isfinite(temperature)) {
67  this->publish_state(temperature);
68  } else {
69  ESP_LOGD(TAG, "Ignoring invalid temperature (success=%d, value=%.1f)", success, temperature);
70  if (!this->has_state()) {
71  this->publish_state(NAN);
72  }
73  }
74 }
75 
76 void InternalTemperatureSensor::dump_config() { LOG_SENSOR("", "Internal Temperature Sensor", this); }
77 
78 } // namespace internal_temperature
79 } // namespace esphome
uint8_t raw[35]
Definition: bl0939.h:19
uint8_t temprature_sens_read()
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
uint16_t temperature
Definition: sun_gtil2.cpp:26
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet...
Definition: sensor.cpp:97
uint32_t temp_single_get_current_temperature(uint32_t *temp_value)