Skip to content

Commit 3323555

Browse files
authored
Added more sensors (#18)
1 parent e780037 commit 3323555

File tree

6 files changed

+161
-2
lines changed

6 files changed

+161
-2
lines changed

src/board_config.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131
#define IDR_CASE_OPENED_MODE INPUT_PULLUP
3232
// End of intruder config
3333

34+
// Batt config
35+
#define HAVE_BATTERY 1
36+
#define ADC_PIN 4
37+
// End of batt config
38+
39+
// Temp sensor
40+
// Broken for ESP32S3 so far: https://github.com/espressif/esp-idf/issues/8088
41+
// #define HAVE_TEMP_SENSOR 1
42+
// End of core temp sensor
43+
3444
// Log store config
3545
// #define HAVE_SD_CARD 1
3646
#define SD_CARD_SS_PIN 14

src/board_manager.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111
#include <SD.h>
1212
#include <ArduinoLog.h>
1313

14+
#if HAVE_BATTERY
15+
#include <esp_adc_cal.h>
16+
#endif
17+
18+
#if HAVE_TEMP_SENSOR
19+
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5,0,0)
20+
#include <driver/temp_sensor.h>
21+
#else
22+
#include <driver/temperature_sensor.h>
23+
#endif
24+
#endif
25+
1426
#define LOG_PREFIX "board_manager: "
1527

1628
namespace
@@ -19,6 +31,10 @@ namespace
1931
static TUIManager& uiManager = TUIManager::Instance();
2032
static TAuthenticator& authenticator = TAuthenticator::Instance();
2133

34+
#if HAVE_TEMP_SENSOR && ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(5,0,0)
35+
temperature_sensor_handle_t temp_sensor;
36+
#endif
37+
2238
void restart()
2339
{
2440
esp_restart();
@@ -34,6 +50,58 @@ namespace
3450
esp_deep_sleep_start();
3551
}
3652

53+
uint32_t adcBattVoltage()
54+
{
55+
#if !HAVE_BATTERY || !ADC_PIN
56+
return 0;
57+
#else
58+
esp_adc_cal_characteristics_t adc_chars;
59+
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 1100, &adc_chars);
60+
uint32_t v1 = 0, raw = 0;
61+
raw = analogRead(ADC_PIN);
62+
v1 = esp_adc_cal_raw_to_voltage(raw, &adc_chars) * 2;
63+
return v1;
64+
#endif
65+
}
66+
67+
void initEsp32TempSensor()
68+
{
69+
#if !HAVE_TEMP_SENSOR
70+
return;
71+
#else
72+
73+
// https://docs.espressif.com/projects/esp-idf/zh_CN/v4.4.4/esp32s3/api-reference/peripherals/temp_sensor.html
74+
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5,0,0)
75+
temp_sensor_config_t temp_sensor = TSENS_CONFIG_DEFAULT();
76+
temp_sensor_set_config(temp_sensor);
77+
temp_sensor_start();
78+
#else
79+
// https://docs.espressif.com/projects/esp-idf/zh_CN/v5.0.1/esp32s3/api-reference/peripherals/temp_sensor.html
80+
static temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(10, 50);
81+
temperature_sensor_install(&temp_sensor_config, &temp_sensor);
82+
temperature_sensor_enable(temp_sensor);
83+
#endif
84+
#endif
85+
}
86+
87+
float esp32CoreTemp()
88+
{
89+
#if !HAVE_TEMP_SENSOR
90+
return 0.0f;
91+
#else
92+
93+
float tsens_value;
94+
// https://docs.espressif.com/projects/esp-idf/zh_CN/v4.4.4/esp32s3/api-reference/peripherals/temp_sensor.html
95+
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5,0,0)
96+
temp_sensor_read_celsius(&tsens_value);
97+
#else
98+
// https://docs.espressif.com/projects/esp-idf/zh_CN/v5.0.1/esp32s3/api-reference/peripherals/temp_sensor.html
99+
temperature_sensor_get_celsius(temp_sensor, &tsens_value);
100+
#endif
101+
102+
return tsens_value;
103+
#endif
104+
}
37105
}
38106

39107
TBoardManager& TBoardManager::Instance()
@@ -56,6 +124,9 @@ bool TBoardManager::Begin()
56124
Log.infoln(LOG_PREFIX "IDR not available");
57125
#endif
58126

127+
Log.infoln(LOG_PREFIX "setup temp sensor");
128+
initEsp32TempSensor();
129+
59130
Log.infoln(LOG_PREFIX "setup preferences");
60131
if (!TPrefStore::Instance().Begin()) {
61132
Log.errorln(LOG_PREFIX "unable to open preferences");
@@ -185,6 +256,16 @@ uint32_t TBoardManager::FreeHeap() const
185256
return esp_get_free_heap_size();
186257
}
187258

