2017-11-19 17:02:03 +00:00
|
|
|
/*
|
|
|
|
xsns_12_ads1115.ino - ADS1x15 A/D Converter support for Sonoff-Tasmota
|
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Stefan Bode and Theo Arends
|
2017-11-19 17:02:03 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_ADS1115_I2CDEV
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* ADS1115 - 4 channel 16BIT A/D converter
|
|
|
|
*
|
|
|
|
* Required library: https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/ADS1115
|
|
|
|
*
|
|
|
|
* I2C Address: 0x48, 0x49, 0x4A or 0x4B
|
|
|
|
*
|
|
|
|
* The ADC input range (or gain) can be changed via the following
|
|
|
|
* functions, but be careful never to exceed VDD +0.3V max, or to
|
|
|
|
* exceed the upper and lower limits if you adjust the input range!
|
|
|
|
* Setting these values incorrectly may destroy your ADC!
|
|
|
|
* ADS1015 ADS1115
|
|
|
|
* ------- -------
|
|
|
|
* ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default)
|
|
|
|
* ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
|
|
|
|
* ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
|
|
|
|
* ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
|
|
|
|
* ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV
|
|
|
|
* ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_12 12
|
|
|
|
|
2017-11-19 17:02:03 +00:00
|
|
|
#include <ADS1115.h>
|
|
|
|
|
|
|
|
ADS1115 adc0;
|
|
|
|
|
|
|
|
uint8_t ads1115_type = 0;
|
|
|
|
uint8_t ads1115_address;
|
|
|
|
uint8_t ads1115_addresses[] = {
|
|
|
|
ADS1115_ADDRESS_ADDR_GND, // address pin low (GND)
|
|
|
|
ADS1115_ADDRESS_ADDR_VDD, // address pin high (VCC)
|
|
|
|
ADS1115_ADDRESS_ADDR_SDA, // address pin tied to SDA pin
|
|
|
|
ADS1115_ADDRESS_ADDR_SCL // address pin tied to SCL pin
|
|
|
|
};
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
int16_t Ads1115GetConversion(uint8_t channel)
|
2017-11-19 17:02:03 +00:00
|
|
|
{
|
|
|
|
switch (channel) {
|
|
|
|
case 0:
|
|
|
|
adc0.getConversionP0GND();
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
adc0.getConversionP1GND();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
adc0.getConversionP2GND();
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
adc0.getConversionP3GND();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void Ads1115Detect(void)
|
2017-11-19 17:02:03 +00:00
|
|
|
{
|
|
|
|
if (ads1115_type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
for (uint8_t i = 0; i < sizeof(ads1115_addresses); i++) {
|
2017-11-19 17:02:03 +00:00
|
|
|
ads1115_address = ads1115_addresses[i];
|
|
|
|
ADS1115 adc0(ads1115_address);
|
|
|
|
if (adc0.testConnection()) {
|
|
|
|
adc0.initialize();
|
2017-12-03 13:06:15 +00:00
|
|
|
adc0.setGain(ADS1115_PGA_6P144); // Set the gain (PGA) +/-6.144V
|
2017-11-19 17:02:03 +00:00
|
|
|
adc0.setRate(ADS1115_RATE_860);
|
|
|
|
adc0.setMode(ADS1115_MODE_CONTINUOUS);
|
|
|
|
ads1115_type = 1;
|
2019-03-08 14:15:42 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "ADS1115", ads1115_address);
|
2017-11-19 17:02:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void Ads1115Show(bool json)
|
2017-11-19 17:02:03 +00:00
|
|
|
{
|
|
|
|
if (ads1115_type) {
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t dsxflg = 0;
|
|
|
|
for (uint8_t i = 0; i < 4; i++) {
|
2017-11-19 17:02:03 +00:00
|
|
|
int16_t adc_value = Ads1115GetConversion(i);
|
|
|
|
|
|
|
|
if (json) {
|
|
|
|
if (!dsxflg ) {
|
2019-03-23 16:57:31 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"ADS1115\":{"));
|
2017-11-19 17:02:03 +00:00
|
|
|
}
|
2019-03-23 16:57:31 +00:00
|
|
|
ResponseAppend_P(PSTR("%s\"A%d\":%d"), (dsxflg) ? "," : "", i, adc_value);
|
2017-11-19 17:02:03 +00:00
|
|
|
dsxflg++;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2019-03-19 16:31:43 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_ANALOG, "ADS1115", i, adc_value);
|
2017-11-19 17:02:03 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (json) {
|
|
|
|
if (dsxflg) {
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
ResponseJsonEnd();
|
2017-11-19 17:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns12(uint8_t function)
|
2017-11-19 17:02:03 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2017-11-19 17:02:03 +00:00
|
|
|
|
|
|
|
if (i2c_flg) {
|
|
|
|
switch (function) {
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_PREP_BEFORE_TELEPERIOD:
|
2017-11-19 17:02:03 +00:00
|
|
|
Ads1115Detect();
|
|
|
|
break;
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
2017-11-19 17:02:03 +00:00
|
|
|
Ads1115Show(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
2019-03-19 16:31:43 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
2017-11-19 17:02:03 +00:00
|
|
|
Ads1115Show(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_ADS1115_I2CDEV
|
|
|
|
#endif // USE_I2C
|