From 6ce474053e280182f1f41327e9dd42d055171ee0 Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Tue, 28 May 2024 23:24:08 +0200 Subject: [PATCH] Feature: Show MCU temperature in system info --- lib/CpuTemperature/src/CpuTemperature.cpp | 51 +++++++++++++++++++++++ lib/CpuTemperature/src/CpuTemperature.h | 14 +++++++ src/WebApi_sysstatus.cpp | 4 +- webapp/src/components/HardwareInfo.vue | 4 ++ webapp/src/locales/de.json | 2 + webapp/src/locales/en.json | 2 + webapp/src/locales/fr.json | 2 + webapp/src/types/SystemStatus.ts | 1 + 8 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 lib/CpuTemperature/src/CpuTemperature.cpp create mode 100644 lib/CpuTemperature/src/CpuTemperature.h diff --git a/lib/CpuTemperature/src/CpuTemperature.cpp b/lib/CpuTemperature/src/CpuTemperature.cpp new file mode 100644 index 000000000..60e3fc7b4 --- /dev/null +++ b/lib/CpuTemperature/src/CpuTemperature.cpp @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2024 Thomas Basler and others + */ + +#include "CpuTemperature.h" +#include + +#if defined(CONFIG_IDF_TARGET_ESP32) +// there is no official API available on the original ESP32 +extern "C" { +uint8_t temprature_sens_read(); +} +#elif defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3) +#include "driver/temp_sensor.h" +#endif + +CpuTemperatureClass CpuTemperature; + +float CpuTemperatureClass::read() +{ + std::lock_guard lock(_mutex); + + float temperature = NAN; + bool success = false; + +#if defined(CONFIG_IDF_TARGET_ESP32) + uint8_t raw = temprature_sens_read(); + ESP_LOGV(TAG, "Raw temperature value: %d", raw); + temperature = (raw - 32) / 1.8f; + success = (raw != 128); +#elif defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3) + temp_sensor_config_t tsens = TSENS_CONFIG_DEFAULT(); + temp_sensor_set_config(tsens); + temp_sensor_start(); +#if defined(CONFIG_IDF_TARGET_ESP32S3) && (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 3)) +#error \ + "ESP32-S3 internal temperature sensor requires ESP IDF V4.4.3 or higher. See https://github.com/esphome/issues/issues/4271" +#endif + esp_err_t result = temp_sensor_read_celsius(&temperature); + temp_sensor_stop(); + success = (result == ESP_OK); +#endif + + if (success && std::isfinite(temperature)) { + return temperature; + } else { + ESP_LOGD(TAG, "Ignoring invalid temperature (success=%d, value=%.1f)", success, temperature); + return NAN; + } +} diff --git a/lib/CpuTemperature/src/CpuTemperature.h b/lib/CpuTemperature/src/CpuTemperature.h new file mode 100644 index 000000000..06199c825 --- /dev/null +++ b/lib/CpuTemperature/src/CpuTemperature.h @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once + +#include + +class CpuTemperatureClass { +public: + float read(); + +private: + std::mutex _mutex; +}; + +extern CpuTemperatureClass CpuTemperature; diff --git a/src/WebApi_sysstatus.cpp b/src/WebApi_sysstatus.cpp index 782e1f27d..646922a60 100644 --- a/src/WebApi_sysstatus.cpp +++ b/src/WebApi_sysstatus.cpp @@ -7,11 +7,12 @@ #include "NetworkSettings.h" #include "PinMapping.h" #include "WebApi.h" +#include "__compiled_constants.h" #include +#include #include #include #include -#include "__compiled_constants.h" void WebApiSysstatusClass::init(AsyncWebServer& server, Scheduler& scheduler) { @@ -33,6 +34,7 @@ void WebApiSysstatusClass::onSystemStatus(AsyncWebServerRequest* request) root["sdkversion"] = ESP.getSdkVersion(); root["cpufreq"] = ESP.getCpuFreqMHz(); + root["cputemp"] = CpuTemperature.read(); root["heap_total"] = ESP.getHeapSize(); root["heap_used"] = ESP.getHeapSize() - ESP.getFreeHeap(); diff --git a/webapp/src/components/HardwareInfo.vue b/webapp/src/components/HardwareInfo.vue index ed8a67918..a28c4a158 100644 --- a/webapp/src/components/HardwareInfo.vue +++ b/webapp/src/components/HardwareInfo.vue @@ -19,6 +19,10 @@ {{ $t('hardwareinfo.CpuFrequency') }} {{ systemStatus.cpufreq }} {{ $t('hardwareinfo.Mhz') }} + + {{ $t('hardwareinfo.CpuTemperature') }} + {{ $n(systemStatus.cputemp, 'decimalNoDigits') }} {{ $t('hardwareinfo.DegreeC') }} + {{ $t('hardwareinfo.FlashSize') }} diff --git a/webapp/src/locales/de.json b/webapp/src/locales/de.json index f64e23627..7b3f89212 100644 --- a/webapp/src/locales/de.json +++ b/webapp/src/locales/de.json @@ -203,6 +203,8 @@ "ChipCores": "Chip-Kerne", "CpuFrequency": "CPU-Frequenz", "Mhz": "MHz", + "CpuTemperature": "CPU Temperatur", + "DegreeC": "°C", "FlashSize": "Flash-Speichergröße", "Bytes": "Bytes", "MegaBytes": "MB" diff --git a/webapp/src/locales/en.json b/webapp/src/locales/en.json index 8d03c6bc6..2f9f64a70 100644 --- a/webapp/src/locales/en.json +++ b/webapp/src/locales/en.json @@ -203,6 +203,8 @@ "ChipCores": "Chip Cores", "CpuFrequency": "CPU Frequency", "Mhz": "MHz", + "CpuTemperature": "CPU Temperature", + "DegreeC": "°C", "FlashSize": "Flash Memory Size", "Bytes": "Bytes", "MegaBytes": "MB" diff --git a/webapp/src/locales/fr.json b/webapp/src/locales/fr.json index 37b621616..676046b66 100644 --- a/webapp/src/locales/fr.json +++ b/webapp/src/locales/fr.json @@ -203,6 +203,8 @@ "ChipCores": "Nombre de cœurs", "CpuFrequency": "Fréquence du CPU", "Mhz": "MHz", + "CpuTemperature": "CPU Temperature", + "DegreeC": "°C", "FlashSize": "Taille de la mémoire flash", "Bytes": "octets", "MegaBytes": "Mo" diff --git a/webapp/src/types/SystemStatus.ts b/webapp/src/types/SystemStatus.ts index 0a35ec0f7..1ee726e65 100644 --- a/webapp/src/types/SystemStatus.ts +++ b/webapp/src/types/SystemStatus.ts @@ -4,6 +4,7 @@ export interface SystemStatus { chiprevision: number; chipcores: number; cpufreq: number; + cputemp: number; flashsize: number; // FirmwareInfo hostname: string;