259+
uint32_t TBoardManager::BattVoltage() const
260+
{
261+
return adcBattVoltage();
262+
}
263+
264+
float TBoardManager::CoreTemp() const
265+
{
266+
return esp32CoreTemp();
267+
}
268+
188269
void TBoardManager::tickRestart()
189270
{
190271
if (restartAt > 0 && Uptime() >= restartAt) {
@@ -209,4 +290,6 @@ void TBoardManager::tickBoardInfo()
209290

210291
infoUpdatedAt = Uptime();
211292
boardInfo.LocalIP = netManager.LocalIP();
293+
boardInfo.CoreTemp = CoreTemp();
294+
boardInfo.BattVoltage = BattVoltage();
212295
}

src/board_manager.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define BB_BOARD_MANAGER_H
33

44
#include "config.h"
5+
#include "board_config.h"
56
#include "net_manager.h"
67

78
class TBoardManager
@@ -21,10 +22,22 @@ class TBoardManager
2122
{
2223
BoardState State;
2324
IPAddress LocalIP;
25+
uint32_t BattVoltage;
26+
float CoreTemp;
2427

2528
bool operator==(const BoardInfo& info) const
2629
{
27-
return State == info.State && LocalIP == info.LocalIP;
30+
return (
31+
State == info.State &&
32+
LocalIP == info.LocalIP &&
33+
#if HAVE_BATTERY
34+
abs((int)BattVoltage - (int)info.BattVoltage) < 50 &&
35+
#endif
36+
#if HAVE_TEMP_SENSOR
37+
fabs(CoreTemp - info.CoreTemp) < 0.1 &&
38+
#endif
39+
true
40+
);
2841
}
2942
};
3043

@@ -40,6 +53,8 @@ class TBoardManager
4053
const TConfig& RuntimeConfig() const;
4154
uint32_t Uptime() const;
4255
uint32_t FreeHeap() const;
56+
uint32_t BattVoltage() const;
57+
float CoreTemp() const;
4358

4459
private:
4560
TBoardManager() = default;

src/gui.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ void TGUI::ShowScreenIdle(TBoardManager::BoardInfo boardInfo)
329329
lv_obj_t* ipLabel = lv_label_create(cont);
330330
lv_obj_align(ipLabel, LV_ALIGN_BOTTOM_LEFT, 24, -24);
331331
lv_obj_set_style_text_font(ipLabel, &font_roboto_mono_20, 0);
332-
lv_obj_set_style_text_align(ipLabel, LV_TEXT_ALIGN_CENTER, 0);
332+
lv_obj_set_style_text_align(ipLabel, LV_TEXT_ALIGN_LEFT, 0);
333333
lv_label_set_text_fmt(ipLabel, "IP: %s", boardInfo.LocalIP.toString());
334334

335335
lv_obj_add_event_cb(ipLabel,
@@ -344,6 +344,48 @@ void TGUI::ShowScreenIdle(TBoardManager::BoardInfo boardInfo)
344344
nullptr
345345
);
346346
lv_msg_subsribe_obj(GUI_MESSAGE_UPDATE_BOARD_INFO, ipLabel, nullptr);
347+
348+
#if HAVE_TEMP_SENSOR
349+
lv_obj_t* tempLabel = lv_label_create(cont);
350+
lv_obj_align(tempLabel, LV_ALIGN_BOTTOM_MID, 0, -24);
351+
lv_obj_set_style_text_font(tempLabel, &font_roboto_mono_20, 0);
352+
lv_obj_set_style_text_align(tempLabel, LV_TEXT_ALIGN_CENTER, 0);
353+
lv_label_set_text_fmt(tempLabel, "Core: %.2f°C", boardInfo.CoreTemp);
354+
355+
lv_obj_add_event_cb(tempLabel,
356+
[](lv_event_t* e) -> void {
357+
lv_msg_t* m = lv_event_get_msg(e);
358+
lv_obj_t* label = lv_event_get_target(e);
359+
360+
auto info = reinterpret_cast<const TBoardManager::BoardInfo*>(lv_msg_get_payload(m));
361+
lv_label_set_text_fmt(label, "BATT: %.2f°C", info->CoreTemp);
362+
},
363+
LV_EVENT_MSG_RECEIVED,
364+
nullptr
365+
);
366+
lv_msg_subsribe_obj(GUI_MESSAGE_UPDATE_BOARD_INFO, tempLabel, nullptr);
367+
#endif
368+
369+
#if HAVE_BATTERY
370+
lv_obj_t* battLabel = lv_label_create(cont);
371+
lv_obj_align(battLabel, LV_ALIGN_BOTTOM_RIGHT, -24, -24);
372+
lv_obj_set_style_text_font(battLabel, &font_roboto_mono_20, 0);
373+
lv_obj_set_style_text_align(battLabel, LV_TEXT_ALIGN_RIGHT, 0);
374+
lv_label_set_text_fmt(battLabel, "BATT: %.2fV", boardInfo.BattVoltage/1000.0f);
375+
376+
lv_obj_add_event_cb(battLabel,
377+
[](lv_event_t* e) -> void {
378+
lv_msg_t* m = lv_event_get_msg(e);
379+
lv_obj_t* label = lv_event_get_target(e);
380+
381+
auto info = reinterpret_cast<const TBoardManager::BoardInfo*>(lv_msg_get_payload(m));
382+
lv_label_set_text_fmt(label, "BATT: %.2fV", info->BattVoltage/1000.0f);
383+
},
384+
LV_EVENT_MSG_RECEIVED,
385+
nullptr
386+
);
387+
lv_msg_subsribe_obj(GUI_MESSAGE_UPDATE_BOARD_INFO, battLabel, nullptr);
388+
#endif
347389
}
348390

349391
TGUI::~TGUI()

src/gui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef GUI_H
22
#define GUI_H
33

4+
#include "board_config.h"
45
#include "board_manager.h"
56
#include <Arduino.h>
67
#include <lvgl.h>

src/ssh_handler.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ssh_handler.h"
22
#include "defaults.h"
33
#include "config.h"
4+
#include "board_config.h"
45
#include "board_manager.h"
56
#include "secrets.h"
67
#include "authenticator.h"
@@ -118,6 +119,13 @@ namespace
118119
{
119120
rsp["uptime"] = boardManager.Uptime();
120121
rsp["free_heap"] = boardManager.FreeHeap();
122+
#if HAVE_BATTERY
123+
rsp["batt_voltage"] = boardManager.BattVoltage();
124+
#endif
125+
#if HAVE_TEMP_SENSOR
126+
rsp["core_temp"] = boardManager.CoreTemp();
127+
#endif
128+
121129
return true;
122130
}
123131
}

0 commit comments

Comments
 (0)