Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[P026][Sysvars] Add Internal temperature sensor value for ESP32 #4820

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
97 changes: 59 additions & 38 deletions src/_P026_Sysinfo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
// #################################### Plugin 026: System Info ##########################################
// #######################################################################################################

/** Changelog:
* 2023-09-23 tonhuisman: Add Internal temperature option for ESP32
* Format source using Uncrustify
* Move #if check to P026_data_struct.h as Arduino compiler doesn't support that :(
* Move other defines to P026_data_struct.h
* 2023-09-23 tonhuisman: Start changelog
*/

# include "src/DataStructs/ESPEasy_packed_raw_data.h"
# include "src/ESPEasyCore/ESPEasyNetwork.h"
Expand All @@ -17,32 +24,31 @@
# define PLUGIN_ID_026 26
# define PLUGIN_NAME_026 "Generic - System Info"

// place sensor type selector right after the output value settings
# define P026_QUERY1_CONFIG_POS 0
# define P026_SENSOR_TYPE_INDEX (P026_QUERY1_CONFIG_POS + VARS_PER_TASK)
# define P026_NR_OUTPUT_VALUES getValueCountFromSensorType(static_cast<Sensor_VType>(PCONFIG(P026_SENSOR_TYPE_INDEX)))

# define P026_NR_OUTPUT_OPTIONS 14
# include "src/PluginStructs/P026_data_struct.h" // Arduino doesn't do #if in .ino sources :(

