From 71dafb39ab613d66b375866d6d6c74f16db19bc3 Mon Sep 17 00:00:00 2001 From: LoSk-p Date: Wed, 5 Jun 2024 14:52:18 +0300 Subject: [PATCH] add db meter --- airrohr-firmware/airrohr-cfg.h | 3 ++ airrohr-firmware/airrohr-cfg.h.py | 1 + airrohr-firmware/airrohr-firmware.ino | 59 +++++++++++++++++++++++++++ airrohr-firmware/dbmeter_regs.h | 21 ++++++++++ airrohr-firmware/ext_def.h | 3 ++ airrohr-firmware/html-content.h | 2 + airrohr-firmware/intl_en.h | 1 + airrohr-firmware/intl_ru.h | 1 + airrohr-firmware/platformio.ini | 20 +++++---- 9 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 airrohr-firmware/dbmeter_regs.h diff --git a/airrohr-firmware/airrohr-cfg.h b/airrohr-firmware/airrohr-cfg.h index d9d667ac..548b1754 100644 --- a/airrohr-firmware/airrohr-cfg.h +++ b/airrohr-firmware/airrohr-cfg.h @@ -36,6 +36,7 @@ enum ConfigShapeId { Config_www_basicauth_enabled, Config_dht_read, Config_htu21d_read, + Config_dbmeter_read, Config_ppd_read, Config_sds_read, Config_gc_read, @@ -109,6 +110,7 @@ static constexpr char CFG_KEY_FS_PWD[] PROGMEM = "fs_pwd"; static constexpr char CFG_KEY_WWW_BASICAUTH_ENABLED[] PROGMEM = "www_basicauth_enabled"; static constexpr char CFG_KEY_DHT_READ[] PROGMEM = "dht_read"; static constexpr char CFG_KEY_HTU21D_READ[] PROGMEM = "htu21d_read"; +static constexpr char CFG_KEY_DBMETER_READ[] PROGMEM = "dbmeter_read"; static constexpr char CFG_KEY_PPD_READ[] PROGMEM = "ppd_read"; static constexpr char CFG_KEY_SDS_READ[] PROGMEM = "sds_read"; static constexpr char CFG_KEY_GC_READ[] PROGMEM = "gc_read"; @@ -182,6 +184,7 @@ static constexpr ConfigShapeEntry configShape[] PROGMEM = { { Config_Type_Bool, 0, CFG_KEY_WWW_BASICAUTH_ENABLED, &cfg::www_basicauth_enabled }, { Config_Type_Bool, 0, CFG_KEY_DHT_READ, &cfg::dht_read }, { Config_Type_Bool, 0, CFG_KEY_HTU21D_READ, &cfg::htu21d_read }, + { Config_Type_Bool, 0, CFG_KEY_DBMETER_READ, &cfg::dbmeter_read }, { Config_Type_Bool, 0, CFG_KEY_PPD_READ, &cfg::ppd_read }, { Config_Type_Bool, 0, CFG_KEY_SDS_READ, &cfg::sds_read }, { Config_Type_Bool, 0, CFG_KEY_GC_READ, &cfg::gc_read }, diff --git a/airrohr-firmware/airrohr-cfg.h.py b/airrohr-firmware/airrohr-cfg.h.py index 24b879c1..9ac1b5ac 100755 --- a/airrohr-firmware/airrohr-cfg.h.py +++ b/airrohr-firmware/airrohr-cfg.h.py @@ -12,6 +12,7 @@ Bool www_basicauth_enabled Bool dht_read Bool htu21d_read +Bool dbmeter_read Bool ppd_read Bool sds_read Bool gc_read diff --git a/airrohr-firmware/airrohr-firmware.ino b/airrohr-firmware/airrohr-firmware.ino index af54f6f3..99b9d72d 100644 --- a/airrohr-firmware/airrohr-firmware.ino +++ b/airrohr-firmware/airrohr-firmware.ino @@ -114,6 +114,7 @@ String SOFTWARE_VERSION(SOFTWARE_VERSION_STR); #include "./bmx280_i2c.h" #include "./sps30_i2c.h" #include "./dnms_i2c.h" +#include "./dbmeter_regs.h" #include "./intl.h" @@ -162,6 +163,7 @@ namespace cfg { // (in)active sensors bool dht_read = DHT_READ; bool htu21d_read = HTU21D_READ; + bool dbmeter_read = DBMETER_READ; bool ppd_read = PPD_READ; bool sds_read = SDS_READ; bool gc_read = GC_READ; @@ -258,6 +260,7 @@ LoggerConfig loggerConfigs[LoggerCount]; long int sample_count = 0; bool htu21d_init_failed = false; +bool dbmeter_init_failed = false; bool bmp_init_failed = false; bool bmx280_init_failed = false; bool sht3x_init_failed = false; @@ -412,6 +415,8 @@ float last_value_HTU21D_H = -1.0; float last_value_SHT3X_T = -128.0; float last_value_SHT3X_H = -1.0; +uint8_t last_value_DBMETER = 0; + uint32_t sds_pm10_sum = 0; uint32_t sds_pm25_sum = 0; uint32_t sds_val_count = 0; @@ -629,6 +634,21 @@ static void initSensorCCS811() { } } +/***************************************************************** + * init DB Meter sensor * + *****************************************************************/ + +static void initDBMeter() { + // Read version register + uint8_t version = dbmeter_readreg(DBM_REG_VERSION); + if (version != 255) { + debug_outln_info(F("DB Meter version = "), String(version)); + } else { + debug_outln_info(F("Check DB Meter wiring...")); + dbmeter_init_failed = true; + } +} + /***************************************************************** * read SDS011 sensor serial and firmware date * *****************************************************************/ @@ -1340,6 +1360,7 @@ static void webserver_config_send_body_get(String& page_content) { add_form_checkbox_sensor(Config_dht_read, FPSTR(INTL_DHT22)); add_form_checkbox_sensor(Config_htu21d_read, FPSTR(INTL_HTU21D)); + add_form_checkbox_sensor(Config_dbmeter_read, FPSTR(INTL_DBMETER)); add_form_checkbox_sensor(Config_bmx280_read, FPSTR(INTL_BMX280)); add_form_checkbox_sensor(Config_sht3x_read, FPSTR(INTL_SHT3X)); @@ -1726,6 +1747,10 @@ static void webserver_values() { add_table_h_value(FPSTR(SENSORS_HTU21D), FPSTR(INTL_HUMIDITY), last_value_HTU21D_H); page_content += FPSTR(EMPTY_ROW); } + if (cfg::dbmeter_read) { + add_table_t_value(FPSTR(SENSORS_DBMETER), FPSTR(INTL_TEMPERATURE), last_value_DBMETER); + page_content += FPSTR(EMPTY_ROW); + } if (cfg::bmp_read) { add_table_t_value(FPSTR(SENSORS_BMP180), FPSTR(INTL_TEMPERATURE), last_value_BMP_T); add_table_value(FPSTR(SENSORS_BMP180), FPSTR(INTL_PRESSURE), check_display_value(last_value_BMP_P / 100.0f, (-1 / 100.0f), 2, 0), unit_P); @@ -2270,6 +2295,7 @@ static void wifiConfig() { debug_outln_info_bool(F("DHT: "), cfg::dht_read); debug_outln_info_bool(F("DS18B20: "), cfg::ds18b20_read); debug_outln_info_bool(F("HTU21D: "), cfg::htu21d_read); + debug_outln_info_bool(F("DBMETER: "), cfg::dbmeter_read); debug_outln_info_bool(F("BMP: "), cfg::bmp_read); debug_outln_info_bool(F("DNMS: "), cfg::dnms_read); debug_outln_info_bool(F("CCS811: "), cfg::ccs811_read); @@ -2713,6 +2739,34 @@ static void fetchSensorHTU21D(String& s) { debug_outln_verbose(FPSTR(DBG_TXT_END_READING), FPSTR(SENSORS_HTU21D)); } +/***************************************************************** + * read DB meter sensor values * + *****************************************************************/ +static void fetchSensorDBMeter(String& s) { + debug_outln_verbose(FPSTR(DBG_TXT_START_READING), FPSTR(SENSORS_DBMETER)); + Wire.setClock(10000); + uint8_t db = dbmeter_readreg(DBM_REG_DECIBEL); + if (db == 255) { + last_value_DBMETER = 0; + debug_outln_error(F("DB Meter read failed")); + } else { + last_value_DBMETER = db; + add_Value2Json(s, F("DB_Meter"), FPSTR(DBG_TXT_DECIBEL), last_value_DBMETER); + } + Wire.setClock(100000); + debug_outln_info(FPSTR(DBG_TXT_SEP)); + debug_outln_verbose(FPSTR(DBG_TXT_END_READING), FPSTR(SENSORS_DBMETER)); +} + +uint8_t dbmeter_readreg (uint8_t regaddr) { + Wire.beginTransmission(DBM_ADDR); + Wire.write(regaddr); + Wire.endTransmission(); + Wire.requestFrom(DBM_ADDR, 1); + delay(10); + return Wire.read(); +} + /***************************************************************** * read BMP180 sensor values * *****************************************************************/ @@ -4486,6 +4540,11 @@ static void powerOnTestSensors() { } } + if (cfg::dbmeter_read) { + debug_outln_info(F("Read DB Meter...")); + initDBMeter(); + } + if (cfg::bmp_read) { debug_outln_info(F("Read BMP...")); if (!bmp.begin()) { diff --git a/airrohr-firmware/dbmeter_regs.h b/airrohr-firmware/dbmeter_regs.h new file mode 100644 index 00000000..465d191e --- /dev/null +++ b/airrohr-firmware/dbmeter_regs.h @@ -0,0 +1,21 @@ +// I2C address for the module +#define DBM_ADDR 0x48 + +// Device registers +#define DBM_REG_VERSION 0x00 +#define DBM_REG_ID3 0x01 +#define DBM_REG_ID2 0x02 +#define DBM_REG_ID1 0x03 +#define DBM_REG_ID0 0x04 +#define DBM_REG_SCRATCH 0x05 +#define DBM_REG_CONTROL 0x06 +#define DBM_REG_TAVG_HIGH 0x07 +#define DBM_REG_TAVG_LOW 0x08 +#define DBM_REG_RESET 0x09 +#define DBM_REG_DECIBEL 0x0A +#define DBM_REG_MIN 0x0B +#define DBM_REG_MAX 0x0C +#define DBM_REG_THR_MIN 0x0D +#define DBM_REG_THR_MAX 0x0E +#define DBM_REG_HISTORY_0 0x14 +#define DBM_REG_HISTORY_99 0x77 \ No newline at end of file diff --git a/airrohr-firmware/ext_def.h b/airrohr-firmware/ext_def.h index 4fda03a8..785ded68 100644 --- a/airrohr-firmware/ext_def.h +++ b/airrohr-firmware/ext_def.h @@ -249,6 +249,9 @@ static const char MEASUREMENT_NAME_INFLUX[] PROGMEM = "feinstaub"; // Geiger Counter #define GC_READ 0 +// Becibel Meter +#define DBMETER_READ 1 + // SDS011, the more expensive version of the particle sensor #define SDS_READ 1 #define SDS_API_PIN 1 diff --git a/airrohr-firmware/html-content.h b/airrohr-firmware/html-content.h index 85def83d..b04fbed5 100644 --- a/airrohr-firmware/html-content.h +++ b/airrohr-firmware/html-content.h @@ -6,6 +6,7 @@ const char TXT_CONTENT_TYPE_TEXT_PLAIN[] PROGMEM = "text/plain"; const char TXT_CONTENT_TYPE_IMAGE_PNG[] PROGMEM = "image/png"; const char DBG_TXT_TEMPERATURE[] PROGMEM = "Temperature (°C): "; +const char DBG_TXT_DECIBEL[] PROGMEM = "Noise Level (DB): "; const char DBG_TXT_HUMIDITY[] PROGMEM = "Humidity (%): "; const char DBG_TXT_PRESSURE[] PROGMEM = "Pressure (hPa): "; const char DBG_TXT_START_READING[] PROGMEM = "R/ "; @@ -34,6 +35,7 @@ const char SENSORS_SPS30[] PROGMEM = "Sensirion SPS30"; const char SENSORS_DHT22[] PROGMEM = "DHT22"; const char SENSORS_DS18B20[] PROGMEM = "DS18B20"; const char SENSORS_HTU21D[] PROGMEM = "HTU21D"; +const char SENSORS_DBMETER[] PROGMEM = "Decibel Meter"; const char SENSORS_SHT3X[] PROGMEM = "SHT3x"; const char SENSORS_BMP180[] PROGMEM = "BMP180"; const char SENSORS_BME280[] PROGMEM = "BME280"; diff --git a/airrohr-firmware/intl_en.h b/airrohr-firmware/intl_en.h index 0b65ae86..492d875c 100644 --- a/airrohr-firmware/intl_en.h +++ b/airrohr-firmware/intl_en.h @@ -22,6 +22,7 @@ const char INTL_SENSORS[] PROGMEM = "Sensors"; const char INTL_MORE_SENSORS[] PROGMEM = "More Sensors"; const char INTL_SDS011[] PROGMEM = "SDS011 ({pm})"; const char INTL_GC[] PROGMEM = "Geiger counter"; +const char INTL_DBMETER[] PROGMEM = "Noise Level Sensor, (DB)"; const char INTL_PMS[] PROGMEM = "Plantower PMS(1,3,5,6,7)003 ({pm})"; const char INTL_HPM[] PROGMEM = "Honeywell PM ({pm})"; const char INTL_NPM[] PROGMEM = "Tera Sensor Next PM ({pm})"; diff --git a/airrohr-firmware/intl_ru.h b/airrohr-firmware/intl_ru.h index 5e24d84c..f232e1ac 100644 --- a/airrohr-firmware/intl_ru.h +++ b/airrohr-firmware/intl_ru.h @@ -22,6 +22,7 @@ const char INTL_SENSORS[] PROGMEM = "Датчики"; const char INTL_MORE_SENSORS[] PROGMEM = "Больше датчиков"; const char INTL_SDS011[] PROGMEM = "SDS011 ({pm})"; const char INTL_GC[] PROGMEM = "Счетчик Гейгера ({cpm})"; +const char INTL_DBMETER[] PROGMEM = "Датчик уровня шума, (дб)"; const char INTL_PMS[] PROGMEM = "Plantower PMS(1,3,5,6,7)003 ({pm})"; const char INTL_HPM[] PROGMEM = "Honeywell PM ({pm})"; const char INTL_NPM[] PROGMEM = "Tera Sensor Next PM ({pm})"; diff --git a/airrohr-firmware/platformio.ini b/airrohr-firmware/platformio.ini index 2b793fd4..4ce89b7f 100644 --- a/airrohr-firmware/platformio.ini +++ b/airrohr-firmware/platformio.ini @@ -43,17 +43,19 @@ board_build.f_cpu = 160000000L ; (like OneWire, LiquidCrystal_I2C and TinyGPSPlus) lib_deps_external = - 1@2.3.5 ; OneWire - 576@1.1.4 ; LiquidCrystal_I2C - Adafruit BMP085 Library@1.0.1 - Adafruit HTU21DF Library@1.0.2 - Adafruit SHT31 Library@1.1.6 - bblanchon/ArduinoJson@6.16.1 - milesburton/DallasTemperature@3.8.0 - ESP8266_SSD1306@4.1.0 - 1655@1.0.2 ; TinyGPSPlus, formerly this was referenced as mikalhart/TinyGPSPlus#v0.95 + paulstoffregen/OneWire@2.3.7 + marcoschwartz/LiquidCrystal_I2C@1.1.4 + adafruit/Adafruit BMP085 Library@1.0.1 + adafruit/Adafruit HTU21DF Library@1.0.5 + https://github.com/adafruit/Adafruit_SHT31.git#1.2.0 + milesburton/DallasTemperature@3.9.1 + sparkfun/SparkFun SCD30 Arduino Library @ ^1.0.13 + bblanchon/ArduinoJson@6.18.3 + ThingPulse/ESP8266 and ESP32 OLED driver for SSD1306 displays @ ^4.2.1 + mikalhart/TinyGPSPlus@1.0.2 climateguard/ClimateGuard RadSens@^1.1.1 sparkfun/SparkFun CCS811 Arduino Library@^2.0.3 + arduino-libraries/ArduinoHttpClient@^0.6.0 ; system libraries from platform -> no version number lib_deps_esp8266_platform =