Tasmota/tasmota/tasmota_xsns_sensor/xsns_02_analog.ino

883 lines
31 KiB
Arduino
Raw Normal View History

2020-08-03 17:21:34 +01:00
/*
2020-09-29 13:08:48 +01:00
xsns_02_analog.ino - ADC support for Tasmota
2020-08-03 17:21:34 +01:00
2021-01-01 12:44:04 +00:00
Copyright (C) 2021 Theo Arends
2020-08-03 17:21:34 +01: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_ADC
/*********************************************************************************************\
2020-10-02 13:24:23 +01:00
* ADC support for ESP8266 GPIO17 (=PIN_A0) and ESP32 up to 8 channels on GPIO32 to GPIO39
2020-08-03 17:21:34 +01:00
\*********************************************************************************************/
#define XSNS_02 2
#ifdef ESP8266
#define ANALOG_RESOLUTION 10 // 12 = 4095, 11 = 2047, 10 = 1023
#define ANALOG_RANGE 1023 // 4095 = 12, 2047 = 11, 1023 = 10
#define ANALOG_PERCENT 10 // backward compatible div10 range
2020-11-28 16:00:15 +00:00
#endif // ESP8266
#ifdef ESP32
#undef ANALOG_RESOLUTION
#define ANALOG_RESOLUTION 12 // 12 = 4095, 11 = 2047, 10 = 1023
#undef ANALOG_RANGE
#define ANALOG_RANGE 4095 // 4095 = 12, 2047 = 11, 1023 = 10
#undef ANALOG_PERCENT
#define ANALOG_PERCENT ((ANALOG_RANGE + 50) / 100) // approximation to 1% ADC range
2020-11-28 16:00:15 +00:00
#endif // ESP32
2020-08-03 17:21:34 +01:00
#define TO_CELSIUS(x) ((x) - 273.15)
#define TO_KELVIN(x) ((x) + 273.15)
// Parameters for equation
#define ANALOG_V33 3.3 // ESP8266 / ESP32 Analog voltage
2021-11-16 22:10:25 +00:00
#define ANALOG_T0 TO_KELVIN(25.0) // 25 degrees Celsius in Kelvin (= 298.15)
2020-08-03 17:21:34 +01:00
2022-09-25 21:40:29 +01:00
// Mode 0 : Shelly 2.5 NTC Thermistor
2020-08-03 17:21:34 +01:00
// 3V3 --- ANALOG_NTC_BRIDGE_RESISTANCE ---v--- NTC --- Gnd
// |
// ADC0
2022-09-25 21:40:29 +01:00
// Mode 1 : NTC towards 3V3 (Sinilink Thermostat Relay Board (XY-WFT1)
// 3V3 --- NTC ---v--- ANALOG_NTC_BRIDGE_RESISTANCE --- Gnd
2022-09-30 22:53:58 +01:00
// |
// ADC0
2020-08-03 17:21:34 +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
// LDR parameters
// 3V3 --- LDR ---v--- ANALOG_LDR_BRIDGE_RESISTANCE --- Gnd
// |
// ADC0
#define ANALOG_LDR_BRIDGE_RESISTANCE 10000 // LDR Voltage bridge resistor
#define ANALOG_LDR_LUX_CALC_SCALAR 12518931 // Experimental
#define ANALOG_LDR_LUX_CALC_EXPONENT -1.4050 // Experimental
// CT Based Apparrent Power Measurement Parameters
// 3V3 --- R1 ----v--- R1 --- Gnd
// |
// CT+ CT-
// |
// ADC0
// Default settings for a 20A/1V Current Transformer.
// Analog peak to peak range is measured and converted to RMS current using ANALOG_CT_MULTIPLIER
#define ANALOG_CT_FLAGS 0 // (uint32_t) reserved for possible future use
#define ANALOG_CT_MULTIPLIER 2146 // (uint32_t) Multiplier*100000 to convert raw ADC peak to peak range 0..ANALOG_RANGE to RMS current in Amps. Value of 100000 corresponds to 1
2020-08-03 17:21:34 +01:00
#define ANALOG_CT_VOLTAGE 2300 // (int) Convert current in Amps to apparrent power in Watts using voltage in Volts*10. Value of 2200 corresponds to 220V
#define CT_FLAG_ENERGY_RESET (1 << 0) // Reset energy total
// Buttons
// ---- Inverted
// 3V3 ---| |----|
// |
// 3V3 --- R1 ----|--- R1 --- Gnd
// |
// |---| |--- Gnd
// | ----
// ADC
#define ANALOG_BUTTON 128 // Add resistor tolerance
// Odroid joysticks
// ---- Up
// 3V3 ---| |------------
// |
// ---- Dn |--- R10k --- Gnd
// 3V3 ---| |--- R10k ---|
// |
// ADC
// Press "Up" will raise ADC to ANALOG_RANGE, Press "Dn" will raise ADC to ANALOG_RANGE/2
#define ANALOG_JOYSTICK (ANALOG_RANGE / 3) +100 // Add resistor tolerance
2021-01-05 13:31:13 +00:00
// pH scale minimum and maximum values
#define ANALOG_PH_MAX 14.0
#define ANALOG_PH_MIN 0.0
// Default values for calibration solution with lower PH
#define ANALOG_PH_CALSOLUTION_LOW_PH 4.0
#define ANALOG_PH_CALSOLUTION_LOW_ANALOG_VALUE 282
2021-01-05 13:31:13 +00:00
// Default values for calibration solution with higher PH
2021-01-23 16:10:06 +00:00
#define ANALOG_PH_CALSOLUTION_HIGH_PH 9.18
#define ANALOG_PH_CALSOLUTION_HIGH_ANALOG_VALUE 435
2021-01-05 13:31:13 +00:00
2021-01-23 16:10:06 +00:00
// Multiplier used to store pH with 2 decimal places in a non decimal datatype
#define ANALOG_PH_DECIMAL_MULTIPLIER 100.0
2021-01-05 13:31:13 +00:00
2022-01-24 10:21:01 +00:00
// MQ-X sensor (MQ-02, MQ-03, MQ-04, MQ-05, MQ-06, MQ-07, MQ-08, MQ-09, MQ-131, MQ-135)
2022-08-22 13:32:08 +01:00
//
2022-01-24 10:21:01 +00:00
// A0 -------------------
// |
// GND ----------- |
// | |
// VCC --- | |
// | | |
// 3V3 GND ADC <- (A0 for nodemcu, wemos; GPIO34,35,36,39 and other analog IN/OUT pin for esp32)
//means mq type (ex for mq-02 use 2, mq-131 use 131)
2022-08-22 13:32:08 +01:00
#define ANALOG_MQ_TYPE 2
//exponential regression a params
#define ANALOG_MQ_A 574.25
//exponential regression b params
#define ANALOG_MQ_B -2.222
/*
Exponential regression:
Gas | a | b
LPG | 44771 | -3.245
CH4 | 2*10^31| 19.01
CO | 521853 | -3.821
Alcohol| 0.3934 | -1.504
Benzene| 4.8387 | -2.68
2022-08-22 13:32:08 +01:00
Hexane | 7585.3 | -2.849
NOx | -462.43 | -2.204
CL2 | 47.209 | -1.186
O3 | 23.943 | -1.11
*/
//ratio for alarm, NOT USED yet (RS / R0 = 15 ppm)
2022-08-22 13:32:08 +01:00
#define ANALOG_MQ_RatioMQCleanAir 15.0
// Multiplier used to store pH with 2 decimal places in a non decimal datatype
#define ANALOG_MQ_DECIMAL_MULTIPLIER 100.0
// lenght of filter
#define ANALOG_MQ_SAMPLES 60
struct {
uint8_t present = 0;
uint8_t type = 0;
} Adcs;
2020-08-03 17:21:34 +01:00
struct {
float temperature = 0;
float current = 0;
float energy = 0;
uint32_t param1 = 0;
uint32_t param2 = 0;
int param3 = 0;
int param4 = 0;
2020-08-03 17:21:34 +01:00
uint32_t previous_millis = 0;
uint16_t last_value = 0;
uint8_t type = 0;
uint8_t pin = 0;
float mq_samples[ANALOG_MQ_SAMPLES];
int indexOfPointer = -1;
2020-08-03 17:21:34 +01:00
} Adc[MAX_ADCS];
2020-09-25 17:15:31 +01:00
#ifdef ESP8266
bool adcAttachPin(uint8_t pin) {
2020-09-29 13:41:45 +01:00
return (ADC0_PIN == pin);
2020-09-25 17:15:31 +01:00
}
#endif
void AdcSaveSettings(uint32_t idx) {
char parameters[32];
snprintf_P(parameters, sizeof(parameters), PSTR("%d,%d,%d,%d,%d"),
Adc[idx].type, Adc[idx].param1, Adc[idx].param2, Adc[idx].param3, Adc[idx].param4);
SettingsUpdateText(SET_ADC_PARAM1 + idx, parameters);
}
2020-08-03 17:21:34 +01:00
void AdcGetSettings(uint32_t idx) {
char parameters[32];
Adcs.type = 0;
Adc[idx].param1 = 0;
Adc[idx].param2 = 0;
Adc[idx].param3 = 0;
Adc[idx].param4 = 0;
if (strchr(SettingsText(SET_ADC_PARAM1 + idx), ',') != nullptr) {
Adcs.type = atoi(subStr(parameters, SettingsText(SET_ADC_PARAM1 + idx), ",", 1));
Adc[idx].param1 = atoi(subStr(parameters, SettingsText(SET_ADC_PARAM1 + idx), ",", 2));
Adc[idx].param2 = atoi(subStr(parameters, SettingsText(SET_ADC_PARAM1 + idx), ",", 3));
Adc[idx].param3 = atoi(subStr(parameters, SettingsText(SET_ADC_PARAM1 + idx), ",", 4));
Adc[idx].param4 = atoi(subStr(parameters, SettingsText(SET_ADC_PARAM1 + idx), ",", 5));
}
}
void AdcInitParams(uint8_t idx) {
if ((Adcs.type != Adc[idx].type) || (Adc[idx].param1 > 1000000)) {
if (ADC_TEMP == Adc[idx].type) {
2020-08-03 17:21:34 +01:00
// Default Shelly 2.5 and 1PM parameters
Adc[idx].param1 = ANALOG_NTC_BRIDGE_RESISTANCE;
Adc[idx].param2 = ANALOG_NTC_RESISTANCE;
Adc[idx].param3 = ANALOG_NTC_B_COEFFICIENT * 10000;
2022-09-25 21:40:29 +01:00
Adc[idx].param4 = 0; // Default to Shelly mode with NTC towards GND
2020-08-03 17:21:34 +01:00
}
else if (ADC_LIGHT == Adc[idx].type) {
Adc[idx].param1 = ANALOG_LDR_BRIDGE_RESISTANCE;
Adc[idx].param2 = ANALOG_LDR_LUX_CALC_SCALAR;
Adc[idx].param3 = ANALOG_LDR_LUX_CALC_EXPONENT * 10000;
2020-08-03 17:21:34 +01:00
}
else if (ADC_RANGE == Adc[idx].type) {
Adc[idx].param1 = 0;
Adc[idx].param2 = ANALOG_RANGE;
Adc[idx].param3 = 0;
Adc[idx].param4 = 100;
2020-08-03 17:21:34 +01:00
}
else if (ADC_CT_POWER == Adc[idx].type) {
Adc[idx].param1 = ANALOG_CT_FLAGS; // (uint32_t) 0
Adc[idx].param2 = ANALOG_CT_MULTIPLIER; // (uint32_t) 100000
Adc[idx].param3 = ANALOG_CT_VOLTAGE; // (int) 10
2020-08-03 17:21:34 +01:00
}
2021-01-05 13:31:13 +00:00
else if (ADC_PH == Adc[idx].type) {
Adc[idx].param1 = ANALOG_PH_CALSOLUTION_LOW_PH * ANALOG_PH_DECIMAL_MULTIPLIER; // PH of the calibration solution 1, which is the one with the lower PH
Adc[idx].param2 = ANALOG_PH_CALSOLUTION_LOW_ANALOG_VALUE; // Reading of AnalogInput while probe is in solution 1
Adc[idx].param3 = ANALOG_PH_CALSOLUTION_HIGH_PH * ANALOG_PH_DECIMAL_MULTIPLIER; // PH of the calibration solution 2, which is the one with the higher PH
Adc[idx].param4 = ANALOG_PH_CALSOLUTION_HIGH_ANALOG_VALUE; // Reading of AnalogInput while probe is in solution 2
}
else if (ADC_MQ == Adc[idx].type) {
Adc[idx].param1 = ANALOG_MQ_TYPE; // Could be MQ-002, MQ-004, MQ-131 ....
Adc[idx].param2 = (int)(ANALOG_MQ_A * ANALOG_MQ_DECIMAL_MULTIPLIER); // Exponential regression
Adc[idx].param3 = (int)(ANALOG_MQ_B * ANALOG_MQ_DECIMAL_MULTIPLIER); // Exponential regression
Adc[idx].param4 = (int)(ANALOG_MQ_RatioMQCleanAir * ANALOG_MQ_DECIMAL_MULTIPLIER); // Exponential regression
}
2020-10-09 10:20:22 +01:00
}
if ((Adcs.type != Adc[idx].type) || (0 == Adc[idx].param1) || (Adc[idx].param1 > ANALOG_RANGE)) {
if ((ADC_BUTTON == Adc[idx].type) || (ADC_BUTTON_INV == Adc[idx].type)) {
Adc[idx].param1 = ANALOG_BUTTON;
}
else if (ADC_JOY == Adc[idx].type) {
Adc[idx].param1 = ANALOG_JOYSTICK;
}
}
}
void AdcAttach(uint32_t pin, uint8_t type) {
if (Adcs.present == MAX_ADCS) { return; }
Adc[Adcs.present].pin = pin;
if (adcAttachPin(Adc[Adcs.present].pin)) {
Adc[Adcs.present].type = type;
// analogSetPinAttenuation(Adc[Adcs.present].pin, ADC_11db); // Default
Adcs.present++;
2020-08-03 17:21:34 +01:00
}
}
void AdcInit(void) {
Adcs.present = 0;
2020-08-03 17:21:34 +01:00
for (uint32_t i = 0; i < MAX_ADCS; i++) {
if (PinUsed(GPIO_ADC_INPUT, i)) {
AdcAttach(Pin(GPIO_ADC_INPUT, i), ADC_INPUT);
2020-08-03 17:21:34 +01:00
}
if (PinUsed(GPIO_ADC_TEMP, i)) {
AdcAttach(Pin(GPIO_ADC_TEMP, i), ADC_TEMP);
2020-08-03 17:21:34 +01:00
}
if (PinUsed(GPIO_ADC_LIGHT, i)) {
AdcAttach(Pin(GPIO_ADC_LIGHT, i), ADC_LIGHT);
2020-08-03 17:21:34 +01:00
}
if (PinUsed(GPIO_ADC_RANGE, i)) {
AdcAttach(Pin(GPIO_ADC_RANGE, i), ADC_RANGE);
2020-08-03 17:21:34 +01:00
}
if (PinUsed(GPIO_ADC_CT_POWER, i)) {
AdcAttach(Pin(GPIO_ADC_CT_POWER, i), ADC_CT_POWER);
}
if (PinUsed(GPIO_ADC_JOY, i)) {
AdcAttach(Pin(GPIO_ADC_JOY, i), ADC_JOY);
2020-08-03 17:21:34 +01:00
}
2021-01-05 13:31:13 +00:00
if (PinUsed(GPIO_ADC_PH, i)) {
AdcAttach(Pin(GPIO_ADC_PH, i), ADC_PH);
}
if (PinUsed(GPIO_ADC_MQ, i)) {
AdcAttach(Pin(GPIO_ADC_MQ, i), ADC_MQ);
}
2020-08-03 17:21:34 +01:00
}
for (uint32_t i = 0; i < MAX_KEYS; i++) {
if (PinUsed(GPIO_ADC_BUTTON, i)) {
AdcAttach(Pin(GPIO_ADC_BUTTON, i), ADC_BUTTON);
}
else if (PinUsed(GPIO_ADC_BUTTON_INV, i)) {
AdcAttach(Pin(GPIO_ADC_BUTTON_INV, i), ADC_BUTTON_INV);
}
}
if (Adcs.present) {
2020-09-25 17:15:31 +01:00
#ifdef ESP32
analogSetClockDiv(1); // Default 1
2021-02-02 16:55:45 +00:00
#if CONFIG_IDF_TARGET_ESP32
analogSetWidth(ANALOG_RESOLUTION); // Default 12 bits (0 - 4095)
2021-02-02 16:55:45 +00:00
#endif // CONFIG_IDF_TARGET_ESP32
analogSetAttenuation(ADC_11db); // Default 11db
2020-09-25 17:15:31 +01:00
#endif
for (uint32_t idx = 0; idx < Adcs.present; idx++) {
AdcGetSettings(idx);
AdcInitParams(idx);
AdcSaveSettings(idx);
}
2020-08-03 17:21:34 +01:00
}
}
uint16_t AdcRead(uint32_t pin, uint32_t factor) {
2020-08-03 17:21:34 +01:00
// factor 1 = 2 samples
// factor 2 = 4 samples
// factor 3 = 8 samples
// factor 4 = 16 samples
// factor 5 = 32 samples
uint32_t samples = 1 << factor;
uint32_t analog = 0;
2020-08-03 17:21:34 +01:00
for (uint32_t i = 0; i < samples; i++) {
analog += analogRead(pin);
delay(1);
}
analog >>= factor;
return analog;
}
#ifdef USE_RULES
void AdcEvery250ms(void) {
2020-09-29 17:10:21 +01:00
char adc_idx[3] = { 0 };
uint32_t offset = 0;
for (uint32_t idx = 0; idx < Adcs.present; idx++) {
2020-09-29 17:10:21 +01:00
#ifdef ESP32
snprintf_P(adc_idx, sizeof(adc_idx), PSTR("%d"), idx +1);
offset = 1;
#endif
2020-08-03 17:21:34 +01:00
if (ADC_INPUT == Adc[idx].type) {
uint16_t new_value = AdcRead(Adc[idx].pin, 5);
if ((new_value < Adc[idx].last_value -ANALOG_PERCENT) || (new_value > Adc[idx].last_value +ANALOG_PERCENT)) {
2020-08-03 17:21:34 +01:00
Adc[idx].last_value = new_value;
uint16_t value = Adc[idx].last_value / ANALOG_PERCENT;
2020-09-29 17:10:21 +01:00
Response_P(PSTR("{\"ANALOG\":{\"A%ddiv10\":%d}}"), idx + offset, (value > 99) ? 100 : value);
XdrvRulesProcess(0);
2020-08-03 17:21:34 +01:00
}
}
else if (ADC_JOY == Adc[idx].type) {
uint16_t new_value = AdcRead(Adc[idx].pin, 1);
if (new_value && (new_value != Adc[idx].last_value)) {
Adc[idx].last_value = new_value;
uint16_t value = new_value / Adc[idx].param1;
2020-09-29 17:10:21 +01:00
Response_P(PSTR("{\"ANALOG\":{\"Joy%s\":%d}}"), adc_idx, value);
XdrvRulesProcess(0);
} else {
Adc[idx].last_value = 0;
}
}
2020-08-03 17:21:34 +01:00
}
}
#endif // USE_RULES
uint8_t AdcGetButton(uint32_t pin) {
for (uint32_t idx = 0; idx < Adcs.present; idx++) {
if (Adc[idx].pin == pin) {
if (ADC_BUTTON_INV == Adc[idx].type) {
2020-10-09 10:20:22 +01:00
return (AdcRead(Adc[idx].pin, 1) < Adc[idx].param1);
}
else if (ADC_BUTTON == Adc[idx].type) {
2020-10-09 10:20:22 +01:00
return (AdcRead(Adc[idx].pin, 1) > Adc[idx].param1);
}
}
2020-09-29 13:08:48 +01:00
}
return 0;
2020-09-29 13:08:48 +01:00
}
uint16_t AdcGetLux(uint32_t idx) {
int adc = AdcRead(Adc[idx].pin, 2);
2020-08-03 17:21:34 +01:00
// Source: https://www.allaboutcircuits.com/projects/design-a-luxmeter-using-a-light-dependent-resistor/
double resistorVoltage = ((double)adc / ANALOG_RANGE) * ANALOG_V33;
2020-08-03 17:21:34 +01:00
double ldrVoltage = ANALOG_V33 - resistorVoltage;
double ldrResistance = ldrVoltage / resistorVoltage * (double)Adc[idx].param1;
double ldrLux = (double)Adc[idx].param2 * FastPrecisePow(ldrResistance, (double)Adc[idx].param3 / 10000);
2020-08-03 17:21:34 +01:00
return (uint16_t)ldrLux;
}
void AddSampleMq(uint32_t idx){
// AddLog(LOG_LEVEL_DEBUG, PSTR("ADC: Adding sample for mq-sensor"));
int _adc = AdcRead(Adc[idx].pin, 2);
// init af array at same value
if (Adc[idx].indexOfPointer==-1) {
// AddLog(LOG_LEVEL_DEBUG, PSTR("ADC: Init samples for mq-sensor"));
for (int i = 0; i < ANALOG_MQ_SAMPLES; i ++) {
Adc[idx].mq_samples[i] = _adc;
}
} else {
Adc[idx].mq_samples[Adc[idx].indexOfPointer] = _adc;
}
Adc[idx].indexOfPointer++;
if (Adc[idx].indexOfPointer==ANALOG_MQ_SAMPLES) {
Adc[idx].indexOfPointer=0;
}
}
float AdcGetMq(uint32_t idx) {
// AddLog(LOG_LEVEL_DEBUG, PSTR("ADC: Getting value for mq-sensor"));
float avg = 0.0;
for (int i = 0; i < ANALOG_MQ_SAMPLES; i ++) {
avg += Adc[idx].mq_samples[i];
}
2022-08-22 13:32:08 +01:00
float voltage = (avg / ANALOG_MQ_SAMPLES) * ANALOG_V33 / ANALOG_RANGE;
float _RL = 10; // Value in KiloOhms
float _RS_Calc = ((ANALOG_V33 * _RL) / voltage) -_RL; // Get value of RS in a gas
if (_RS_Calc < 0) {
_RS_Calc = 0; // No negative values accepted.
}
float _R0 = 10;
float _ratio = _RS_Calc / _R0; // Get ratio RS_gas/RS_air
float ppm = Adc[idx].param2 / ANALOG_MQ_DECIMAL_MULTIPLIER * FastPrecisePow(_ratio, Adc[idx].param3 / ANALOG_MQ_DECIMAL_MULTIPLIER); // Source excel analisis https://github.com/miguel5612/MQSensorsLib_Docs/tree/master/Internal_design_documents
if (ppm < 0) { ppm = 0; } // No negative values accepted or upper datasheet recomendation.
if (ppm > 100000) { ppm = 100000; }
// AddLog(LOG_LEVEL_DEBUG, PSTR("ADC: Ppm read. ADC-RAW: %2_f, ppm: %2_f"), &voltage, &ppm);
return ppm;
}
2021-01-05 13:31:13 +00:00
float AdcGetPh(uint32_t idx) {
int adc = AdcRead(Adc[idx].pin, 2);
2022-01-04 15:33:35 +00:00
float y1 = (float)Adc[idx].param1 / ANALOG_PH_DECIMAL_MULTIPLIER;
int32_t x1 = Adc[idx].param2;
float y2 = (float)Adc[idx].param3 / ANALOG_PH_DECIMAL_MULTIPLIER;
int32_t x2 = Adc[idx].param4;
2021-01-05 13:31:13 +00:00
2022-01-04 15:33:35 +00:00
float m = (y2 - y1) / (float)(x2 - x1);
float ph = m * (float)(adc - x1) + y1;
2021-01-23 16:10:06 +00:00
// AddLog(LOG_LEVEL_DEBUG, PSTR("ADC: Analog pH read. ADC-RAW: %d, cal-low(pH=ADC): %2_f = %d, cal-high(pH=ADC): %2_f = %d"), adc, &y1, x1, &y2, x2);
2021-01-23 16:10:06 +00:00
2021-01-05 13:31:13 +00:00
return ph;
}
float AdcGetRange(uint32_t idx) {
2020-08-03 17:21:34 +01:00
// formula for calibration: value, fromLow, fromHigh, toLow, toHigh
// Example: 514, 632, 236, 0, 100
// int( ((<param2> - <analog-value>) / (<param2> - <param1>) ) * (<param3> - <param4>) ) + <param4> )
int adc = AdcRead(Adc[idx].pin, 2);
double adcrange = ( ((double)Adc[idx].param2 - (double)adc) / ( ((double)Adc[idx].param2 - (double)Adc[idx].param1)) * ((double)Adc[idx].param3 - (double)Adc[idx].param4) + (double)Adc[idx].param4 );
return (float)adcrange;
2020-08-03 17:21:34 +01:00
}
void AdcGetCurrentPower(uint8_t idx, uint8_t factor) {
// 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;
uint16_t analog = 0;
uint16_t analog_min = ANALOG_RANGE;
2020-08-03 17:21:34 +01:00
uint16_t analog_max = 0;
if (0 == Adc[idx].param1) {
2020-08-03 17:21:34 +01:00
for (uint32_t i = 0; i < samples; i++) {
analog = analogRead(Adc[idx].pin);
if (analog < analog_min) {
analog_min = analog;
}
if (analog > analog_max) {
analog_max = analog;
}
delay(1);
}
Adc[idx].current = (float)(analog_max-analog_min) * ((float)(Adc[idx].param2) / 100000);
2020-08-03 17:21:34 +01:00
}
else {
analog = AdcRead(Adc[idx].pin, 5);
if (analog > Adc[idx].param1) {
Adc[idx].current = ((float)(analog) - (float)Adc[idx].param1) * ((float)(Adc[idx].param2) / 100000);
2020-08-03 17:21:34 +01:00
}
else {
Adc[idx].current = 0;
}
}
float power = Adc[idx].current * (float)(Adc[idx].param3) / 10;
2020-08-03 17:21:34 +01:00
uint32_t current_millis = millis();
Adc[idx].energy = Adc[idx].energy + ((power * (current_millis - Adc[idx].previous_millis)) / 3600000000);
Adc[idx].previous_millis = current_millis;
}
void AdcEverySecond(void) {
for (uint32_t idx = 0; idx < Adcs.present; idx++) {
2020-08-03 17:21:34 +01:00
if (ADC_TEMP == Adc[idx].type) {
int adc = AdcRead(Adc[idx].pin, 2);
2021-11-16 22:10:25 +00:00
// Steinhart-Hart equation for thermistor as temperature sensor:
// double Rt = (adc * Adc[idx].param1 * MAX_ADC_V) / (ANALOG_RANGE * ANALOG_V33 - (double)adc * MAX_ADC_V);
// MAX_ADC_V in ESP8266 is 1
// MAX_ADC_V in ESP32 is 3.3
2022-09-27 22:05:53 +01:00
double Rt;
#ifdef ESP8266
2022-09-25 21:40:29 +01:00
if (Adc[idx].param4) { // Alternate mode
2022-09-27 22:05:53 +01:00
Rt = (double)Adc[idx].param1 * (ANALOG_RANGE * ANALOG_V33 - (double)adc) / (double)adc;
} else {
Rt = (double)Adc[idx].param1 * (double)adc / (ANALOG_RANGE * ANALOG_V33 - (double)adc);
2022-09-25 21:40:29 +01:00
}
2021-11-16 22:10:25 +00:00
#else
2022-09-27 22:05:53 +01:00
if (Adc[idx].param4) { // Alternate mode
Rt = (double)Adc[idx].param1 * (ANALOG_RANGE - (double)adc) / (double)adc;
} else {
Rt = (double)Adc[idx].param1 * (double)adc / (ANALOG_RANGE - (double)adc);
}
2022-01-04 15:33:35 +00:00
#endif
double BC = (double)Adc[idx].param3 / 10000; // Shelly param3 = 3350 (ANALOG_NTC_B_COEFFICIENT)
double T = BC / (BC / ANALOG_T0 + TaylorLog(Rt / (double)Adc[idx].param2)); // Shelly param2 = 10000 (ANALOG_NTC_RESISTANCE)
2020-08-03 17:21:34 +01:00
Adc[idx].temperature = ConvertTemp(TO_CELSIUS(T));
}
else if (ADC_CT_POWER == Adc[idx].type) {
AdcGetCurrentPower(idx, 5);
}
2022-01-23 18:19:32 +00:00
else if (ADC_MQ == Adc[idx].type) {
AddSampleMq(idx);
2022-01-23 18:19:32 +00:00
AdcGetMq(idx);
}
2020-08-03 17:21:34 +01:00
}
}
2020-08-04 17:01:51 +01:00
void AdcShowContinuation(bool *jsonflg) {
if (*jsonflg) {
ResponseAppend_P(PSTR(","));
} else {
ResponseAppend_P(PSTR(",\"ANALOG\":{"));
*jsonflg = true;
}
}
2020-08-03 17:21:34 +01:00
void AdcShow(bool json) {
bool domo_flag[ADC_END] = { false };
2020-09-25 17:15:31 +01:00
char adc_name[10] = { 0 }; // ANALOG8
2020-09-29 17:10:21 +01:00
char adc_idx[3] = { 0 };
2020-09-25 17:15:31 +01:00
uint32_t offset = 0;
2020-08-04 17:01:51 +01:00
bool jsonflg = false;
for (uint32_t idx = 0; idx < Adcs.present; idx++) {
2020-09-25 17:15:31 +01:00
#ifdef ESP32
2020-08-04 17:01:51 +01:00
snprintf_P(adc_name, sizeof(adc_name), PSTR("Analog%d"), idx +1);
2020-09-29 17:10:21 +01:00
snprintf_P(adc_idx, sizeof(adc_idx), PSTR("%d"), idx +1);
2020-09-25 17:15:31 +01:00
offset = 1;
#endif
2020-08-03 17:21:34 +01:00
switch (Adc[idx].type) {
case ADC_INPUT: {
uint16_t analog = AdcRead(Adc[idx].pin, 5);
if (json) {
2020-08-04 17:01:51 +01:00
AdcShowContinuation(&jsonflg);
2020-09-25 17:15:31 +01:00
ResponseAppend_P(PSTR("\"A%d\":%d"), idx + offset, analog);
2020-08-03 17:21:34 +01:00
#ifdef USE_WEBSERVER
} else {
2020-09-25 17:15:31 +01:00
WSContentSend_PD(HTTP_SNS_ANALOG, "", idx + offset, analog);
2020-08-03 17:21:34 +01:00
#endif // USE_WEBSERVER
}
break;
}
case ADC_TEMP: {
if (json) {
2020-08-04 17:01:51 +01:00
AdcShowContinuation(&jsonflg);
2021-06-11 17:14:12 +01:00
ResponseAppend_P(PSTR("\"" D_JSON_TEMPERATURE "%s\":%*_f"), adc_idx, Settings->flag2.temperature_resolution, &Adc[idx].temperature);
2020-10-29 12:37:09 +00:00
if ((0 == TasmotaGlobal.tele_period) && (!domo_flag[ADC_TEMP])) {
2020-08-03 17:21:34 +01:00
#ifdef USE_DOMOTICZ
2021-01-26 15:26:00 +00:00
DomoticzFloatSensor(DZ_TEMP, Adc[idx].temperature);
2020-08-03 17:21:34 +01:00
domo_flag[ADC_TEMP] = true;
#endif // USE_DOMOTICZ
#ifdef USE_KNX
KnxSensor(KNX_TEMPERATURE, Adc[idx].temperature);
#endif // USE_KNX
}
#ifdef USE_WEBSERVER
} else {
2021-01-26 15:26:00 +00:00
WSContentSend_Temp(adc_name, Adc[idx].temperature);
2020-08-03 17:21:34 +01:00
#endif // USE_WEBSERVER
}
break;
}
case ADC_LIGHT: {
uint16_t adc_light = AdcGetLux(idx);
2020-08-03 17:21:34 +01:00
if (json) {
2020-08-04 17:01:51 +01:00
AdcShowContinuation(&jsonflg);
2020-09-29 17:10:21 +01:00
ResponseAppend_P(PSTR("\"" D_JSON_ILLUMINANCE "%s\":%d"), adc_idx, adc_light);
2020-08-03 17:21:34 +01:00
#ifdef USE_DOMOTICZ
2020-10-29 12:37:09 +00:00
if ((0 == TasmotaGlobal.tele_period) && (!domo_flag[ADC_LIGHT])) {
2020-08-03 17:21:34 +01:00
DomoticzSensor(DZ_ILLUMINANCE, adc_light);
domo_flag[ADC_LIGHT] = true;
}
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_ILLUMINANCE, adc_name, adc_light);
#endif // USE_WEBSERVER
}
break;
}
case ADC_RANGE: {
float adc_range = AdcGetRange(idx);
char range_chr[FLOATSZ];
2021-06-11 17:14:12 +01:00
dtostrfd(adc_range, Settings->flag2.frequency_resolution, range_chr);
2020-08-03 17:21:34 +01:00
if (json) {
2020-08-04 17:01:51 +01:00
AdcShowContinuation(&jsonflg);
ResponseAppend_P(PSTR("\"" D_JSON_RANGE "%s\":%s"), adc_idx, range_chr);
2020-08-03 17:21:34 +01:00
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_RANGE_CHR, adc_name, range_chr);
2020-08-03 17:21:34 +01:00
#endif // USE_WEBSERVER
}
break;
}
case ADC_CT_POWER: {
AdcGetCurrentPower(idx, 5);
float voltage = (float)(Adc[idx].param3) / 10;
2020-08-03 17:21:34 +01:00
char voltage_chr[FLOATSZ];
2021-06-11 17:14:12 +01:00
dtostrfd(voltage, Settings->flag2.voltage_resolution, voltage_chr);
2020-08-03 17:21:34 +01:00
char current_chr[FLOATSZ];
2021-06-11 17:14:12 +01:00
dtostrfd(Adc[idx].current, Settings->flag2.current_resolution, current_chr);
2020-08-03 17:21:34 +01:00
char power_chr[FLOATSZ];
2021-06-11 17:14:12 +01:00
dtostrfd(voltage * Adc[idx].current, Settings->flag2.wattage_resolution, power_chr);
2020-08-03 17:21:34 +01:00
char energy_chr[FLOATSZ];
2021-06-11 17:14:12 +01:00
dtostrfd(Adc[idx].energy, Settings->flag2.energy_resolution, energy_chr);
2020-08-03 17:21:34 +01:00
if (json) {
2020-08-04 17:01:51 +01:00
AdcShowContinuation(&jsonflg);
2020-09-29 17:10:21 +01:00
ResponseAppend_P(PSTR("\"CTEnergy%s\":{\"" D_JSON_ENERGY "\":%s,\"" D_JSON_POWERUSAGE "\":%s,\"" D_JSON_VOLTAGE "\":%s,\"" D_JSON_CURRENT "\":%s}"),
adc_idx, energy_chr, power_chr, voltage_chr, current_chr);
2020-08-03 17:21:34 +01:00
#ifdef USE_DOMOTICZ
2020-10-29 12:37:09 +00:00
if ((0 == TasmotaGlobal.tele_period) && (!domo_flag[ADC_CT_POWER])) {
2020-08-03 17:21:34 +01:00
DomoticzSensor(DZ_POWER_ENERGY, power_chr);
DomoticzSensor(DZ_VOLTAGE, voltage_chr);
DomoticzSensor(DZ_CURRENT, current_chr);
domo_flag[ADC_CT_POWER] = true;
}
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_VOLTAGE, voltage_chr);
WSContentSend_PD(HTTP_SNS_CURRENT, current_chr);
WSContentSend_PD(HTTP_SNS_POWER, power_chr);
WSContentSend_PD(HTTP_SNS_ENERGY_TOTAL, energy_chr);
#endif // USE_WEBSERVER
}
break;
}
2020-08-04 17:01:51 +01:00
case ADC_JOY: {
uint16_t new_value = AdcRead(Adc[idx].pin, 1);
uint16_t value = new_value / Adc[idx].param1;
if (json) {
AdcShowContinuation(&jsonflg);
2020-09-29 17:10:21 +01:00
ResponseAppend_P(PSTR("\"Joy%s\":%d"), adc_idx, value);
2020-08-04 17:01:51 +01:00
}
break;
}
2021-01-05 13:31:13 +00:00
case ADC_PH: {
float ph = AdcGetPh(idx);
char ph_chr[FLOATSZ];
2021-01-05 13:31:13 +00:00
dtostrfd(ph, 2, ph_chr);
if (json) {
AdcShowContinuation(&jsonflg);
ResponseAppend_P(PSTR("\"pH%d\":%s"), idx + offset, ph_chr);
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_PH, "", ph_chr);
#endif // USE_WEBSERVER
}
break;
}
case ADC_MQ: {
float mq = AdcGetMq(idx);
char mq_chr[FLOATSZ];
dtostrfd(mq, 2, mq_chr);
float mqnumber =Adc[idx].param1;
char mqnumber_chr[FLOATSZ];
dtostrfd(mqnumber, 0, mqnumber_chr);
if (json) {
AdcShowContinuation(&jsonflg);
ResponseAppend_P(PSTR("\"MQ%d_%d\":%s"), Adc[idx].param1, idx + offset, mq_chr);
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_MQ, mqnumber_chr, mq_chr);
2021-01-05 13:31:13 +00:00
#endif // USE_WEBSERVER
}
break;
}
2020-08-03 17:21:34 +01:00
}
}
2020-08-04 17:01:51 +01:00
if (jsonflg) {
ResponseJsonEnd();
}
2020-08-03 17:21:34 +01:00
}
/*********************************************************************************************\
* Commands
\*********************************************************************************************/
const char kAdcCommands[] PROGMEM = "|" // No prefix
D_CMND_ADCPARAM;
void (* const AdcCommand[])(void) PROGMEM = {
&CmndAdcParam };
void CmndAdcParam(void) {
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_ADCS)) {
uint8_t idx = XdrvMailbox.index -1;
if (XdrvMailbox.data_len) {
2020-10-09 10:20:22 +01:00
if (XdrvMailbox.payload > ADC_INPUT) {
AdcGetSettings(idx);
2021-01-30 11:27:48 +00:00
if (ArgC() > 3) { // Process parameter entry
char argument[XdrvMailbox.data_len];
2022-09-25 21:40:29 +01:00
// AdcParam 2, 32000, 10000, 3350 ADC_TEMP Shelly mode
// AdcParam 2, 32000, 10000, 3350, 1 ADC_TEMP Alternate mode
// AdcParam 3, 10000, 12518931, -1.405
2020-10-09 10:20:22 +01:00
// AdcParam 4, 128, 0, 0
// AdcParam 5, 128, 0, 0
// AdcParam 6, 0, ANALOG_RANGE, 0, 100
// AdcParam 7, 0, 2146, 0.23
// AdcParam 8, 1000, 0, 0
Adc[idx].type = XdrvMailbox.payload;
2021-01-30 11:27:48 +00:00
Adc[idx].param1 = strtol(ArgV(argument, 2), nullptr, 10);
Adc[idx].param2 = strtol(ArgV(argument, 3), nullptr, 10);
if (ADC_RANGE == XdrvMailbox.payload) {
2021-01-30 11:27:48 +00:00
Adc[idx].param3 = abs(strtol(ArgV(argument, 4), nullptr, 10));
Adc[idx].param4 = abs(strtol(ArgV(argument, 5), nullptr, 10));
} else {
2021-01-30 11:27:48 +00:00
Adc[idx].param3 = (int)(CharToFloat(ArgV(argument, 4)) * 10000);
2022-09-25 21:40:29 +01:00
if (ArgC() > 4) {
Adc[idx].param4 = (int)(CharToFloat(ArgV(argument, 5)) * 10000);
}
else{
Adc[idx].param4 = 0;
}
}
2021-01-05 13:31:13 +00:00
if (ADC_PH == XdrvMailbox.payload) {
2021-01-30 11:27:48 +00:00
float phLow = CharToFloat(ArgV(argument, 2));
float phHigh = CharToFloat(ArgV(argument, 4));
2021-01-05 13:31:13 +00:00
Adc[idx].param1 = phLow * ANALOG_PH_DECIMAL_MULTIPLIER;
2021-01-30 11:27:48 +00:00
Adc[idx].param2 = strtol(ArgV(argument, 3), nullptr, 10);
2021-01-05 13:31:13 +00:00
Adc[idx].param3 = phHigh * ANALOG_PH_DECIMAL_MULTIPLIER;
2021-01-30 11:27:48 +00:00
Adc[idx].param4 = strtol(ArgV(argument, 5), nullptr, 10);
// AddLog(LOG_LEVEL_INFO, PSTR("ADC: Analog pH probe calibrated. cal-low(pH=ADC) %2_f = %d, cal-high(pH=ADC) %2_f = %d"), &phLow, Adc[idx].param2, &phHigh, Adc[idx].param4);
2021-01-05 13:31:13 +00:00
}
if (ADC_CT_POWER == XdrvMailbox.payload) {
if (((1 == Adc[idx].param1) & CT_FLAG_ENERGY_RESET) > 0) {
for (uint32_t idx = 0; idx < MAX_ADCS; idx++) {
Adc[idx].energy = 0;
}
Adc[idx].param1 ^= CT_FLAG_ENERGY_RESET; // Cancel energy reset flag
2020-08-03 17:21:34 +01:00
}
}
if (ADC_MQ == XdrvMailbox.payload) {
float a = CharToFloat(ArgV(argument, 3));
float b = CharToFloat(ArgV(argument, 4));
float ratioMQCleanAir = CharToFloat(ArgV(argument, 5));
if (a==0 && b==0 && ratioMQCleanAir==0)
{
if (Adc[idx].param1==2)
{
a=574.25;
2022-01-23 16:28:13 +00:00
b=-2.222;
ratioMQCleanAir=9.83;
}
if (Adc[idx].param1==4)
{
2022-01-23 16:28:13 +00:00
a=1012.7;
b=-2.786;
ratioMQCleanAir=4.4;
}
if (Adc[idx].param1==7)
{
2022-01-23 16:28:13 +00:00
a=99.042;
b=-1.518;
ratioMQCleanAir=27.5;
}
if (Adc[idx].param1==131)
{
2022-01-23 16:28:13 +00:00
a=23.943;
b=-1.11;
ratioMQCleanAir=15;
}
}
Adc[idx].param2 = (int)(a * ANALOG_MQ_DECIMAL_MULTIPLIER); // Exponential regression
Adc[idx].param3 = (int)(b * ANALOG_MQ_DECIMAL_MULTIPLIER); // Exponential regression
Adc[idx].param4 = (int)(ratioMQCleanAir * ANALOG_MQ_DECIMAL_MULTIPLIER); // Exponential regression
// AddLog(LOG_LEVEL_INFO, PSTR("ADC: MQ reset mq%d, a = %2_f, b = %2_f, ratioMQCleanAir = %2_f"), Adc[idx].param1, &a, &b, &ratioMQCleanAir);
}
} else { // Set default values based on current adc type
// AdcParam 2
// AdcParam 3
2020-10-09 10:20:22 +01:00
// AdcParam 4
// AdcParam 5
// AdcParam 6
// AdcParam 7
// AdcParam 8
Adcs.type = 0;
AdcInitParams(idx);
2020-08-03 17:21:34 +01:00
}
AdcSaveSettings(idx);
2020-08-03 17:21:34 +01:00
}
}
// AdcParam
AdcGetSettings(idx);
Response_P(PSTR("{\"" D_CMND_ADCPARAM "%d\":[%d,%d,%d"), idx +1, Adcs.type, Adc[idx].param1, Adc[idx].param2);
if ((ADC_RANGE == Adc[idx].type) || (ADC_MQ == Adc[idx].type)){
ResponseAppend_P(PSTR(",%d,%d"), Adc[idx].param3, Adc[idx].param4);
} else {
int value = Adc[idx].param3;
uint8_t precision;
for (precision = 4; precision > 0; precision--) {
if (value % 10) { break; }
value /= 10;
}
char param3[FLOATSZ];
dtostrfd(((double)Adc[idx].param3)/10000, precision, param3);
2022-09-30 22:53:58 +01:00
ResponseAppend_P(PSTR(",%s,%d"), param3, Adc[idx].param4);
2020-08-03 17:21:34 +01:00
}
ResponseAppend_P(PSTR("]}"));
2020-08-03 17:21:34 +01:00
}
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
2022-11-11 09:44:56 +00:00
bool Xsns02(uint32_t function) {
2020-08-03 17:21:34 +01:00
bool result = false;
switch (function) {
case FUNC_COMMAND:
result = DecodeCommand(kAdcCommands, AdcCommand);
break;
case FUNC_MODULE_INIT:
2020-08-03 17:21:34 +01:00
AdcInit();
break;
default:
if (Adcs.present) {
2020-08-03 17:21:34 +01:00
switch (function) {
#ifdef USE_RULES
case FUNC_EVERY_250_MSECOND:
AdcEvery250ms();
break;
#endif // USE_RULES
case FUNC_EVERY_SECOND:
AdcEverySecond();
break;
case FUNC_JSON_APPEND:
AdcShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
AdcShow(0);
break;
#endif // USE_WEBSERVER
}
}
}
return result;
}
#endif // USE_ADC