diff --git a/data_svelte/items.json b/data_svelte/items.json index c1b7c299..f345cae8 100644 --- a/data_svelte/items.json +++ b/data_svelte/items.json @@ -274,5 +274,24 @@ "page": "", "descr": "", "int": 15 + }, + { + "name": "21. Датчик напряжения ADS1115", + "num": 21, + "type": "Reading", + "subtype": "Ads1115", + "id": "Ads3", + "widget": "anydataVlt", + "page": "Сенсоры", + "descr": "ADS_3", + + "pin": "0", + "mode": "volt", + "gain": "3/4x", + + "plus": 0, + "multiply": 1, + "round": 100, + "int": 10 } ] \ No newline at end of file diff --git a/data_svelte/widgets.json b/data_svelte/widgets.json index 12a3e3db..f66db328 100644 --- a/data_svelte/widgets.json +++ b/data_svelte/widgets.json @@ -183,5 +183,12 @@ "Включен" ], "status": 0 + }, + { + "name": "anydataVlt", + "label": "Вольты", + "widget": "anydata", + "after": "Vlt", + "icon": "speedometer" } ] \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index d471b68b..3c8f1ed1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -36,6 +36,7 @@ lib_deps = me-no-dev/ESPAsyncUDP ;для esp32 данная библиотека встроена в ядро ClosedCube HDC1080 https://github.com/JonasGMorsch/GY-21.git + adafruit/Adafruit ADS1X15 @ ^2.3.0 monitor_filters = esp8266_exception_decoder upload_speed = 921600 monitor_speed = 115200 @@ -57,6 +58,7 @@ lib_deps = marcoschwartz/LiquidCrystal_I2C@^1.1.4 ClosedCube HDC1080 https://github.com/JonasGMorsch/GY-21.git + adafruit/Adafruit ADS1X15 @ ^2.3.0 monitor_filters = esp32_exception_decoder upload_speed = 921600 monitor_speed = 115200 diff --git a/src/modules/API.cpp b/src/modules/API.cpp index 52364d37..c3ff2aef 100644 --- a/src/modules/API.cpp +++ b/src/modules/API.cpp @@ -13,6 +13,7 @@ void* getAPI_Hdc1080(String subtype, String params); void* getAPI_GY21(String subtype, String params); void* getAPI_Lcd2004(String subtype, String params); void* getAPI_SysExt(String subtype, String params); +void* getAPI_Ads1115(String subtype, String params); //============================================================================================ void* getAPI(String subtype, String params) { @@ -30,6 +31,7 @@ void* getAPI(String subtype, String params) { if ((tmpAPI = getAPI_GY21(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Lcd2004(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_SysExt(subtype, params)) != nullptr) return tmpAPI; + if ((tmpAPI = getAPI_Ads1115(subtype, params)) != nullptr) return tmpAPI; //================================================================================================================ return nullptr; diff --git a/src/modules/Ads1115.cpp b/src/modules/Ads1115.cpp new file mode 100644 index 00000000..5ff1733e --- /dev/null +++ b/src/modules/Ads1115.cpp @@ -0,0 +1,95 @@ +/****************************************************************** + Used Adafruit ADS1X15 Driver (16-bit Differential or Single-Ended ADCs with PGA and Comparator) + Support for ADS1015/1115 + https://github.com/adafruit/Adafruit_ADS1X15 + + adapted for version 4 @Serghei63 + ******************************************************************/ + +#include "Global.h" +#include "Classes/IoTItem.h" + +#include "Wire.h" +#include // Библиотека для работы с модулями ADS1115 и ADS1015 + +Adafruit_ADS1115 ads; + +class Ads1115 : public IoTItem { + int _pin; + bool _isRaw; + bool _isInited = false; + + public: + Ads1115(String parameters): IoTItem(parameters) { + String tmp; + jsonRead(parameters, "pin", tmp); + _pin = tmp.toInt(); + + jsonRead(parameters, "mode", tmp); + _isRaw = tmp == "raw"; + + if (!ads.begin()) { + Serial.println("Failed to initialize ADS."); + _isInited = false; + } else _isInited = true; + + String gain; + jsonRead(parameters, "gain", gain); + if (gain == "1x") ads.setGain(GAIN_ONE); + else if (gain == "2x") ads.setGain(GAIN_TWO); + else if (gain == "4x") ads.setGain(GAIN_FOUR); + else if (gain == "8x") ads.setGain(GAIN_EIGHT); + else if (gain == "16x") ads.setGain(GAIN_SIXTEEN); + else ads.setGain(GAIN_TWOTHIRDS); + // ВОЗМОЖНЫЕ ВАРИАНТЫ УСТАНОВКИ КУ: + // ads.setGain(GAIN_TWOTHIRDS); //| 2/3х | +/-6.144V | 1bit = 0.1875mV | + // ads.setGain(GAIN_ONE); //| 1х | +/-4.096V | 1bit = 0.125mV | + // ads.setGain(GAIN_TWO); //| 2х | +/-2.048V | 1bit = 0.0625mV | + // ads.setGain(GAIN_FOUR); //| 4х | +/-1.024V | 1bit = 0.03125mV | + // ads.setGain(GAIN_EIGHT); //| 8х | +/-0.512V | 1bit = 0.015625mV | + // ads.setGain(GAIN_SIXTEEN); //| 16х | +/-0.256V | 1bit = 0.0078125mV | + } + + void doByInterval() { + if (_isInited) { + if (_isRaw) value.valD = ads.readADC_SingleEnded(_pin); // Чтение АЦП нулевого канала(rawdata) + else value.valD = ads.computeVolts(ads.readADC_SingleEnded(_pin)); // Чтение АЦП нулевого канала (Вольты) + regEvent(value.valD, "ADC1115"); + } + } + + ~Ads1115(); +}; + + +void *getAPI_Ads1115(String subtype, String param) +{ + if (subtype == F("Ads1115")) { + return new Ads1115(param); + } else { + return nullptr; + } +} + + + + + // { + // "name": "26. Датчик напряжения ADS1115", + // "num": 26, + // "type": "Reading", + // "subtype": "Ads1115", + // "id": "Ads3", + // "widget": "anydataVlt", + // "page": "Сенсоры", + // "descr": "ADS_3", + + // "pin": "0", + // "mode": "volt", + // "gain": "3/4x", + + // "plus": 0, + // "multiply": 1, + // "round": 100, + // "int": 10 + // } \ No newline at end of file