From e78b54dc2301736701a1bc801ae194cc838c5c15 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Thu, 10 Oct 2024 13:19:12 +0200 Subject: [PATCH] add S3 temperature #2077 --- interface/src/app/status/HardwareStatus.tsx | 3 +- interface/src/types/system.ts | 1 + src/system.cpp | 35 +++++++++++++++++++++ src/system.h | 21 +++++++++++++ src/web/WebStatusService.cpp | 3 ++ 5 files changed, 62 insertions(+), 1 deletion(-) diff --git a/interface/src/app/status/HardwareStatus.tsx b/interface/src/app/status/HardwareStatus.tsx index 50b21dc3e..a0acf4441 100644 --- a/interface/src/app/status/HardwareStatus.tsx +++ b/interface/src/app/status/HardwareStatus.tsx @@ -97,7 +97,8 @@ const HardwareStatus = () => { (data.cpu_cores === 1 ? 'single-core)' : 'dual-core)') + ' @ ' + data.cpu_freq_mhz + - ' Mhz' + ' Mhz' + + (data.temperature ? ', T: ' + data.temperature + ' °C' : '') } /> diff --git a/interface/src/types/system.ts b/interface/src/types/system.ts index ca9d34555..b71586af3 100644 --- a/interface/src/types/system.ts +++ b/interface/src/types/system.ts @@ -41,6 +41,7 @@ export interface SystemStatus { has_loader: boolean; has_partition: boolean; status: string; + temperature?: number; } export enum LogLevel { diff --git a/src/system.cpp b/src/system.cpp index 531c7ff22..1c7d776cd 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -458,6 +458,21 @@ void System::start() { appused_ = ESP.getSketchSize() / 1024; appfree_ = esp_ota_get_running_partition()->size / 1024 - appused_; refreshHeapMem(); // refresh free heap and max alloc heap +#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2 +#if ESP_IDF_VERSION_MAJOR < 5 + temp_sensor_config_t temp_sensor = TSENS_CONFIG_DEFAULT(); + temp_sensor_get_config(&temp_sensor); + temp_sensor.dac_offset = TSENS_DAC_DEFAULT; // DEFAULT: range:-10℃ ~ 80℃, error < 1℃. + temp_sensor_set_config(temp_sensor); + temp_sensor_start(); + temp_sensor_read_celsius(&temperature_); +#else + temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80); + temperature_sensor_install(&temp_sensor_config, &temperature_handle_); + temperature_sensor_enable(temperature_handle_); + temperature_sensor_get_celsius(temperature_handle_, &temperature_); +#endif +#endif #endif EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { @@ -692,6 +707,9 @@ void System::heartbeat_json(JsonObject output) { #ifndef EMSESP_STANDALONE output["freemem"] = getHeapMem(); output["max_alloc"] = getMaxAllocMem(); +#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2 + output["temperature"] = temperature_; +#endif #endif #ifndef EMSESP_STANDALONE @@ -770,6 +788,16 @@ void System::system_check() { if (!last_system_check_ || ((uint32_t)(uuid::get_uptime() - last_system_check_) >= SYSTEM_CHECK_FREQUENCY)) { last_system_check_ = uuid::get_uptime(); +#ifndef EMSESP_STANDALONE +#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2 +#if ESP_IDF_VERSION_MAJOR < 5 + temp_sensor_read_celsius(&temperature_); +#else + temperature_sensor_get_celsius(temperature_handle_, &temperature_); +#endif +#endif +#endif + #ifdef EMSESP_PINGTEST static uint64_t ping_count = 0; LOG_NOTICE("Ping test, #%d", ping_count++); @@ -987,6 +1015,9 @@ void System::show_system(uuid::console::Shell & shell) { shell.printfln(" SDK version: %s", ESP.getSdkVersion()); shell.printfln(" CPU frequency: %lu MHz", ESP.getCpuFreqMHz()); +#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2 + shell.printfln(" CPU temperature: %d °C", (int)temperature()); +#endif shell.printfln(" Free heap/Max alloc: %lu KB / %lu KB", getHeapMem(), getMaxAllocMem()); shell.printfln(" App used/free: %lu KB / %lu KB", appUsed(), appFree()); uint32_t FSused = LittleFS.usedBytes() / 1024; @@ -1444,6 +1475,10 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output node["freePsram"] = ESP.getFreePsram() / 1024; } node["model"] = EMSESP::system_.getBBQKeesGatewayDetails(); +#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2 + node["temperature"] = EMSESP::system_.temperature(); +#endif + #endif // Network Status diff --git a/src/system.h b/src/system.h index 60b619195..3bf59eef7 100644 --- a/src/system.h +++ b/src/system.h @@ -39,6 +39,14 @@ #include #include +#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2 +#if ESP_IDF_VERSION_MAJOR < 5 +#include "driver/temp_sensor.h" +#else +#include "driver/temperature_sensor.h" +#endif +#endif + using uuid::console::Shell; #define EMSESP_FS_CONFIG_DIRECTORY "/config" @@ -303,6 +311,12 @@ class System { test_set_all_active_ = n; } +#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2 + float temperature() { + return temperature_; + } +#endif + private: static uuid::log::Logger logger_; static bool restart_requested_; @@ -395,6 +409,13 @@ class System { uint32_t psram_; uint32_t appused_; uint32_t appfree_; + +#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2 +#if ESP_IDF_VERSION_MAJOR >= 5 + temperature_sensor_handle_t temperature_handle_ = NULL; +#endif + float temperature_ = 0; +#endif }; } // namespace emsesp diff --git a/src/web/WebStatusService.cpp b/src/web/WebStatusService.cpp index 378e44cf1..1fffc5881 100644 --- a/src/web/WebStatusService.cpp +++ b/src/web/WebStatusService.cpp @@ -119,6 +119,9 @@ void WebStatusService::systemStatus(AsyncWebServerRequest * request) { root["free_psram"] = ESP.getFreePsram() / 1024; } root["model"] = EMSESP::system_.getBBQKeesGatewayDetails(); +#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2 + root["temperature"] = EMSESP::system_.temperature(); +#endif // check for a factory partition first const esp_partition_t * partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, nullptr);