Добавляем ADS1115

This commit is contained in:
2022-02-20 22:43:16 +03:00
parent 7c48ca6c5a
commit 905d137341
5 changed files with 125 additions and 0 deletions

View File

@@ -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;

95
src/modules/Ads1115.cpp Normal file
View File

@@ -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 <Adafruit_ADS1X15.h> // Библиотека для работы с модулями 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
// }