const __FlashStringHelper* Plugin_026_valuename(uint8_t value_nr, bool displayString) {
const __FlashStringHelper* strings[] {
F("Uptime") , F("uptime"),
F("Free RAM") , F("freeheap"),
F("Wifi RSSI") , F("rssi"),
F("Input VCC") , F("vcc"),
F("System load") , F("load"),
F("IP 1.Octet") , F("ip1"),
F("IP 2.Octet") , F("ip2"),
F("IP 3.Octet") , F("ip3"),
F("IP 4.Octet") , F("ip4"),
F("Web activity") , F("web"),
F("Free Stack") , F("freestack"),
F("None") , F(""),
F("WiFi TX pwr") , F("txpwr"),
F("Free 2nd Heap"), F("free2ndheap")
const __FlashStringHelper *strings[] {
F("Uptime"), F("uptime"),
F("Free RAM"), F("freeheap"),
F("Wifi RSSI"), F("rssi"),
F("Input VCC"), F("vcc"),
F("System load"), F("load"),
F("IP 1.Octet"), F("ip1"),
F("IP 2.Octet"), F("ip2"),
F("IP 3.Octet"), F("ip3"),
F("IP 4.Octet"), F("ip4"),
F("Web activity"), F("web"),
F("Free Stack"), F("freestack"),
F("None"), F(""),
F("WiFi TX pwr"), F("txpwr"),
F("Free 2nd Heap"), F("free2ndheap"),
# if FEATURE_INTERNAL_TEMPERATURE
F("Internal temperature (ESP32)"), F("internaltemp"),
# endif // if FEATURE_INTERNAL_TEMPERATURE
};
const size_t index = (2* value_nr) + (displayString ? 0 : 1);
const size_t index = (2 * value_nr) + (displayString ? 0 : 1);
constexpr size_t nrStrings = NR_ELEMENTS(strings);

if (index < nrStrings) {
return strings[index];
}
Expand Down Expand Up @@ -77,6 +83,7 @@ boolean Plugin_026(uint8_t function, struct EventStruct *event, String& string)
case PLUGIN_GET_DEVICEVALUENAMES:
{
const int valueCount = P026_NR_OUTPUT_VALUES;

for (uint8_t i = 0; i < VARS_PER_TASK; ++i) {
if (i < valueCount) {
const uint8_t pconfigIndex = i + P026_QUERY1_CONFIG_POS;
Expand Down Expand Up @@ -134,8 +141,9 @@ boolean Plugin_026(uint8_t function, struct EventStruct *event, String& string)
// Work around to get the "none" at the end.
options[index] = Plugin_026_valuename(11, true);
indices[index] = 11;

const int valueCount = P026_NR_OUTPUT_VALUES;

for (uint8_t i = 0; i < valueCount; ++i) {
const uint8_t pconfigIndex = i + P026_QUERY1_CONFIG_POS;
sensorTypeHelper_loadOutputSelector(event, pconfigIndex, i, P026_NR_OUTPUT_OPTIONS, options, indices);
Expand All @@ -153,6 +161,7 @@ boolean Plugin_026(uint8_t function, struct EventStruct *event, String& string)
{
// Save output selector parameters.
const int valueCount = P026_NR_OUTPUT_VALUES;

for (uint8_t i = 0; i < valueCount; ++i) {
const uint8_t pconfigIndex = i + P026_QUERY1_CONFIG_POS;
const uint8_t choice = PCONFIG(pconfigIndex);
Expand All @@ -171,10 +180,12 @@ boolean Plugin_026(uint8_t function, struct EventStruct *event, String& string)
case PLUGIN_READ:
{
const int valueCount = P026_NR_OUTPUT_VALUES;

for (int i = 0; i < valueCount; ++i) {
UserVar[event->BaseVarIndex + i] = P026_get_value(PCONFIG(i));
}
#ifndef LIMIT_BUILD_SIZE
# ifndef LIMIT_BUILD_SIZE

if (loglevelActiveFor(LOG_LEVEL_INFO)) {
String log;

Expand All @@ -191,7 +202,7 @@ boolean Plugin_026(uint8_t function, struct EventStruct *event, String& string)
addLogMove(LOG_LEVEL_INFO, log);
}
}
#endif
# endif // ifndef LIMIT_BUILD_SIZE
success = true;
break;
}
Expand All @@ -202,19 +213,23 @@ boolean Plugin_026(uint8_t function, struct EventStruct *event, String& string)
// return decode(bytes,
// [header, uint24, uint24, int8, vcc, pct_8, uint8, uint8, uint8, uint8, uint24, uint16],
// ['header', 'uptime', 'freeheap', 'rssi', 'vcc', 'load', 'ip1', 'ip2', 'ip3', 'ip4', 'web', 'freestack']);
// on ESP32 you can add 'internaltemperature' of type int16 (1e2) to the list
uint8_t index = 0;
string += LoRa_addInt(P026_get_value(index++), PackedData_uint24); // uptime
string += LoRa_addInt(P026_get_value(index++), PackedData_uint24); // freeheap
string += LoRa_addFloat(P026_get_value(index++), PackedData_int8); // rssi
string += LoRa_addFloat(P026_get_value(index++), PackedData_vcc); // vcc
string += LoRa_addFloat(P026_get_value(index++), PackedData_pct_8); // load
string += LoRa_addInt(P026_get_value(index++), PackedData_uint8); // ip1
string += LoRa_addInt(P026_get_value(index++), PackedData_uint8); // ip2
string += LoRa_addInt(P026_get_value(index++), PackedData_uint8); // ip3
string += LoRa_addInt(P026_get_value(index++), PackedData_uint8); // ip4
string += LoRa_addInt(P026_get_value(index++), PackedData_uint24); // web
string += LoRa_addInt(P026_get_value(index++), PackedData_uint16); // freestack
event->Par1 = index; // valuecount
string += LoRa_addInt(P026_get_value(index++), PackedData_uint24); // uptime
string += LoRa_addInt(P026_get_value(index++), PackedData_uint24); // freeheap
string += LoRa_addFloat(P026_get_value(index++), PackedData_int8); // rssi
string += LoRa_addFloat(P026_get_value(index++), PackedData_vcc); // vcc
string += LoRa_addFloat(P026_get_value(index++), PackedData_pct_8); // load
string += LoRa_addInt(P026_get_value(index++), PackedData_uint8); // ip1
string += LoRa_addInt(P026_get_value(index++), PackedData_uint8); // ip2
string += LoRa_addInt(P026_get_value(index++), PackedData_uint8); // ip3
string += LoRa_addInt(P026_get_value(index++), PackedData_uint8); // ip4
string += LoRa_addInt(P026_get_value(index++), PackedData_uint24); // web
string += LoRa_addInt(P026_get_value(index++), PackedData_uint16); // freestack
# if FEATURE_INTERNAL_TEMPERATURE
string += LoRa_addInt(P026_get_value(index++) * 100.0f, PackedData_int16_1e2); // internal temperature in 0.01 degrees
tonhuisman marked this conversation as resolved.
Show resolved Hide resolved
# endif // if FEATURE_INTERNAL_TEMPERATURE
event->Par1 = index; // valuecount
success = true;
break;
}
Expand All @@ -226,6 +241,7 @@ boolean Plugin_026(uint8_t function, struct EventStruct *event, String& string)
float P026_get_value(uint8_t type)
{
float res{};

switch (type)
{
case 0: res = getUptimeMinutes(); break;
Expand All @@ -243,7 +259,7 @@ float P026_get_value(uint8_t type)
case 6:
case 7:
case 8:
res = NetworkLocalIP()[type - 5]; break;
res = NetworkLocalIP()[type - 5]; break;
case 9: res = timePassedSince(lastWeb) / 1000.0f; break; // respond in seconds
case 10: res = getCurrentFreeStack(); break;
case 12: res = WiFiEventData.wifi_TX_pwr; break;
Expand All @@ -252,6 +268,11 @@ float P026_get_value(uint8_t type)
res = FreeMem2ndHeap();
# endif // ifdef USE_SECOND_HEAP
break;
case 14:
# if FEATURE_INTERNAL_TEMPERATURE
res = getInternalTemperature();
# endif // if FEATURE_INTERNAL_TEMPERATURE
break;
}
return res;
}
Expand Down
8 changes: 8 additions & 0 deletions src/src/CustomBuild/define_plugin_sets.h
Original file line number Diff line number Diff line change
Expand Up @@ -2975,6 +2975,14 @@ To create/register a plugin, you have to :
#define FEATURE_PLUGIN_PRIORITY 0 // Disable by default
#endif

#ifndef FEATURE_INTERNAL_TEMPERATURE
#if defined(ESP32) // Feature is only available on (most?) ESP32 chips
#define FEATURE_INTERNAL_TEMPERATURE 1
#else
#define FEATURE_INTERNAL_TEMPERATURE 0 // Not evailable on ESP8266
#endif
#endif

#ifndef FEATURE_I2C_DEVICE_CHECK
#ifdef ESP8266_1M
#define FEATURE_I2C_DEVICE_CHECK 0 // Disabled by default for 1M units
Expand Down
42 changes: 42 additions & 0 deletions src/src/Helpers/Hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,48 @@ int espeasy_analogRead(int pin, bool readAsTouch) {

#endif // ifdef ESP32

#if FEATURE_INTERNAL_TEMPERATURE

/**
* Code based on: https://github.com/esphome/esphome/blob/518ecb4cc4489c8a76b899bfda7576b05d84c226/esphome/components/internal_temperature/internal_temperature.cpp#L40
*/

#ifdef ESP32
#if defined(ESP32_CLASSIC)
// there is no official API available on the original ESP32
extern "C" {
uint8_t temprature_sens_read();
}
#elif defined(ESP32C3) || defined(ESP32S2) || defined(ESP32S3)
#include "driver/temp_sensor.h"
#endif // ESP32_CLASSIC
#endif // ESP32

float getInternalTemperature() {
tonhuisman marked this conversation as resolved.
Show resolved Hide resolved
float temperature = -273.15f; // Inprobable value
tonhuisman marked this conversation as resolved.
Show resolved Hide resolved
bool success;
#ifdef ESP32
#if defined(ESP32_CLASSIC)
uint8_t raw = temprature_sens_read();
#ifndef BUILD_NO_DEBUG
addLog(LOG_LEVEL_DEBUG, concat(F("ESP32: Raw temperature value: "), raw));
#endif
success = (raw != 128);
temperature = (raw - 32) / 1.8f;
#elif defined(ESP32C3) || defined(ESP32S2) || defined(ESP32S3)
temp_sensor_config_t tsens = TSENS_CONFIG_DEFAULT();
temp_sensor_set_config(tsens);
temp_sensor_start();
esp_err_t result = temp_sensor_read_celsius(&temperature);
temp_sensor_stop();
if (result = ESP_OK) {
temperature = -273.15f;
tonhuisman marked this conversation as resolved.
Show resolved Hide resolved
}
#endif // ESP32_CLASSIC
#endif // USE_ESP32
return temperature;
}
#endif // if FEATURE_INTERNAL_TEMPERATURE

/********************************************************************************************\
Hardware information
Expand Down
4 changes: 4 additions & 0 deletions src/src/Helpers/Hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ int espeasy_analogRead(int pin,
extern esp_adc_cal_characteristics_t adc_chars[ADC_ATTEN_MAX];
#endif // ifdef ESP32

#if FEATURE_INTERNAL_TEMPERATURE
float getInternalTemperature();
#endif // if FEATURE_INTERNAL_TEMPERATURE


/********************************************************************************************\
Hardware information
Expand Down
6 changes: 6 additions & 0 deletions src/src/Helpers/StringProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ const __FlashStringHelper * getLabel(LabelType::Enum label) {
case LabelType::MAX_OTA_SKETCH_SIZE: return F("Max. OTA Sketch Size");
case LabelType::OTA_2STEP: return F("OTA 2-step Needed");
case LabelType::OTA_POSSIBLE: return F("OTA possible");
#if FEATURE_INTERNAL_TEMPERATURE
case LabelType::INTERNAL_TEMPERATURE: return F("Internal temperature (ESP32)");
#endif // if FEATURE_INTERNAL_TEMPERATURE
#if FEATURE_ETHERNET
case LabelType::ETH_IP_ADDRESS: return F("Eth IP Address");
case LabelType::ETH_IP_SUBNET: return F("Eth IP Subnet");
Expand Down Expand Up @@ -413,6 +416,9 @@ String getValue(LabelType::Enum label) {
case LabelType::IP_ADDRESS_SUBNET: return getValue(LabelType::IP_ADDRESS) + F(" / ") + getValue(LabelType::IP_SUBNET);
case LabelType::GATEWAY: return formatIP(NetworkGatewayIP());
case LabelType::CLIENT_IP: return formatIP(web_server.client().remoteIP());
#if FEATURE_INTERNAL_TEMPERATURE
case LabelType::INTERNAL_TEMPERATURE: return toString(getInternalTemperature());
#endif // if FEATURE_INTERNAL_TEMPERATURE

#if FEATURE_MDNS
case LabelType::M_DNS: return NetworkGetHostname() + F(".local");
Expand Down
3 changes: 3 additions & 0 deletions src/src/Helpers/StringProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ struct LabelType {
MAX_OTA_SKETCH_SIZE,
OTA_2STEP,
OTA_POSSIBLE,
#if FEATURE_INTERNAL_TEMPERATURE
INTERNAL_TEMPERATURE,
#endif // if FEATURE_INTERNAL_TEMPERATURE
#if FEATURE_ETHERNET
ETH_IP_ADDRESS,
ETH_IP_SUBNET,
Expand Down
6 changes: 6 additions & 0 deletions src/src/Helpers/SystemVariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ LabelType::Enum SystemVariables2LabelType(SystemVariables::Enum enumval) {
case SystemVariables::DNS_2: label = LabelType::DNS_2; break;
case SystemVariables::GATEWAY: label = LabelType::GATEWAY; break;
case SystemVariables::CLIENTIP: label = LabelType::CLIENT_IP; break;
#if FEATURE_INTERNAL_TEMPERATURE
case SystemVariables::INTERNAL_TEMPERATURE: label = LabelType::INTERNAL_TEMPERATURE; break;
#endif // if FEATURE_INTERNAL_TEMPERATURE

#if FEATURE_ETHERNET

Expand Down Expand Up @@ -359,6 +362,9 @@ const __FlashStringHelper * SystemVariables::toFlashString(SystemVariables::Enum
case Enum::ISMQTTIMP: return F("ismqttimp");
case Enum::ISNTP: return F("isntp");
case Enum::ISWIFI: return F("iswifi");
#if FEATURE_INTERNAL_TEMPERATURE
case Enum::INTERNAL_TEMPERATURE: return F("internaltemperature");
#endif // if FEATURE_INTERNAL_TEMPERATURE
#if FEATURE_ETHERNET
case Enum::ETHWIFIMODE: return F("ethwifimode");
case Enum::ETHCONNECTED: return F("ethconnected");
Expand Down
31 changes: 16 additions & 15 deletions src/src/Helpers/SystemVariables.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "../../ESPEasy_common.h"

class SystemVariables {

public:

enum Enum : uint8_t {
Expand All @@ -13,13 +12,16 @@ class SystemVariables {
BSSID,
CR,
IP,
IP4, // 4th IP octet
IP4, // 4th IP octet
SUBNET,
GATEWAY,
DNS,
DNS_1,
DNS_2,
CLIENTIP,
#if FEATURE_INTERNAL_TEMPERATURE
INTERNAL_TEMPERATURE,
#endif // if FEATURE_INTERNAL_TEMPERATURE
ISMQTT,
ISMQTTIMP,
ISNTP,
Expand Down Expand Up @@ -85,7 +87,7 @@ class SystemVariables {
SYS_MONTH_0,
S_CR,
S_LF,
UNIT_sysvar, // We already use UNIT as define.
UNIT_sysvar, // We already use UNIT as define.
#if FEATURE_ZEROFILLED_UNITNUMBER
UNIT_0_sysvar,
#endif // FEATURE_ZEROFILLED_UNITNUMBER
Expand All @@ -96,12 +98,12 @@ class SystemVariables {
UPTIME_MS,
VCC,
WI_CH,
FLASH_FREQ, // Frequency of the flash chip
FLASH_SIZE, // Real size of the flash chip
FLASH_FREQ, // Frequency of the flash chip
FLASH_SIZE, // Real size of the flash chip
FLASH_CHIP_VENDOR,
FLASH_CHIP_MODEL,
FS_SIZE, // Size of the file system
FS_FREE, // Free space (in bytes) on the file system
FS_SIZE, // Size of the file system
FS_FREE, // Free space (in bytes) on the file system

ESP_CHIP_ID,
ESP_CHIP_FREQ,
Expand All @@ -117,18 +119,17 @@ class SystemVariables {

// Find the next thing to replace.
// Return UNKNOWN when nothing needs to be replaced.
static SystemVariables::Enum nextReplacementEnum(const String& str, SystemVariables::Enum last_tested);

static String toString(SystemVariables::Enum enumval);
static const __FlashStringHelper * toFlashString(SystemVariables::Enum enumval);
static SystemVariables::Enum nextReplacementEnum(const String & str,
SystemVariables::Enum last_tested);

static String getSystemVariable(SystemVariables::Enum enumval);
static String toString(SystemVariables::Enum enumval);
static const __FlashStringHelper* toFlashString(SystemVariables::Enum enumval);

static void parseSystemVariables(String& s, boolean useURLencode);
static String getSystemVariable(SystemVariables::Enum enumval);

static void parseSystemVariables(String& s,
boolean useURLencode);
};




#endif // HELPERS_SYSTEMVARIABLES_H