Skip to content

Commit

Permalink
Added more sensors (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
buglloc authored Dec 12, 2023
1 parent e780037 commit 3323555
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/board_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
#define IDR_CASE_OPENED_MODE INPUT_PULLUP
// End of intruder config

// Batt config
#define HAVE_BATTERY 1
#define ADC_PIN 4
// End of batt config

// Temp sensor
// Broken for ESP32S3 so far: https://github.com/espressif/esp-idf/issues/8088
// #define HAVE_TEMP_SENSOR 1
// End of core temp sensor

// Log store config
// #define HAVE_SD_CARD 1
#define SD_CARD_SS_PIN 14
Expand Down
83 changes: 83 additions & 0 deletions src/board_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
#include <SD.h>
#include <ArduinoLog.h>

#if HAVE_BATTERY
#include <esp_adc_cal.h>
#endif

#if HAVE_TEMP_SENSOR
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5,0,0)
#include <driver/temp_sensor.h>
#else
#include <driver/temperature_sensor.h>
#endif
#endif

#define LOG_PREFIX "board_manager: "

namespace
Expand All @@ -19,6 +31,10 @@ namespace
static TUIManager& uiManager = TUIManager::Instance();
static TAuthenticator& authenticator = TAuthenticator::Instance();

#if HAVE_TEMP_SENSOR && ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(5,0,0)
temperature_sensor_handle_t temp_sensor;
#endif

void restart()
{
esp_restart();
Expand All @@ -34,6 +50,58 @@ namespace
esp_deep_sleep_start();
}

uint32_t adcBattVoltage()
{
#if !HAVE_BATTERY || !ADC_PIN
return 0;
#else
esp_adc_cal_characteristics_t adc_chars;
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 1100, &adc_chars);
uint32_t v1 = 0, raw = 0;
raw = analogRead(ADC_PIN);
v1 = esp_adc_cal_raw_to_voltage(raw, &adc_chars) * 2;
return v1;
#endif
}

void initEsp32TempSensor()
{
#if !HAVE_TEMP_SENSOR
return;
#else

// https://docs.espressif.com/projects/esp-idf/zh_CN/v4.4.4/esp32s3/api-reference/peripherals/temp_sensor.html
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5,0,0)
temp_sensor_config_t temp_sensor = TSENS_CONFIG_DEFAULT();
temp_sensor_set_config(temp_sensor);
temp_sensor_start();
#else
// https://docs.espressif.com/projects/esp-idf/zh_CN/v5.0.1/esp32s3/api-reference/peripherals/temp_sensor.html
static temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(10, 50);
temperature_sensor_install(&temp_sensor_config, &temp_sensor);
temperature_sensor_enable(temp_sensor);
#endif
#endif
}

float esp32CoreTemp()
{
#if !HAVE_TEMP_SENSOR
return 0.0f;
#else

float tsens_value;
// https://docs.espressif.com/projects/esp-idf/zh_CN/v4.4.4/esp32s3/api-reference/peripherals/temp_sensor.html
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5,0,0)
temp_sensor_read_celsius(&tsens_value);
#else
// https://docs.espressif.com/projects/esp-idf/zh_CN/v5.0.1/esp32s3/api-reference/peripherals/temp_sensor.html
temperature_sensor_get_celsius(temp_sensor, &tsens_value);
#endif

return tsens_value;
#endif
}
}

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

Log.infoln(LOG_PREFIX "setup temp sensor");
initEsp32TempSensor();

Log.infoln(LOG_PREFIX "setup preferences");
if (!TPrefStore::Instance().Begin()) {
Log.errorln(LOG_PREFIX "unable to open preferences");
Expand Down Expand Up @@ -185,6 +256,16 @@ uint32_t TBoardManager::FreeHeap() const
return esp_get_free_heap_size();
}

uint32_t TBoardManager::BattVoltage() const
{
return adcBattVoltage();
}

float TBoardManager::CoreTemp() const
{
return esp32CoreTemp();
}

