diff --git a/data_svelte/items.json b/data_svelte/items.json index c02fdb17..2437d262 100644 --- a/data_svelte/items.json +++ b/data_svelte/items.json @@ -53,7 +53,7 @@ "type": "Reading", "subtype": "Sht20h", "id": "Hum2", - "widget": "anydataTmp", + "widget": "anydataHum", "page": "Сенсоры", "descr": "Влажность", "int": 15 @@ -77,11 +77,35 @@ "type": "Reading", "subtype": "Dht1122h", "id": "Hum3", - "widget": "anydataTmp", + "widget": "anydataHum", "page": "Сенсоры", "descr": "Влажность", "int": 15, "pin": 0, "senstype": "dht11" + }, + { + "name": "7. Cенсор температуры Bmp280", + "num": 7, + "type": "Reading", + "subtype": "Bmp280t", + "id": "tmp3", + "widget": "anydataTmp", + "page": "Сенсоры", + "descr": "Температура", + "int": 15, + "addr": "0x76" + }, + { + "name": "8. Cенсор давления Bmp280", + "num": 8, + "type": "Reading", + "subtype": "Bmp280p", + "id": "Press3", + "widget": "anydataPress", + "page": "Сенсоры", + "descr": "Давление", + "int": 15, + "addr": "0x76" } ] \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 1cdb8748..5fb418d2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -30,6 +30,7 @@ lib_deps = milesburton/DallasTemperature@^3.9.1 robtillaart/SHT2x@^0.1.1 beegee-tokyo/DHT sensor library for ESPx + adafruit/Adafruit BMP280 Library monitor_filters = esp8266_exception_decoder upload_speed = 921600 monitor_speed = 115200 @@ -45,6 +46,7 @@ lib_deps = milesburton/DallasTemperature@^3.9.1 robtillaart/SHT2x@^0.1.1 beegee-tokyo/DHT sensor library for ESPx + adafruit/Adafruit BMP280 Library monitor_filters = esp32_exception_decoder upload_speed = 921600 monitor_speed = 115200 diff --git a/src/classes/IoTSensor.cpp b/src/classes/IoTSensor.cpp index 3781d4ec..611b5530 100644 --- a/src/classes/IoTSensor.cpp +++ b/src/classes/IoTSensor.cpp @@ -52,12 +52,21 @@ void IoTSensor::regEvent(float value, String consoleInfo = "") { if (_multiply) value = value * _multiply; if (_plus) value = value + _multiply; if (_round != 0) { - if (value > 0) value = (int)(value * _round + 0.5) / _round; - if (value < 0) value = (int)(value * _round - 0.5) / _round; + if (value > 0) { + value = (int)(value * _round + 0.5F); + value = value / _round; + } + if (value < 0) { + value = (int)(value * _round - 0.5F); + value = value / _round; + } } if (_map1 != _map2) value = map(value, _map1, _map2, _map3, _map4); - regEvent((String)value, consoleInfo); + // убираем лишние нули + char buf[20]; + sprintf(buf, "%g", value); + regEvent((String)buf, consoleInfo); } void IoTSensor::doByInterval() {} diff --git a/src/modules/API.cpp b/src/modules/API.cpp index 7df50ce9..9628fd87 100644 --- a/src/modules/API.cpp +++ b/src/modules/API.cpp @@ -6,6 +6,7 @@ void* getAPI_AnalogAdc(String subtype, String params); void* getAPI_Ds18b20(String subtype, String params); void* getAPI_Sht20(String subtype, String params); void* getAPI_Dht1122(String subtype, String params); +void* getAPI_Bmp280(String subtype, String params); //============================================================================================ void* getAPI(String subtype, String params) { @@ -16,6 +17,7 @@ void* getAPI(String subtype, String params) { if ((tmpAPI = getAPI_Ds18b20(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Sht20(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Dht1122(subtype, params)) != nullptr) return tmpAPI; + if ((tmpAPI = getAPI_Bmp280(subtype, params)) != nullptr) return tmpAPI; //================================================================================================================ return nullptr; diff --git a/src/modules/Bmp280.cpp b/src/modules/Bmp280.cpp new file mode 100644 index 00000000..4f264706 --- /dev/null +++ b/src/modules/Bmp280.cpp @@ -0,0 +1,73 @@ +/****************************************************************** + Used Adafruit BMP280 Driver (Barometric Pressure Sensor) + Support for BMP280 + https://github.com/adafruit/Adafruit_BMP280_Library + ******************************************************************/ + + +#include "Global.h" +#include "Classes/IoTSensor.h" + +#include +#include + + +std::map bmps; + +class Bmp280t : public IoTSensor { + private: + Adafruit_BMP280* _bmp; + + public: + Bmp280t(Adafruit_BMP280* bmp, String parameters): IoTSensor(parameters) { + _bmp = bmp; + } + + void doByInterval() { + float value = _bmp->readTemperature(); + if (String(value) != "nan") regEvent(value, "Bmp280t"); + else SerialPrint("E", "Sensor DHTt", "Error"); + } + + ~Bmp280t(); +}; + + +class Bmp280p : public IoTSensor { + private: + Adafruit_BMP280* _bmp; + + public: + Bmp280p(Adafruit_BMP280* bmp, String parameters): IoTSensor(parameters) { + _bmp = bmp; + } + + void doByInterval() { + float value = _bmp->readPressure(); + if (String(value) != "nan") { + value = value / 1.333224 / 100; + regEvent(value, "Bmp280p"); + } else SerialPrint("E", "Sensor DHTh", "Error"); + } + + ~Bmp280p(); +}; + + +void* getAPI_Bmp280(String subtype, String param) { + String addr; + jsonRead(param, "addr", addr); + + if (bmps.find(addr) == bmps.end()) { + bmps[addr] = new Adafruit_BMP280(); + bmps[addr]->begin(hexStringToUint8(addr)); + } + + if (subtype == F("Bmp280t")) { + return new Bmp280t(bmps[addr], param); + } else if (subtype == F("Bmp280p")) { + return new Bmp280p(bmps[addr], param); + } else { + return nullptr; + } +} diff --git a/src/modules/Dht1122.cpp b/src/modules/Dht1122.cpp index 788e7da4..1dfbe964 100644 --- a/src/modules/Dht1122.cpp +++ b/src/modules/Dht1122.cpp @@ -16,15 +16,15 @@ std::map dhts; class Dht1122t : public IoTSensor { private: - int _pin; + DHTesp* _dht; public: - Dht1122t(String parameters): IoTSensor(parameters) { - jsonRead(parameters, "pin", _pin); + Dht1122t(DHTesp* dht, String parameters): IoTSensor(parameters) { + _dht = dht; } void doByInterval() { - float value = dhts[_pin]->getTemperature(); + float value = _dht->getTemperature(); if (String(value) != "nan") regEvent(value, "Dht1122t"); else SerialPrint("E", "Sensor DHTt", "Error"); } @@ -35,15 +35,15 @@ class Dht1122t : public IoTSensor { class Dht1122h : public IoTSensor { private: - int _pin; + DHTesp* _dht; public: - Dht1122h(String parameters): IoTSensor(parameters) { - jsonRead(parameters, "pin", _pin); + Dht1122h(DHTesp* dht, String parameters): IoTSensor(parameters) { + _dht = dht; } void doByInterval() { - float value = dhts[_pin]->getHumidity(); + float value = _dht->getHumidity(); if (String(value) != "nan") regEvent(value, "Dht1122h"); else SerialPrint("E", "Sensor DHTh", "Error"); } @@ -59,21 +59,19 @@ void* getAPI_Dht1122(String subtype, String param) { jsonRead(param, "senstype", senstype); if (dhts.find(pin) == dhts.end()) { - DHTesp* dht = new DHTesp(); + dhts[pin] = new DHTesp(); if (senstype == "dht11") { - dht->setup(pin, DHTesp::DHT11); + dhts[pin]->setup(pin, DHTesp::DHT11); } else if (senstype == "dht22") { - dht->setup(pin, DHTesp::DHT22); + dhts[pin]->setup(pin, DHTesp::DHT22); } - - dhts[pin] = dht; } if (subtype == F("Dht1122t")) { - return new Dht1122t(param); + return new Dht1122t(dhts[pin], param); } else if (subtype == F("Dht1122h")) { - return new Dht1122h(param); + return new Dht1122h(dhts[pin], param); } else { return nullptr; }