2018-11-06 16:33:51 +00:00
|
|
|
/*
|
|
|
|
xsns_02_analog.ino - ESP8266 ADC support for Sonoff-Tasmota
|
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Theo Arends
|
2018-11-06 16:33:51 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef USE_ADC_VCC
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* ADC support
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-05-16 17:43:23 +01:00
|
|
|
#define XSNS_02 2
|
2019-04-11 15:59:48 +01:00
|
|
|
|
|
|
|
#define TO_CELSIUS(x) ((x) - 273.15)
|
|
|
|
#define TO_KELVIN(x) ((x) + 273.15)
|
|
|
|
|
2019-05-16 17:43:23 +01:00
|
|
|
// Parameters for equation
|
|
|
|
#define ANALOG_V33 3.3 // ESP8266 Analog voltage
|
|
|
|
#define ANALOG_T0 TO_KELVIN(25.0) // 25 degrees Celcius in Kelvin (= 298.15)
|
|
|
|
|
|
|
|
// Shelly 2.5 NTC Thermistor
|
|
|
|
// 3V3 --- ANALOG_NTC_BRIDGE_RESISTANCE ---v--- NTC --- Gnd
|
|
|
|
// |
|
|
|
|
// ADC0
|
2019-05-24 11:28:09 +01:00
|
|
|
#define ANALOG_NTC_BRIDGE_RESISTANCE 32000 // NTC Voltage bridge resistor
|
|
|
|
#define ANALOG_NTC_RESISTANCE 10000 // NTC Resistance
|
|
|
|
#define ANALOG_NTC_B_COEFFICIENT 3350 // NTC Beta Coefficient
|
2019-05-16 17:43:23 +01:00
|
|
|
|
|
|
|
// LDR parameters
|
|
|
|
// 3V3 --- LDR ---v--- ANALOG_LDR_BRIDGE_RESISTANCE --- Gnd
|
|
|
|
// |
|
|
|
|
// ADC0
|
2019-05-24 11:28:09 +01:00
|
|
|
#define ANALOG_LDR_BRIDGE_RESISTANCE 10000 // LDR Voltage bridge resistor
|
2019-05-16 17:43:23 +01:00
|
|
|
#define ANALOG_LDR_LUX_CALC_SCALAR 12518931 // Experimental
|
2019-05-24 11:28:09 +01:00
|
|
|
#define ANALOG_LDR_LUX_CALC_EXPONENT -1.4050 // Experimental
|
2018-11-06 16:33:51 +00:00
|
|
|
|
|
|
|
uint16_t adc_last_value = 0;
|
2019-04-18 10:07:38 +01:00
|
|
|
float adc_temp = 0;
|
2018-11-06 16:33:51 +00:00
|
|
|
|
2019-05-24 11:28:09 +01:00
|
|
|
void AdcInit(void)
|
|
|
|
{
|
|
|
|
if ((Settings.adc_param_type != my_adc0) || (Settings.adc_param1 > 1000000) || (Settings.adc_param1 < 100)) {
|
|
|
|
if (ADC0_TEMP == my_adc0) {
|
|
|
|
// Default Shelly 2.5 and 1PM parameters
|
|
|
|
Settings.adc_param_type = ADC0_TEMP;
|
|
|
|
Settings.adc_param1 = ANALOG_NTC_BRIDGE_RESISTANCE;
|
|
|
|
Settings.adc_param2 = ANALOG_NTC_RESISTANCE;
|
|
|
|
Settings.adc_param3 = ANALOG_NTC_B_COEFFICIENT * 10000;
|
|
|
|
}
|
|
|
|
else if (ADC0_LIGHT == my_adc0) {
|
|
|
|
Settings.adc_param_type = ADC0_LIGHT;
|
|
|
|
Settings.adc_param1 = ANALOG_LDR_BRIDGE_RESISTANCE;
|
|
|
|
Settings.adc_param2 = ANALOG_LDR_LUX_CALC_SCALAR;
|
|
|
|
Settings.adc_param3 = ANALOG_LDR_LUX_CALC_EXPONENT * 10000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 10:01:37 +01:00
|
|
|
uint16_t AdcRead(uint8_t factor)
|
2018-11-06 16:33:51 +00:00
|
|
|
{
|
2019-04-12 10:01:37 +01:00
|
|
|
// factor 1 = 2 samples
|
|
|
|
// factor 2 = 4 samples
|
|
|
|
// factor 3 = 8 samples
|
|
|
|
// factor 4 = 16 samples
|
|
|
|
// factor 5 = 32 samples
|
|
|
|
uint8_t samples = 1 << factor;
|
2018-11-06 16:33:51 +00:00
|
|
|
uint16_t analog = 0;
|
2019-06-30 14:56:55 +01:00
|
|
|
for (uint8_t i = 0; i < samples; i++) {
|
2018-11-06 16:33:51 +00:00
|
|
|
analog += analogRead(A0);
|
|
|
|
delay(1);
|
|
|
|
}
|
2019-04-12 10:01:37 +01:00
|
|
|
analog >>= factor;
|
2018-11-06 16:33:51 +00:00
|
|
|
return analog;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_RULES
|
2018-11-14 13:32:09 +00:00
|
|
|
void AdcEvery250ms(void)
|
2018-11-06 16:33:51 +00:00
|
|
|
{
|
2019-05-13 14:56:01 +01:00
|
|
|
if (ADC0_INPUT == my_adc0) {
|
2019-04-12 10:01:37 +01:00
|
|
|
uint16_t new_value = AdcRead(5);
|
2019-04-11 15:59:48 +01:00
|
|
|
if ((new_value < adc_last_value -10) || (new_value > adc_last_value +10)) {
|
|
|
|
adc_last_value = new_value;
|
|
|
|
uint16_t value = adc_last_value / 10;
|
|
|
|
Response_P(PSTR("{\"ANALOG\":{\"A0div10\":%d}}"), (value > 99) ? 100 : value);
|
|
|
|
XdrvRulesProcess();
|
|
|
|
}
|
2018-11-06 16:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // USE_RULES
|
|
|
|
|
2019-05-16 17:43:23 +01:00
|
|
|
uint16_t AdcGetLux()
|
|
|
|
{
|
|
|
|
int adc = AdcRead(2);
|
|
|
|
// Source: https://www.allaboutcircuits.com/projects/design-a-luxmeter-using-a-light-dependent-resistor/
|
|
|
|
double resistorVoltage = ((double)adc / 1023) * ANALOG_V33;
|
|
|
|
double ldrVoltage = ANALOG_V33 - resistorVoltage;
|
2019-05-24 11:28:09 +01:00
|
|
|
double ldrResistance = ldrVoltage / resistorVoltage * (double)Settings.adc_param1;
|
|
|
|
double ldrLux = (double)Settings.adc_param2 * FastPrecisePow(ldrResistance, (double)Settings.adc_param3 / 10000);
|
2019-05-16 17:43:23 +01:00
|
|
|
|
|
|
|
return (uint16_t)ldrLux;
|
|
|
|
}
|
|
|
|
|
2019-04-18 10:07:38 +01:00
|
|
|
void AdcEverySecond(void)
|
|
|
|
{
|
2019-05-13 14:56:01 +01:00
|
|
|
if (ADC0_TEMP == my_adc0) {
|
2019-04-18 10:07:38 +01:00
|
|
|
int adc = AdcRead(2);
|
|
|
|
// Steinhart-Hart equation for thermistor as temperature sensor
|
2019-05-24 11:28:09 +01:00
|
|
|
double Rt = (adc * Settings.adc_param1) / (1024.0 * ANALOG_V33 - (double)adc);
|
|
|
|
double BC = (double)Settings.adc_param3 / 10000;
|
|
|
|
double T = BC / (BC / ANALOG_T0 + TaylorLog(Rt / (double)Settings.adc_param2));
|
2019-04-18 10:07:38 +01:00
|
|
|
adc_temp = ConvertTemp(TO_CELSIUS(T));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 11:28:09 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Commands
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define D_CMND_ADCPARAM "AdcParam"
|
|
|
|
enum AdcCommands { CMND_ADCPARAM };
|
|
|
|
const char kAdcCommands[] PROGMEM = D_CMND_ADCPARAM;
|
|
|
|
|
|
|
|
bool AdcCommand(void)
|
|
|
|
{
|
|
|
|
char command[CMDSZ];
|
|
|
|
bool serviced = true;
|
|
|
|
|
|
|
|
int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, kAdcCommands);
|
|
|
|
if (CMND_ADCPARAM == command_code) {
|
|
|
|
if (XdrvMailbox.data_len) {
|
|
|
|
if ((ADC0_TEMP == XdrvMailbox.payload) || (ADC0_LIGHT == XdrvMailbox.payload)) {
|
|
|
|
// if ((XdrvMailbox.payload == my_adc0) && ((ADC0_TEMP == my_adc0) || (ADC0_LIGHT == my_adc0))) {
|
|
|
|
if (strstr(XdrvMailbox.data, ",") != nullptr) { // Process parameter entry
|
|
|
|
char sub_string[XdrvMailbox.data_len +1];
|
|
|
|
// AdcParam 2, 32000, 10000, 3350
|
|
|
|
// AdcParam 3, 10000, 12518931, -1.405
|
|
|
|
Settings.adc_param_type = XdrvMailbox.payload;
|
|
|
|
// Settings.adc_param_type = my_adc0;
|
|
|
|
Settings.adc_param1 = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10);
|
|
|
|
Settings.adc_param2 = strtol(subStr(sub_string, XdrvMailbox.data, ",", 3), nullptr, 10);
|
|
|
|
Settings.adc_param3 = (int)(CharToDouble(subStr(sub_string, XdrvMailbox.data, ",", 4)) * 10000);
|
|
|
|
} else { // Set default values based on current adc type
|
|
|
|
// AdcParam 2
|
|
|
|
// AdcParam 3
|
|
|
|
Settings.adc_param_type = 0;
|
|
|
|
AdcInit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AdcParam
|
|
|
|
int value = Settings.adc_param3;
|
|
|
|
uint8_t precision;
|
|
|
|
for (precision = 4; precision > 0; precision--) {
|
|
|
|
if (value % 10) { break; }
|
|
|
|
value /= 10;
|
|
|
|
}
|
|
|
|
char param3[33];
|
|
|
|
dtostrfd(((double)Settings.adc_param3)/10000, precision, param3);
|
|
|
|
Response_P(PSTR("{\"" D_CMND_ADCPARAM "\":[%d,%d,%d,%s]}"),
|
|
|
|
Settings.adc_param_type, Settings.adc_param1, Settings.adc_param2, param3);
|
|
|
|
}
|
|
|
|
else serviced = false; // Unknown command
|
|
|
|
|
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void AdcShow(bool json)
|
2018-11-06 16:33:51 +00:00
|
|
|
{
|
2019-05-13 14:56:01 +01:00
|
|
|
if (ADC0_INPUT == my_adc0) {
|
2019-04-12 10:01:37 +01:00
|
|
|
uint16_t analog = AdcRead(5);
|
2018-11-06 16:33:51 +00:00
|
|
|
|
2019-04-11 15:59:48 +01:00
|
|
|
if (json) {
|
|
|
|
ResponseAppend_P(PSTR(",\"ANALOG\":{\"A0\":%d}"), analog);
|
2018-11-06 16:33:51 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2019-04-11 15:59:48 +01:00
|
|
|
} else {
|
|
|
|
WSContentSend_PD(HTTP_SNS_ANALOG, "", 0, analog);
|
2018-11-06 16:33:51 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2019-04-11 15:59:48 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-14 16:46:40 +01:00
|
|
|
else if (ADC0_TEMP == my_adc0) {
|
2019-04-11 15:59:48 +01:00
|
|
|
char temperature[33];
|
2019-04-18 10:07:38 +01:00
|
|
|
dtostrfd(adc_temp, Settings.flag2.temperature_resolution, temperature);
|
2019-04-11 15:59:48 +01:00
|
|
|
|
|
|
|
if (json) {
|
|
|
|
ResponseAppend_P(JSON_SNS_TEMP, "ANALOG", temperature);
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
if (0 == tele_period) {
|
|
|
|
DomoticzSensor(DZ_TEMP, temperature);
|
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_KNX
|
|
|
|
if (0 == tele_period) {
|
2019-05-13 15:36:17 +01:00
|
|
|
KnxSensor(KNX_TEMPERATURE, adc_temp);
|
2019-04-11 15:59:48 +01:00
|
|
|
}
|
|
|
|
#endif // USE_KNX
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
|
|
|
WSContentSend_PD(HTTP_SNS_TEMP, "", temperature, TempUnit());
|
2019-05-16 17:43:23 +01:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ADC0_LIGHT == my_adc0) {
|
|
|
|
uint16_t adc_light = AdcGetLux();
|
|
|
|
|
|
|
|
if (json) {
|
|
|
|
ResponseAppend_P(JSON_SNS_ILLUMINANCE, "ANALOG", adc_light);
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
if (0 == tele_period) {
|
|
|
|
DomoticzSensor(DZ_ILLUMINANCE, adc_light);
|
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
|
|
|
WSContentSend_PD(HTTP_SNS_ILLUMINANCE, "", adc_light);
|
2019-04-11 15:59:48 +01:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
2018-11-06 16:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns02(uint8_t function)
|
2018-11-06 16:33:51 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-11-06 16:33:51 +00:00
|
|
|
|
2019-05-16 17:43:23 +01:00
|
|
|
if ((ADC0_INPUT == my_adc0) || (ADC0_TEMP == my_adc0) || (ADC0_LIGHT == my_adc0)) {
|
2018-11-06 16:33:51 +00:00
|
|
|
switch (function) {
|
|
|
|
#ifdef USE_RULES
|
|
|
|
case FUNC_EVERY_250_MSECOND:
|
|
|
|
AdcEvery250ms();
|
|
|
|
break;
|
|
|
|
#endif // USE_RULES
|
2019-04-18 10:07:38 +01:00
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
AdcEverySecond();
|
|
|
|
break;
|
2019-05-24 11:28:09 +01:00
|
|
|
case FUNC_INIT:
|
|
|
|
AdcInit();
|
|
|
|
break;
|
|
|
|
case FUNC_COMMAND:
|
|
|
|
result = AdcCommand();
|
|
|
|
break;
|
2018-11-06 16:33:51 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
AdcShow(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
2019-03-19 16:31:43 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
2018-11-06 16:33:51 +00:00
|
|
|
AdcShow(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_ADC_VCC
|