void TBoardManager::tickRestart()
{
if (restartAt > 0 && Uptime() >= restartAt) {
Expand All @@ -209,4 +290,6 @@ void TBoardManager::tickBoardInfo()

infoUpdatedAt = Uptime();
boardInfo.LocalIP = netManager.LocalIP();
boardInfo.CoreTemp = CoreTemp();
boardInfo.BattVoltage = BattVoltage();
}
17 changes: 16 additions & 1 deletion src/board_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define BB_BOARD_MANAGER_H

#include "config.h"
#include "board_config.h"
#include "net_manager.h"

class TBoardManager
Expand All @@ -21,10 +22,22 @@ class TBoardManager
{
BoardState State;
IPAddress LocalIP;
uint32_t BattVoltage;
float CoreTemp;

bool operator==(const BoardInfo& info) const
{
return State == info.State && LocalIP == info.LocalIP;
return (
State == info.State &&
LocalIP == info.LocalIP &&
#if HAVE_BATTERY
abs((int)BattVoltage - (int)info.BattVoltage) < 50 &&
#endif
#if HAVE_TEMP_SENSOR
fabs(CoreTemp - info.CoreTemp) < 0.1 &&
#endif
true
);
}
};

Expand All @@ -40,6 +53,8 @@ class TBoardManager
const TConfig& RuntimeConfig() const;
uint32_t Uptime() const;
uint32_t FreeHeap() const;
uint32_t BattVoltage() const;
float CoreTemp() const;

private:
TBoardManager() = default;
Expand Down
44 changes: 43 additions & 1 deletion src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ void TGUI::ShowScreenIdle(TBoardManager::BoardInfo boardInfo)
lv_obj_t* ipLabel = lv_label_create(cont);
lv_obj_align(ipLabel, LV_ALIGN_BOTTOM_LEFT, 24, -24);
lv_obj_set_style_text_font(ipLabel, &font_roboto_mono_20, 0);
lv_obj_set_style_text_align(ipLabel, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_style_text_align(ipLabel, LV_TEXT_ALIGN_LEFT, 0);
lv_label_set_text_fmt(ipLabel, "IP: %s", boardInfo.LocalIP.toString());

lv_obj_add_event_cb(ipLabel,
Expand All @@ -344,6 +344,48 @@ void TGUI::ShowScreenIdle(TBoardManager::BoardInfo boardInfo)
nullptr
);
lv_msg_subsribe_obj(GUI_MESSAGE_UPDATE_BOARD_INFO, ipLabel, nullptr);

#if HAVE_TEMP_SENSOR
lv_obj_t* tempLabel = lv_label_create(cont);
lv_obj_align(tempLabel, LV_ALIGN_BOTTOM_MID, 0, -24);
lv_obj_set_style_text_font(tempLabel, &font_roboto_mono_20, 0);
lv_obj_set_style_text_align(tempLabel, LV_TEXT_ALIGN_CENTER, 0);
lv_label_set_text_fmt(tempLabel, "Core: %.2f°C", boardInfo.CoreTemp);

lv_obj_add_event_cb(tempLabel,
[](lv_event_t* e) -> void {
lv_msg_t* m = lv_event_get_msg(e);
lv_obj_t* label = lv_event_get_target(e);

auto info = reinterpret_cast<const TBoardManager::BoardInfo*>(lv_msg_get_payload(m));
lv_label_set_text_fmt(label, "BATT: %.2f°C", info->CoreTemp);
},
LV_EVENT_MSG_RECEIVED,
nullptr
);
lv_msg_subsribe_obj(GUI_MESSAGE_UPDATE_BOARD_INFO, tempLabel, nullptr);
#endif

#if HAVE_BATTERY
lv_obj_t* battLabel = lv_label_create(cont);
lv_obj_align(battLabel, LV_ALIGN_BOTTOM_RIGHT, -24, -24);
lv_obj_set_style_text_font(battLabel, &font_roboto_mono_20, 0);
lv_obj_set_style_text_align(battLabel, LV_TEXT_ALIGN_RIGHT, 0);
lv_label_set_text_fmt(battLabel, "BATT: %.2fV", boardInfo.BattVoltage/1000.0f);

lv_obj_add_event_cb(battLabel,
[](lv_event_t* e) -> void {
lv_msg_t* m = lv_event_get_msg(e);
lv_obj_t* label = lv_event_get_target(e);

auto info = reinterpret_cast<const TBoardManager::BoardInfo*>(lv_msg_get_payload(m));
lv_label_set_text_fmt(label, "BATT: %.2fV", info->BattVoltage/1000.0f);
},
LV_EVENT_MSG_RECEIVED,
nullptr
);
lv_msg_subsribe_obj(GUI_MESSAGE_UPDATE_BOARD_INFO, battLabel, nullptr);
#endif
}

TGUI::~TGUI()
Expand Down
1 change: 1 addition & 0 deletions src/gui.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef GUI_H
#define GUI_H

#include "board_config.h"
#include "board_manager.h"
#include <Arduino.h>
#include <lvgl.h>
Expand Down
8 changes: 8 additions & 0 deletions src/ssh_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ssh_handler.h"
#include "defaults.h"
#include "config.h"
#include "board_config.h"
#include "board_manager.h"
#include "secrets.h"
#include "authenticator.h"
Expand Down Expand Up @@ -118,6 +119,13 @@ namespace
{
rsp["uptime"] = boardManager.Uptime();
rsp["free_heap"] = boardManager.FreeHeap();
#if HAVE_BATTERY
rsp["batt_voltage"] = boardManager.BattVoltage();
#endif
#if HAVE_TEMP_SENSOR
rsp["core_temp"] = boardManager.CoreTemp();
#endif

return true;
}
}
Expand Down

0 comments on commit 3323555

Please sign in to comment.