From 05abbf0b558d3abcadc27fe64b8a2f1b40fd6590 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Tue, 13 Jul 2021 15:04:21 +0200 Subject: [PATCH] Added BMP085 sensor --- .gitmodules | 3 + components/Adafruit_BMP085_Unified | 1 + main/CMakeLists.txt | 2 +- main/main.cpp | 147 ++++++++++++++++++++++------- 4 files changed, 117 insertions(+), 36 deletions(-) create mode 160000 components/Adafruit_BMP085_Unified diff --git a/.gitmodules b/.gitmodules index f4d2fa0..890f328 100644 --- a/.gitmodules +++ b/.gitmodules @@ -31,3 +31,6 @@ [submodule "components/Adafruit_TSL2561"] path = components/Adafruit_TSL2561 url = git@github.com:0xFEEDC0DE64/Adafruit_TSL2561.git +[submodule "components/Adafruit_BMP085_Unified"] + path = components/Adafruit_BMP085_Unified + url = git@github.com:0xFEEDC0DE64/Adafruit_BMP085_Unified.git diff --git a/components/Adafruit_BMP085_Unified b/components/Adafruit_BMP085_Unified new file mode 160000 index 0000000..531b734 --- /dev/null +++ b/components/Adafruit_BMP085_Unified @@ -0,0 +1 @@ +Subproject commit 531b7344690833843364f4cf80bebe2013a4b386 diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 6d52278..ed2869b 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -11,7 +11,7 @@ set(sources set(dependencies freertos nvs_flash esp_http_server esp_https_ota mdns app_update esp_system mqtt arduino-esp32 cpputils date espchrono espcpputils espwifistack expected fmt - Adafruit_Sensor Adafruit_TSL2561 + Adafruit_BMP085_Unified Adafruit_TSL2561 ) idf_component_register( diff --git a/main/main.cpp b/main/main.cpp index 88886f6..4ba15f0 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -26,6 +26,8 @@ // 3rdparty lib includes #include #include +#include +#include // local includes #include "espwifistack.h" @@ -50,6 +52,11 @@ constexpr const std::string_view topic_switch_status = "dahoam/wohnzimmer/lichts constexpr const std::string_view topic_lux_availability = "dahoam/wohnzimmer/lux1/available"; constexpr const std::string_view topic_lux_value = "dahoam/wohnzimmer/lux1/value"; +constexpr const std::string_view topic_bmp085_availability = "dahoam/wohnzimmer/bmp085_1/available"; +constexpr const std::string_view topic_bmp085_pressure = "dahoam/wohnzimmer/bmp085_1/pressure"; +constexpr const std::string_view topic_bmp085_temperature = "dahoam/wohnzimmer/bmp085_1/temperature"; +constexpr const std::string_view topic_bmp085_altitude = "dahoam/wohnzimmer/bmp085_1/altitude"; + std::atomic mqttConnected; espcpputils::mqtt_client mqttClient; @@ -60,10 +67,14 @@ std::atomic switchState; //espchrono::millis_clock::time_point lastSwitchToggle; Adafruit_TSL2561_Unified tsl{TSL2561_ADDR_FLOAT, 12345}; +Adafruit_BMP085_Unified bmp{10085}; -bool tslInitialized; +bool tslInitialized{}; +bool bmpInitialized{}; std::optional lastTslValue; +struct BmpValue { float pressure; float temperature; float altitude; }; +std::optional lastBmpValue; esp_err_t webserver_root_handler(httpd_req_t *req); esp_err_t webserver_on_handler(httpd_req_t *req); @@ -317,9 +328,9 @@ extern "C" void app_main() ESP_LOGI(TAG, "Sensor: %s", sensor.name); ESP_LOGI(TAG, "Driver Ver: %i", sensor.version); ESP_LOGI(TAG, "Unique ID: %i", sensor.sensor_id); - ESP_LOGI(TAG, "Max Value: %f lux", sensor.max_value); - ESP_LOGI(TAG, "Min Value: %f lux", sensor.min_value); - ESP_LOGI(TAG, "Resolution: %f lux", sensor.resolution); + ESP_LOGI(TAG, "Max Value: %.1f lux", sensor.max_value); + ESP_LOGI(TAG, "Min Value: %.1f lux", sensor.min_value); + ESP_LOGI(TAG, "Resolution: %.1f lux", sensor.resolution); ESP_LOGI(TAG, "------------------------------------"); ESP_LOGI(TAG, ""); @@ -341,6 +352,25 @@ extern "C" void app_main() } } + { + ESP_LOGI(TAG, "calling bmp.begin()..."); + bmpInitialized = bmp.begin(); + ESP_LOGI(TAG, "finished with %s", bmpInitialized ? "true" : "false"); + + if (bmpInitialized) { + sensor_t sensor = bmp.getSensor(); + ESP_LOGI(TAG, "------------------------------------"); + ESP_LOGI(TAG, "Sensor: %s", sensor.name); + ESP_LOGI(TAG, "Driver Ver: %i", sensor.version); + ESP_LOGI(TAG, "Unique ID: %i", sensor.sensor_id); + ESP_LOGI(TAG, "Max Value: %.1f hPa", sensor.max_value); + ESP_LOGI(TAG, "Min Value: %.1f hPa", sensor.min_value); + ESP_LOGI(TAG, "Resolution: %.1f hPa", sensor.resolution); + ESP_LOGI(TAG, "------------------------------------"); + ESP_LOGI(TAG, ""); + } + } + while (true) { wifi_stack::update(wifiConfig); @@ -367,33 +397,77 @@ extern "C" void app_main() if (std::optional event = tsl.getEvent()) { /* Display the results (light is measured in lux) */ if (event->light) { - ESP_LOGW(TAG, "%f lux", event->light); + const float tslValue = event->light; + ESP_LOGI(TAG, "read tsl: %.1f lux", tslValue); if (mqttClient && mqttConnected) { if (!lastTslValue) mqttVerbosePub(topic_lux_availability, "online", 0, 1); - if (!lastTslValue || *lastTslValue != event->light) - mqttVerbosePub(topic_lux_value, std::to_string(event->light), 0, 1); + if (!lastTslValue || fmt::format("{:.1f}", *lastTslValue) != fmt::format("{:.1f}", tslValue)) + mqttVerbosePub(topic_lux_value, fmt::format("{:.1f}", tslValue), 0, 1); } - lastTslValue = event->light; + lastTslValue = tslValue; } else { /* If event.light = 0 lux the sensor is probably saturated * and no reliable data could be generated! */ - ESP_LOGD(TAG, "Sensor overload %f", event->light); + ESP_LOGD(TAG, "tsl sensor overload %f", event->light); } } else { ESP_LOGW(TAG, "tsl failed"); - if (lastTslValue) { - if (mqttClient && mqttConnected) - mqttVerbosePub(topic_lux_availability, "offline", 0, 1); - lastTslValue = std::nullopt; - } + goto tslOffline; + } + } else { + tslOffline: + if (lastTslValue) { + if (mqttClient && mqttConnected) + mqttVerbosePub(topic_lux_availability, "offline", 0, 1); + lastTslValue = std::nullopt; + } + } + + if (bmpInitialized) { + if (std::optional event = bmp.getEvent()) { + if (event->pressure) { + BmpValue bmpValue { .pressure = event->pressure }; + ESP_LOGI(TAG, "read bmp Pressure: %.1f hPa", bmpValue.pressure); + + bmp.getTemperature(&bmpValue.temperature); + ESP_LOGI(TAG, "read bmp Temperature: %.1f C", bmpValue.temperature); + + /* Then convert the atmospheric pressure, and SLP to altitude */ + /* Update this next line with the current SLP for better results */ + constexpr const float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA; + bmpValue.altitude = bmp.pressureToAltitude(seaLevelPressure, event->pressure); + ESP_LOGI(TAG, "read bmp Altitude: %.1f m", bmpValue.altitude); + + if (mqttClient && mqttConnected) { + if (!lastBmpValue) + mqttVerbosePub(topic_bmp085_availability, "online", 0, 1); + if (!lastBmpValue || fmt::format("{:.1f}", lastBmpValue->pressure) != fmt::format("{:.1f}", bmpValue.pressure)) + mqttVerbosePub(topic_bmp085_pressure, fmt::format("{:.1f}", bmpValue.pressure), 0, 1); + if (!lastBmpValue || fmt::format("{:.1f}", lastBmpValue->temperature) != fmt::format("{:.1f}", bmpValue.temperature)) + mqttVerbosePub(topic_bmp085_temperature, fmt::format("{:.1f}", bmpValue.temperature), 0, 1); + if (!lastBmpValue || fmt::format("{:.1f}", lastBmpValue->altitude) != fmt::format("{:.1f}", bmpValue.altitude)) + mqttVerbosePub(topic_bmp085_altitude, fmt::format("{:.1f}", bmpValue.altitude), 0, 1); + } + + lastBmpValue = bmpValue; + } else { + ESP_LOGW(TAG, "bmp sensor error"); + goto bmpOffline; + } + } else { + ESP_LOGW(TAG, "bmp failed"); + goto bmpOffline; + } + } else { + bmpOffline: + if (lastBmpValue) { + if (mqttClient && mqttConnected) + mqttVerbosePub(topic_bmp085_availability, "offline", 0, 1); + lastBmpValue = std::nullopt; } - } else if (lastTslValue) { - if (mqttClient && mqttConnected) - mqttVerbosePub(topic_lux_availability, "offline", 0, 1); - lastTslValue = std::nullopt; } #if defined(CONFIG_ESP_TASK_WDT_PANIC) || defined(CONFIG_ESP_TASK_WDT) @@ -440,17 +514,16 @@ esp_err_t webserver_root_handler(httpd_req_t *req) "off
\n" "toggle
\n" "reboot
\n" - "
\n" - "Lamp: "; - body += lampState ? "ON" : "OFF"; - body += "
\n" - "Switch: "; - body += switchState ? "ON" : "OFF"; - body += "
\n"; + "
\n"; + body += fmt::format("Lamp: {}
\n", lampState ? "ON" : "OFF"); + body += fmt::format("Switch: {}
\n", switchState ? "ON" : "OFF"); if (lastTslValue) { - body += "Brightness: "; - body += std::to_string(*lastTslValue); - body += " lux
\n"; + body += fmt::format("Brightness: {:.1f} lux
\n", *lastTslValue); + } + if (lastBmpValue) { + body += fmt::format("BMP085 Pressure: {:.1f} lux
\n", lastBmpValue->pressure); + body += fmt::format("BMP085 Temperature: {:.1f} C
\n", lastBmpValue->temperature); + body += fmt::format("BMP085 Altitude: {:.1f} m
\n", lastBmpValue->altitude); } CALL_AND_EXIT_ON_ERROR(httpd_resp_set_type, req, "text/html") @@ -543,11 +616,15 @@ void mqttEventHandler(void *event_handler_arg, esp_event_base_t event_base, int3 mqttVerbosePub(topic_switch_availability, "online", 0, 1); mqttVerbosePub(topic_switch_status, switchState.load() ? "ON" : "OFF", 0, 1); - if (lastTslValue) { - mqttVerbosePub(topic_lux_availability, "online", 0, 1); - mqttVerbosePub(topic_lux_value, std::to_string(*lastTslValue).c_str(), 0, 1); - } else { - mqttVerbosePub(topic_lux_availability, "offline", 0, 1); + mqttVerbosePub(topic_lux_availability, lastTslValue ? "online" : "offline", 0, 1); + if (lastTslValue) + mqttVerbosePub(topic_lux_value, fmt::format("{:.1f}", *lastTslValue), 0, 1); + + mqttVerbosePub(topic_bmp085_availability, lastBmpValue ? "online" : "offline", 0, 1); + if (lastBmpValue) { + mqttVerbosePub(topic_bmp085_pressure, fmt::format("{:.1f}", lastBmpValue->pressure), 0, 1); + mqttVerbosePub(topic_bmp085_temperature, fmt::format("{:.1f}", lastBmpValue->temperature), 0, 1); + mqttVerbosePub(topic_bmp085_altitude, fmt::format("{:.1f}", lastBmpValue->altitude), 0, 1); } mqttClient.subscribe(topic_lamp_set, 0); @@ -581,9 +658,9 @@ void mqttEventHandler(void *event_handler_arg, esp_event_base_t event_base, int3 if (topic == topic_lamp_set) { - lampState = value == "ON"; + bool newState = (lampState = (value == "ON")); if (mqttClient && mqttConnected) - mqttVerbosePub(topic_lamp_status, lampState ? "ON" : "OFF", 0, 1); + mqttVerbosePub(topic_lamp_status, newState ? "ON" : "OFF", 0, 1); } break;