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

add read_temp_c to system + mqtt #1739

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/publisher/pubMqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class PubMqtt {
publish(subtopics[MQTT_UPTIME], mVal.data());
publish(subtopics[MQTT_RSSI], String(WiFi.RSSI()).c_str());
publish(subtopics[MQTT_FREE_HEAP], String(ESP.getFreeHeap()).c_str());
publish(subtopics[MQTT_TEMP_SENS_C], String(ah::readTemperature()).c_str());
#ifndef ESP32
publish(subtopics[MQTT_HEAP_FRAG], String(ESP.getHeapFragmentation()).c_str());
#endif
Expand Down Expand Up @@ -656,7 +657,7 @@ class PubMqtt {
size_t index;
size_t total;

message_s()
message_s()
: topic { nullptr }
, payload { nullptr }
, len { 0 }
Expand Down
3 changes: 2 additions & 1 deletion src/publisher/pubMqttDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ enum {
MQTT_STATUS,
MQTT_LWT_ONLINE,
MQTT_LWT_OFFLINE,
MQTT_ACK_PWR_LMT
MQTT_ACK_PWR_LMT,
MQTT_TEMP_SENS_C
};

const char* const subtopics[] PROGMEM = {
Expand Down
17 changes: 16 additions & 1 deletion src/utils/helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 2023 Ahoy, https://github.com/lumpapu/ahoy
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------

#include <Arduino.h>
#include "helper.h"
#include "dbg.h"
#include "../plugins/plugin_lang.h"
Expand Down Expand Up @@ -142,4 +142,19 @@ namespace ah {
}
DBGPRINTLN("");
}
float readTemperature() {
/*// ADC1 channel 0 is GPIO36
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_0);
int adc_reading = adc1_get_raw(ADC1_CHANNEL_0);
// Convert the raw ADC reading to a voltage in mV
esp_adc_cal_characteristics_t characteristics;
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_0, ADC_WIDTH_BIT_12, 1100, &characteristics);
uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, &characteristics);
// Convert the voltage to a temperature in Celsius
// This formula is an approximation and might need to be calibrated for your specific use case.
float temperature = (voltage - 500) / 10.0;*/

return temperatureRead();
}
}
2 changes: 2 additions & 0 deletions src/utils/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace ah {
String getTimeStrMs(uint64_t t);
uint64_t Serial2u64(const char *val);
void dumpBuf(uint8_t buf[], uint8_t len, uint8_t firstRepl = 0, uint8_t lastRepl = 0);

float readTemperature();
}

#endif /*__HELPER_H__*/
2 changes: 2 additions & 0 deletions src/web/RestApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,8 @@ class RestApi {
void getChipInfo(JsonObject obj) {
obj[F("cpu_freq")] = ESP.getCpuFreqMHz();
obj[F("sdk")] = ESP.getSdkVersion();
obj[F("temp_sensor_c")] = ah::readTemperature();

#if defined(ESP32)
obj[F("revision")] = ESP.getChipRevision();
obj[F("model")] = ESP.getChipModel();
Expand Down
3 changes: 2 additions & 1 deletion src/web/html/system.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
tr("{#ENVIRONMENT}", obj.generic.env + " ({#BUILD_OPTIONS}: " + obj.generic.modules + ")"),
tr("Version", obj.generic.version + " - " + obj.generic.build),
tr("Chip", "CPU: " + obj.chip.cpu_freq + "MHz, " + obj.chip.cores + " Core(s)"),
tr("Chip Model", obj.chip.model)
tr("Chip Model", obj.chip.model),
tr("Chip temp.", obj.chip.temp_sensor_c + "&deg;C"),
]

document.getElementById("info").append(
Expand Down