Skip to content

Commit

Permalink
Feature: Show MCU temperature in system info
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed May 28, 2024
1 parent e211dd5 commit 6ce4740
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 1 deletion.
51 changes: 51 additions & 0 deletions lib/CpuTemperature/src/CpuTemperature.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2024 Thomas Basler and others
*/

#include "CpuTemperature.h"
#include <Arduino.h>

#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<std::mutex> 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;
}
}
14 changes: 14 additions & 0 deletions lib/CpuTemperature/src/CpuTemperature.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include <mutex>

class CpuTemperatureClass {
public:
float read();

private:
std::mutex _mutex;
};

extern CpuTemperatureClass CpuTemperature;
4 changes: 3 additions & 1 deletion src/WebApi_sysstatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
#include "NetworkSettings.h"
#include "PinMapping.h"
#include "WebApi.h"
#include "__compiled_constants.h"
#include <AsyncJson.h>
#include <CpuTemperature.h>
#include <Hoymiles.h>
#include <LittleFS.h>
#include <ResetReason.h>
#include "__compiled_constants.h"

void WebApiSysstatusClass::init(AsyncWebServer& server, Scheduler& scheduler)
{
Expand All @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions webapp/src/components/HardwareInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<th>{{ $t('hardwareinfo.CpuFrequency') }}</th>
<td>{{ systemStatus.cpufreq }} {{ $t('hardwareinfo.Mhz') }}</td>
</tr>
<tr>
<th>{{ $t('hardwareinfo.CpuTemperature') }}</th>
<td>{{ $n(systemStatus.cputemp, 'decimalNoDigits') }} {{ $t('hardwareinfo.DegreeC') }}</td>
</tr>
<tr>
<th>{{ $t('hardwareinfo.FlashSize') }}</th>
<td>
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions webapp/src/types/SystemStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface SystemStatus {
chiprevision: number;
chipcores: number;
cpufreq: number;
cputemp: number;
flashsize: number;
// FirmwareInfo
hostname: string;
Expand Down

0 comments on commit 6ce4740

Please sign in to comment.