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/>.
*/
2024-05-04 14:34:11 +01:00
# ifndef FIRMWARE_MINIMAL
2024-05-03 14:01:31 +01:00
2020-08-03 17:21:34 +01:00
# 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
2024-08-04 15:35:02 +01:00
*
2024-08-09 15:11:30 +01:00
* Command AdcParam < x > allows for configuration of multiple sequential ADC GPIOs .
* Due to it ' s sequential nature it loses configurations when in - between ADC GPIOs are redefined .
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* AdcParam < channel > < parameters >
* AdcParam1 1 < - Set first ADC GPIO found , counting from GPIO0 up , to default parameters based on configured GPIO_ADC_xxxx
* AdcParam1 1 , 32000 , 40000 , 3350 < - Temperature parameters for first ADC GPIO found , counting from GPIO0 up , and configured as GPIO_ADC_TEMP
* AdcParam2 1 , 511 < - Button parameter for second ADC GPIO found , counting from GPIO0 up , and configured as GPIO_ADC_BUTTON1
* AdcParam3 1 , 511 < - Button parameter for third ADC GPIO found , counting from GPIO0 up , and configured as GPIO_ADC_BUTTON2
2024-08-04 15:35:02 +01:00
*
2024-08-09 15:11:30 +01:00
* Version v14 .1 .0 .4 supersedes previous command with command AdcGpio for easier configuration of multiple ADC GPIOs .
* As it is GPIO based it will not loose configurations when in - between GPIOs are redefined .
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* AdcGpio < gpio > < parameters >
* AdcGpio 1 < - ESP8266 Set ADC on GPIO17 to default parameters based on configured GPIO_ADC_xxxx
* AdcGpio 32000 , 40000 , 3350 < - ESP8266 Temperature parameters for ADC on GPIO17 configured as GPIO_ADC_TEMP ( no index needed )
* AdcGpio33 1 < - ESP32 Set ADC on GPIO33 to default parameters based on configured GPIO_ADC_xxxx
* AdcGpio33 32000 , 40000 , 3350 < - ESP32 Temperature parameters for ADC on GPIO33 configured as GPIO_ADC_TEMP
* AdcGpio37 511 < - ESP32 Button parameter for ADC on GPIO37 configured as GPIO_ADC_BUTTON1
2024-08-04 15:35:02 +01:00
*
*
* For shelly RGBW PM :
* Template { " NAME " : " Shelly Plus RGBW PM Pot.meter " , " GPIO " : [ 1 , 0 , 0 , 0 , 419 , 0 , 0 , 0 , 0 , 0 , 544 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 416 , 417 , 418 , 0 , 0 , 0 , 0 , 0 , 4736 , 11264 , 11296 , 4704 , 0 , 4705 , 0 ] , " FLAG " : 0 , " BASE " : 1 }
2024-08-09 15:11:30 +01:00
* AdcGpio33 32000 , 40000 , 3350 < - Temperature parameters
* AdcGpio34 1161 , 3472 , 10 , 30 < - Voltage parameters
* AdcGpio35 960 , 1017 , 0.01 , 0.706 < - Current parameters
* AdcGpio36 1552 , 176 , 3 , 1 < - I1 Potentiometer RGBW or RGB if SO37 128
* AdcGpio36 1552 , 176 , 3 , 3 < - I1 Potentiometer RGBW or RGB if SO37 128 without fading
* AdcGpio38 1552 , 176 , 3 , 1 < - I3 Potentiometer W if SO37 128
2024-08-04 15:35:02 +01:00
* Fade 1
* Speed 6
* VoltRes 2
* WattRes 2
*
* Template { " NAME " : " Shelly Plus RGBW PM Button " , " GPIO " : [ 1 , 0 , 0 , 0 , 419 , 0 , 0 , 0 , 0 , 0 , 544 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 416 , 417 , 418 , 0 , 0 , 0 , 0 , 0 , 4736 , 11264 , 11296 , 4800 , 4801 , 4802 , 4803 ] , " FLAG " : 0 , " BASE " : 1 }
2024-08-09 15:11:30 +01:00
* AdcGpio33 32000 , 40000 , 3350 < - Temperature parameters
* AdcGpio34 1161 , 3472 , 10 , 30 < - Voltage parameters
* AdcGpio35 960 , 1017 , 0.01 , 0.706 < - Current parameters
* AdcGpio36 511 < - I1 ADC Button
* AdcGpio37 511 < - I2 ADC Button
* AdcGpio38 511 < - I3 ADC Button
* AdcGpio39 511 < - I4 ADC Button
2024-08-04 15:35:02 +01:00
* Fade 1
* Speed 6
* VoltRes 2
* WattRes 2
2020-08-03 17:21:34 +01:00
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# define XSNS_02 2
2024-08-15 11:52:12 +01:00
# if defined(ESP32) && defined(USE_ENERGY_SENSOR)
// Only ESP32 and up support more than one ADC channel enabling energy driver
2024-07-27 16:11:25 +01:00
# define XNRG_33 33
2024-08-15 11:52:12 +01:00
# endif // ESP32 and USE_ENERGY_SENSOR
2020-08-03 17:21:34 +01:00
2023-08-05 19:34:24 +01:00
# ifdef ESP32
2024-05-15 15:45:42 +01:00
# include "esp32-hal-adc.h"
2023-08-05 19:34:24 +01:00
# endif
2020-10-08 17:27:12 +01:00
# ifdef ESP8266
# define ANALOG_RESOLUTION 10 // 12 = 4095, 11 = 2047, 10 = 1023
# define ANALOG_RANGE 1023 // 4095 = 12, 2047 = 11, 1023 = 10
2020-11-28 16:00:15 +00:00
# endif // ESP8266
2024-07-25 17:04:01 +01:00
2020-11-28 16:00:15 +00:00
# ifdef ESP32
2020-10-08 17:27:12 +01:00
# undef ANALOG_RESOLUTION
2020-08-04 15:33:05 +01:00
# define ANALOG_RESOLUTION 12 // 12 = 4095, 11 = 2047, 10 = 1023
2020-10-08 17:27:12 +01:00
# undef ANALOG_RANGE
2020-08-06 13:39:01 +01:00
# define ANALOG_RANGE 4095 // 4095 = 12, 2047 = 11, 1023 = 10
2020-11-28 16:00:15 +00:00
# endif // ESP32
2020-08-04 15:33:05 +01:00
2024-08-13 12:57:58 +01:00
# define ANALOG_MARGIN 5 // backward compatible div10 range
2024-07-28 13:15:35 +01:00
# define TO_CELSIUS(x) ((x) - 273.15f)
# define TO_KELVIN(x) ((x) + 273.15f)
2020-08-03 17:21:34 +01:00
// Parameters for equation
2024-07-28 13:15:35 +01:00
# define ANALOG_V33 3.3f // ESP8266 / ESP32 Analog voltage
# define ANALOG_T0 TO_KELVIN(25.0f) // 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
2024-08-13 12:57:58 +01:00
// LDR parameters (example as used on Ulanzi)
2020-08-03 17:21:34 +01:00
// 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
2024-07-28 13:15:35 +01:00
# define ANALOG_LDR_LUX_CALC_EXPONENT -1.4050f // Experimental
2020-08-03 17:21:34 +01:00
// 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
2020-08-04 15:33:05 +01:00
# 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
2020-10-08 17:27:12 +01:00
// Buttons
// ---- Inverted
// 3V3 ---| |----|
// |
// 3V3 --- R1 ----|--- R1 --- Gnd
// |
// |---| |--- Gnd
// | ----
// ADC
2024-08-03 16:52:47 +01:00
# define ANALOG_BUTTON_THRESHOLD ANALOG_RANGE / 8 // Add resistor tolerance
2020-10-08 17:27:12 +01:00
2020-08-06 13:39:01 +01:00
// 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
2024-08-03 16:52:47 +01:00
# define ANALOG_JOYSTICK_THRESHOLD (ANALOG_RANGE / 3) +100 // Add resistor tolerance
2020-08-06 13:39:01 +01:00
2021-01-05 13:31:13 +00:00
// pH scale minimum and maximum values
2024-07-28 13:15:35 +01:00
# define ANALOG_PH_MAX 14.0f
# define ANALOG_PH_MIN 0.0f
2021-01-05 13:31:13 +00:00
// Default values for calibration solution with lower PH
2024-07-28 13:15:35 +01:00
# define ANALOG_PH_CALSOLUTION_LOW_PH 4.0f
2021-01-05 15:25:56 +00:00
# define ANALOG_PH_CALSOLUTION_LOW_ANALOG_VALUE 282
2021-01-05 13:31:13 +00:00
// Default values for calibration solution with higher PH
2024-07-28 13:15:35 +01:00
# define ANALOG_PH_CALSOLUTION_HIGH_PH 9.18f
2021-01-05 15:25:56 +00:00
# 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
2024-07-28 13:15:35 +01:00
# define ANALOG_PH_DECIMAL_MULTIPLIER 100.0f
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)
2022-01-23 16:10:35 +00:00
//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
2022-01-23 16:10:35 +00:00
//exponential regression a params
2024-07-28 13:15:35 +01:00
# define ANALOG_MQ_A 574.25f
2022-01-23 16:10:35 +00:00
//exponential regression b params
2024-07-28 13:15:35 +01:00
# define ANALOG_MQ_B -2.222f
2022-01-23 16:10:35 +00:00
/*
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
2022-01-23 16:10:35 +00:00
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)
2024-07-28 13:15:35 +01:00
# define ANALOG_MQ_RatioMQCleanAir 15.0f
2022-01-23 16:10:35 +00:00
// Multiplier used to store pH with 2 decimal places in a non decimal datatype
2024-07-28 13:15:35 +01:00
# define ANALOG_MQ_DECIMAL_MULTIPLIER 100.0f
2022-01-23 22:54:29 +00:00
// lenght of filter
# define ANALOG_MQ_SAMPLES 60
2022-01-23 16:10:35 +00:00
2024-08-09 15:11:30 +01:00
/*********************************************************************************************/
2020-08-04 15:33:05 +01:00
struct {
2024-08-04 15:35:02 +01:00
uint8_t present ;
2020-08-04 15:33:05 +01:00
} Adcs ;
2020-08-03 17:21:34 +01:00
struct {
2024-07-26 10:01:09 +01:00
float * mq_samples ;
2024-08-04 15:35:02 +01:00
float temperature ;
float current ;
float energy ;
2024-08-09 15:11:30 +01:00
int param [ 4 ] ;
2024-08-04 15:35:02 +01:00
int indexOfPointer ;
2024-08-09 15:11:30 +01:00
uint32_t previous_millis ;
2024-08-04 15:35:02 +01:00
uint16_t last_value ;
2024-08-03 16:52:47 +01:00
uint16_t type ;
2024-08-04 15:35:02 +01:00
uint8_t index ;
uint8_t pin ;
2020-08-03 17:21:34 +01:00
} Adc [ MAX_ADCS ] ;
2024-08-03 16:52:47 +01:00
/*********************************************************************************************\
* External use
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
uint32_t AdcRange ( void ) {
return ANALOG_RANGE ;
2020-09-25 17:15:31 +01:00
}
2024-08-03 16:52:47 +01:00
bool AdcPin ( uint32_t pin ) {
for ( uint32_t channel = 0 ; channel < Adcs . present ; channel + + ) {
if ( pin = = Adc [ channel ] . pin ) {
return true ;
}
}
return false ;
}
2024-08-09 15:11:30 +01:00
/*********************************************************************************************/
# ifdef ESP32
void AdcFreeUnusedSettings ( void ) {
// Go over all SET_ADC_PARAMx looking for pin numbers currently used on channels
// Clear non used freeing global text space
char parameters [ 40 ] ;
for ( uint32_t param_idx = 0 ; param_idx < = MAX_ADCS ; param_idx + + ) {
if ( strchr ( SettingsText ( SET_ADC_PARAM1 + param_idx ) , ' , ' ) ! = nullptr ) {
uint32_t pin = atoi ( subStr ( parameters , SettingsText ( SET_ADC_PARAM1 + param_idx ) , " , " , 6 ) ) ;
uint32_t channel ;
for ( channel = 0 ; channel < Adcs . present ; channel + + ) {
if ( pin = = Adc [ channel ] . pin ) { break ; }
}
if ( channel = = Adcs . present ) {
SettingsUpdateText ( SET_ADC_PARAM1 + param_idx , " " ) ;
}
}
}
2024-08-03 16:52:47 +01:00
}
2024-08-09 15:11:30 +01:00
# endif // ESP32
2024-08-03 16:52:47 +01:00
2024-08-09 15:11:30 +01:00
int AdcFindSlot ( uint32_t channel ) {
char parameters [ 40 ] ;
for ( uint32_t param_idx = 0 ; param_idx < MAX_ADCS ; param_idx + + ) {
if ( strchr ( SettingsText ( SET_ADC_PARAM1 + param_idx ) , ' , ' ) ! = nullptr ) {
// if (Adc[channel].pin == atoi(subStr(parameters, SettingsText(SET_ADC_PARAM1 + param_idx), ",", 6))) { // Assuming pin 0 is never an ADC channel
subStr ( parameters , SettingsText ( SET_ADC_PARAM1 + param_idx ) , " , " , 6 ) ;
if ( strlen ( parameters ) & & ( atoi ( parameters ) = = Adc [ channel ] . pin ) ) {
return param_idx ; // Found
}
}
}
return - 1 ; // Not found
}
2024-08-03 16:52:47 +01:00
void AdcSaveSettings ( uint32_t channel ) {
2024-08-09 15:11:30 +01:00
char parameters [ 40 ] ;
snprintf_P ( parameters , sizeof ( parameters ) , PSTR ( " %d,%d,%d,%d,%d,%d " ) ,
Adc [ channel ] . type ,
Adc [ channel ] . param [ 0 ] , Adc [ channel ] . param [ 1 ] , Adc [ channel ] . param [ 2 ] , Adc [ channel ] . param [ 3 ] ,
Adc [ channel ] . pin ) ;
# ifdef ESP8266
SettingsUpdateText ( SET_ADC_PARAM1 , parameters ) ; // Save in only slot
# else // ESP32
// Find used slot based on channel pin. If not find a free slot.
int param_idx = AdcFindSlot ( channel ) ;
if ( - 1 = = param_idx ) {
for ( param_idx = 0 ; param_idx < MAX_ADCS ; param_idx + + ) { // Find a free slot
if ( strchr ( SettingsText ( SET_ADC_PARAM1 + param_idx ) , ' , ' ) = = nullptr ) {
break ;
}
}
}
SettingsUpdateText ( SET_ADC_PARAM1 + param_idx , parameters ) ; // Save in current slot
# endif // ESP32
}
uint32_t AdcGetType ( uint32_t channel , uint32_t param_idx ) {
// Get params and adc_type
char parameters [ 40 ] ;
for ( uint32_t i = 0 ; i < 4 ; i + + ) {
Adc [ channel ] . param [ i ] = atoi ( subStr ( parameters , SettingsText ( SET_ADC_PARAM1 + param_idx ) , " , " , i + 2 ) ) ;
2024-08-04 15:35:02 +01:00
}
2024-08-09 15:11:30 +01:00
return atoi ( subStr ( parameters , SettingsText ( SET_ADC_PARAM1 + param_idx ) , " , " , 1 ) ) ;
2020-08-04 15:33:05 +01:00
}
2024-08-03 16:52:47 +01:00
bool AdcGetSettings ( uint32_t channel ) {
uint32_t adc_type = 0 ;
2024-08-09 15:11:30 +01:00
// Find corresponding pin (since v14.1.0.4)
int param_idx = AdcFindSlot ( channel ) ;
if ( param_idx > - 1 ) {
// Param is 148,32000,10000,3350,0,17
adc_type = AdcGetType ( channel , param_idx ) ;
} else {
// Legacy support (No pin in param)
// Param is 1,32000,10000,33500000,0 or 148,32000,10000,33500000,0 or 148,32000,10000,3350,0
if ( strchr ( SettingsText ( SET_ADC_PARAM1 + channel ) , ' , ' ) ! = nullptr ) {
adc_type = AdcGetType ( channel , channel ) ;
if ( ( adc_type > 0 ) & & ( adc_type < GPIO_ADC_INPUT ) ) { // Former ADC_END
adc_type = Adc [ channel ] . type ; // Migrate adc_type from 1..12 to UserSelectablePins index
}
2024-08-13 12:57:58 +01:00
if ( GPIO_ADC_INPUT = = adc_type ) {
Adc [ channel ] . param [ 2 ] = ANALOG_MARGIN ; // Margin / Tolerance
Adc [ channel ] . param [ 3 ] = 0 ; // Default mode (0) or Direct mode (1) using Dimmer or Channel command
}
2024-08-09 15:11:30 +01:00
if ( ( GPIO_ADC_TEMP = = adc_type ) & & ( Adc [ channel ] . param [ 2 ] > 1000000 ) ) {
Adc [ channel ] . param [ 2 ] / = 10000 ; // Fix legacy value from 33500000 to 3350
}
AdcSaveSettings ( channel ) ; // Add pin
2020-08-04 15:33:05 +01:00
}
}
2024-08-04 15:35:02 +01:00
return ( Adc [ channel ] . type = = adc_type ) ;
2020-08-04 15:33:05 +01:00
}
2024-08-03 16:52:47 +01:00
void AdcInitParams ( uint32_t channel ) {
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 0 ] = 0 ;
Adc [ channel ] . param [ 1 ] = 0 ;
Adc [ channel ] . param [ 2 ] = 0 ;
Adc [ channel ] . param [ 3 ] = 0 ;
2024-08-04 15:35:02 +01:00
uint32_t adc_type = Adc [ channel ] . type ;
2024-08-03 16:52:47 +01:00
switch ( adc_type ) {
case GPIO_ADC_INPUT :
2024-08-09 15:11:30 +01:00
// Adc[channel].param[0] = 0;
Adc [ channel ] . param [ 1 ] = ANALOG_RANGE ;
2024-08-13 12:57:58 +01:00
Adc [ channel ] . param [ 2 ] = ANALOG_MARGIN ; // Margin / Tolerance
2024-08-09 15:11:30 +01:00
// Adc[channel].param[3] = 0; // Default mode (0) or Direct mode (1) using Dimmer or Channel command
2024-08-03 16:52:47 +01:00
break ;
case GPIO_ADC_TEMP :
// Default Shelly 2.5 and 1PM parameters
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 0 ] = ANALOG_NTC_BRIDGE_RESISTANCE ;
Adc [ channel ] . param [ 1 ] = ANALOG_NTC_RESISTANCE ;
Adc [ channel ] . param [ 2 ] = ANALOG_NTC_B_COEFFICIENT ;
// Adc[channel].param[3] = 0; // Default to Shelly mode with NTC towards GND
2024-08-03 16:52:47 +01:00
break ;
case GPIO_ADC_LIGHT :
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 0 ] = ANALOG_LDR_BRIDGE_RESISTANCE ;
Adc [ channel ] . param [ 1 ] = ANALOG_LDR_LUX_CALC_SCALAR ;
Adc [ channel ] . param [ 2 ] = ANALOG_LDR_LUX_CALC_EXPONENT * 10000 ;
// Adc[channel].param[3] = 0;
2024-08-03 16:52:47 +01:00
break ;
case GPIO_ADC_BUTTON :
case GPIO_ADC_BUTTON_INV :
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 0 ] = ANALOG_BUTTON_THRESHOLD ; // Between 0 or 1
// Adc[channel].param[1] = 0;
// Adc[channel].param[2] = 0;
// Adc[channel].param[3] = 0;
2024-08-03 16:52:47 +01:00
break ;
case GPIO_ADC_RANGE :
2024-08-09 15:11:30 +01:00
// Adc[channel].param[0] = 0;
Adc [ channel ] . param [ 1 ] = ANALOG_RANGE ;
// Adc[channel].param[2] = 0;
Adc [ channel ] . param [ 3 ] = 100 ;
2024-08-03 16:52:47 +01:00
break ;
case GPIO_ADC_CT_POWER :
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 0 ] = ANALOG_CT_FLAGS ; // (uint32_t) 0
Adc [ channel ] . param [ 1 ] = ANALOG_CT_MULTIPLIER ; // (uint32_t) 100000
Adc [ channel ] . param [ 2 ] = ANALOG_CT_VOLTAGE ; // (int) 10
// Adc[channel].param[3] = 0;
2024-08-03 16:52:47 +01:00
break ;
case GPIO_ADC_JOY :
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 0 ] = ANALOG_JOYSTICK_THRESHOLD ;
// Adc[channel].param[1] = 0;
// Adc[channel].param[2] = 0;
// Adc[channel].param[3] = 0;
2024-08-03 16:52:47 +01:00
break ;
case GPIO_ADC_PH :
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 0 ] = ANALOG_PH_CALSOLUTION_LOW_PH * ANALOG_PH_DECIMAL_MULTIPLIER ; // PH of the calibration solution 1, which is the one with the lower PH
Adc [ channel ] . param [ 1 ] = ANALOG_PH_CALSOLUTION_LOW_ANALOG_VALUE ; // Reading of AnalogInput while probe is in solution 1
Adc [ channel ] . param [ 2 ] = ANALOG_PH_CALSOLUTION_HIGH_PH * ANALOG_PH_DECIMAL_MULTIPLIER ; // PH of the calibration solution 2, which is the one with the higher PH
Adc [ channel ] . param [ 3 ] = ANALOG_PH_CALSOLUTION_HIGH_ANALOG_VALUE ; // Reading of AnalogInput while probe is in solution 2
2024-08-03 16:52:47 +01:00
break ;
case GPIO_ADC_MQ :
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 0 ] = ANALOG_MQ_TYPE ; // Could be MQ-002, MQ-004, MQ-131 ....
Adc [ channel ] . param [ 1 ] = ( int ) ( ANALOG_MQ_A * ANALOG_MQ_DECIMAL_MULTIPLIER ) ; // Exponential regression
Adc [ channel ] . param [ 2 ] = ( int ) ( ANALOG_MQ_B * ANALOG_MQ_DECIMAL_MULTIPLIER ) ; // Exponential regression
Adc [ channel ] . param [ 3 ] = ( int ) ( ANALOG_MQ_RatioMQCleanAir * ANALOG_MQ_DECIMAL_MULTIPLIER ) ; // Exponential regression
2024-08-03 16:52:47 +01:00
break ;
case GPIO_ADC_VOLTAGE :
case GPIO_ADC_CURRENT :
2024-08-09 15:11:30 +01:00
// Adc[channel].param[0] = 0;
Adc [ channel ] . param [ 1 ] = ANALOG_RANGE ;
// Adc[channel].param[2] = 0;
Adc [ channel ] . param [ 3 ] = ANALOG_V33 * 10000 ;
2024-08-03 16:52:47 +01:00
break ;
2020-08-03 17:21:34 +01:00
}
2024-08-03 16:52:47 +01:00
2024-08-09 15:11:30 +01:00
// AddLog(LOG_LEVEL_DEBUG, PSTR("ADC: AdcParam%d %d,%d,%d,%d,%d"), channel+1, Adc[channel].pin, Adc[channel].param[0], Adc[channel].param[1], Adc[channel].param[2], Adc[channel].param[3]);
2020-08-03 17:21:34 +01:00
}
void AdcInit ( void ) {
2020-08-04 15:33:05 +01:00
Adcs . present = 0 ;
2024-08-04 15:35:02 +01:00
memset ( & Adc , 0 , sizeof ( Adc ) ) ;
2024-08-03 16:52:47 +01:00
uint32_t pin = 0 ; // ESP32 full range of GPIOs possible for ADC channels
# ifdef ESP8266
pin = ADC0_PIN ; // ESP8266 single ADC channel
# endif
for ( pin ; pin < nitems ( TasmotaGlobal . gpio_pin ) ; pin + + ) {
uint32_t adc_type = TasmotaGlobal . gpio_pin [ pin ] > > 5 ;
switch ( adc_type ) {
case GPIO_ADC_MQ :
Adc [ Adcs . present ] . mq_samples = ( float * ) calloc ( sizeof ( float ) , ANALOG_MQ_SAMPLES ) ; // Need calloc to reset registers to 0
if ( nullptr = = Adc [ Adcs . present ] . mq_samples ) { continue ; }
case GPIO_ADC_INPUT :
2024-08-13 14:11:51 +01:00
case GPIO_ADC_TEMP :
case GPIO_ADC_LIGHT :
case GPIO_ADC_BUTTON :
case GPIO_ADC_BUTTON_INV :
case GPIO_ADC_RANGE :
case GPIO_ADC_CT_POWER :
case GPIO_ADC_JOY :
case GPIO_ADC_PH :
case GPIO_ADC_VOLTAGE :
case GPIO_ADC_CURRENT :
2024-08-09 15:11:30 +01:00
Adc [ Adcs . present ] . indexOfPointer = - 1 ; // Used to skip first update of GPIO_ADC_INPUT after restart
2024-08-03 16:52:47 +01:00
Adc [ Adcs . present ] . pin = pin ;
2024-08-04 15:35:02 +01:00
Adc [ Adcs . present ] . type = adc_type ;
Adc [ Adcs . present ] . index = TasmotaGlobal . gpio_pin [ pin ] & 0x001F ;
2024-08-03 16:52:47 +01:00
Adcs . present + + ;
if ( Adcs . present = = MAX_ADCS ) { break ; }
2020-10-08 17:27:12 +01:00
}
}
2020-08-04 15:33:05 +01:00
if ( Adcs . present ) {
2020-09-25 17:15:31 +01:00
# ifdef ESP32
2024-07-25 17:04:01 +01:00
analogReadResolution ( ANALOG_RESOLUTION ) ; // Default 12 bits (0 - 4095)
analogSetAttenuation ( ADC_11db ) ; // Default 11db
2020-09-25 17:15:31 +01:00
# endif
2024-08-03 16:52:47 +01:00
for ( uint32_t channel = 0 ; channel < Adcs . present ; channel + + ) {
if ( ! AdcGetSettings ( channel ) ) {
AdcInitParams ( channel ) ;
AdcSaveSettings ( channel ) ;
}
2024-01-07 14:10:19 +00:00
}
}
2024-08-09 15:11:30 +01:00
# ifdef ESP32
AdcFreeUnusedSettings ( ) ;
# endif // ESP32
2024-01-12 11:17:31 +00:00
}
2024-08-09 15:11:30 +01:00
/*********************************************************************************************/
uint32_t AdcRead1 ( uint32_t pin ) {
# ifdef ESP32
return analogReadMilliVolts ( pin ) / ( ANALOG_V33 * 1000 ) * ANALOG_RANGE ; // Go back from mV to ADC
# else
return analogRead ( pin ) ;
# endif
}
uint32_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
2023-05-24 16:24:48 +01:00
SystemBusyDelayExecute ( ) ;
2023-05-24 15:21:59 +01:00
2020-08-04 15:33:05 +01:00
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 + + ) {
2024-01-13 14:16:34 +00:00
# ifdef ESP32
analog + = analogReadMilliVolts ( pin ) ; // get the value corrected by calibrated values from the eFuses
# else
analog + = analogRead ( pin ) ;
# endif
2020-08-03 17:21:34 +01:00
delay ( 1 ) ;
}
analog > > = factor ;
2024-01-13 14:16:34 +00:00
# ifdef ESP32
2024-07-28 13:15:35 +01:00
analog = analog / ( ANALOG_V33 * 1000 ) * ANALOG_RANGE ; // Go back from mV to ADC
2024-01-13 14:16:34 +00:00
# endif
2020-08-03 17:21:34 +01:00
return analog ;
}
void AdcEvery250ms ( void ) {
2024-08-03 16:52:47 +01:00
char adc_channel [ 3 ] = { 0 } ;
2020-09-29 17:10:21 +01:00
uint32_t offset = 0 ;
2024-08-04 15:35:02 +01:00
uint32_t dimmer_count = 0 ;
# ifdef USE_LIGHT
if ( ! light_controller . isCTRGBLinked ( ) ) { // SetOption37 >= 128 (Light) RGB and White channel separation (default 0)
for ( uint32_t channel = 0 ; channel < Adcs . present ; channel + + ) {
2024-08-09 15:11:30 +01:00
if ( ( GPIO_ADC_INPUT = = Adc [ channel ] . type ) & & ( Adc [ channel ] . param [ 3 ] > 0 ) ) {
2024-08-04 15:35:02 +01:00
dimmer_count + + ;
}
}
}
# endif // USE_LIGHT
2024-08-03 16:52:47 +01:00
for ( uint32_t channel = 0 ; channel < Adcs . present ; channel + + ) {
2024-08-04 15:35:02 +01:00
uint32_t type_index = Adc [ channel ] . index ;
2020-09-29 17:10:21 +01:00
# ifdef ESP32
2024-08-03 16:52:47 +01:00
snprintf_P ( adc_channel , sizeof ( adc_channel ) , PSTR ( " %d " ) , type_index + 1 ) ;
2020-09-29 17:10:21 +01:00
offset = 1 ;
# endif
2024-08-04 15:35:02 +01:00
uint32_t adc_type = Adc [ channel ] . type ;
2024-08-09 15:11:30 +01:00
int param0 = Adc [ channel ] . param [ 0 ] ;
int param1 = Adc [ channel ] . param [ 1 ] ;
int param2 = Adc [ channel ] . param [ 2 ] ;
int param3 = Adc [ channel ] . param [ 3 ] ;
2024-08-03 16:52:47 +01:00
if ( GPIO_ADC_INPUT = = adc_type ) {
int adc = AdcRead ( Adc [ channel ] . pin , 4 ) ; // 4 = 16 mS
2024-08-09 15:11:30 +01:00
bool swap = ( param1 < param0 ) ;
uint32_t lo = ( swap ) ? param1 : param0 ;
uint32_t hi = ( swap ) ? param0 : param1 ;
2024-08-03 16:52:47 +01:00
int new_value = changeUIntScale ( adc , lo , hi , 0 , 100 ) ;
if ( swap ) {
new_value = 100 - new_value ;
}
2024-08-09 15:11:30 +01:00
if ( ( new_value < Adc [ channel ] . last_value - param2 ) | |
( new_value > Adc [ channel ] . last_value + param2 ) | |
2024-08-03 16:52:47 +01:00
( ( 0 = = new_value ) & & ( Adc [ channel ] . last_value ! = 0 ) ) | | // Lowest end
( ( 100 = = new_value ) & & ( Adc [ channel ] . last_value ! = 100 ) ) ) { // Highest end
Adc [ channel ] . last_value = new_value ;
if ( - 1 = = Adc [ channel ] . indexOfPointer ) {
Adc [ channel ] . indexOfPointer = 0 ;
continue ; // Do not use potentiometer state on restart
Analog GPIO changes
- Analog GPIO ``ADC Input`` with ``AdcParam<x> 1,<start_range>,<end_range>,<margin>,1`` provide direct light control
- Analog GPIO ``ADC Voltage`` with ``AdcParam<x> 11,<start_range>,<end_range>,<lowest_voltage>,<highest_voltage>`` provide energy monitoring with dc voltage
- Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage
2024-07-28 17:52:38 +01:00
}
# ifdef USE_LIGHT
2024-08-13 12:57:58 +01:00
if ( 0 = = param3 ) { // Default (0) or Direct mode (1)
Analog GPIO changes
- Analog GPIO ``ADC Input`` with ``AdcParam<x> 1,<start_range>,<end_range>,<margin>,1`` provide direct light control
- Analog GPIO ``ADC Voltage`` with ``AdcParam<x> 11,<start_range>,<end_range>,<lowest_voltage>,<highest_voltage>`` provide energy monitoring with dc voltage
- Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage
2024-07-28 17:52:38 +01:00
# endif // USE_LIGHT
2024-08-03 16:52:47 +01:00
Response_P ( PSTR ( " { \" ANALOG \" :{ \" A%ddiv10 \" :%d}} " ) , type_index + offset , new_value ) ;
Analog GPIO changes
- Analog GPIO ``ADC Input`` with ``AdcParam<x> 1,<start_range>,<end_range>,<margin>,1`` provide direct light control
- Analog GPIO ``ADC Voltage`` with ``AdcParam<x> 11,<start_range>,<end_range>,<lowest_voltage>,<highest_voltage>`` provide energy monitoring with dc voltage
- Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage
2024-07-28 17:52:38 +01:00
XdrvRulesProcess ( 0 ) ;
# ifdef USE_LIGHT
} else {
char command [ 33 ] ;
2024-08-04 15:35:02 +01:00
if ( Settings - > flag3 . pwm_multi_channels ) { // SetOption68 - Enable multi-channels PWM instead of Color PWM
2024-08-03 16:52:47 +01:00
snprintf_P ( command , sizeof ( command ) , PSTR ( D_CMND_CHANNEL " %d %d " ) , type_index + 1 , new_value ) ;
Analog GPIO changes
- Analog GPIO ``ADC Input`` with ``AdcParam<x> 1,<start_range>,<end_range>,<margin>,1`` provide direct light control
- Analog GPIO ``ADC Voltage`` with ``AdcParam<x> 11,<start_range>,<end_range>,<lowest_voltage>,<highest_voltage>`` provide energy monitoring with dc voltage
- Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage
2024-07-28 17:52:38 +01:00
} else {
2024-08-04 15:35:02 +01:00
uint32_t dimmer_option ;
if ( dimmer_count > 1 ) {
dimmer_option = ( 0 = = type_index ) ? 1 : 2 ; // Change RGB (1) or W(W) (2) dimmer
} else {
2024-08-13 12:57:58 +01:00
dimmer_option = ( 3 = = param3 ) ? 3 : 0 ; // Change both RGB and W(W) Dimmers (0) with no fading (3)
2024-08-04 15:35:02 +01:00
}
snprintf_P ( command , sizeof ( command ) , PSTR ( D_CMND_DIMMER " %d %d " ) , dimmer_option , new_value ) ;
Analog GPIO changes
- Analog GPIO ``ADC Input`` with ``AdcParam<x> 1,<start_range>,<end_range>,<margin>,1`` provide direct light control
- Analog GPIO ``ADC Voltage`` with ``AdcParam<x> 11,<start_range>,<end_range>,<lowest_voltage>,<highest_voltage>`` provide energy monitoring with dc voltage
- Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage
2024-07-28 17:52:38 +01:00
}
ExecuteCommand ( command , SRC_SWITCH ) ;
}
# endif // USE_LIGHT
2020-08-03 17:21:34 +01:00
}
}
2024-08-03 16:52:47 +01:00
else if ( GPIO_ADC_JOY = = adc_type ) {
uint16_t new_value = AdcRead ( Adc [ channel ] . pin , 1 ) ;
if ( new_value & & ( new_value ! = Adc [ channel ] . last_value ) ) {
Adc [ channel ] . last_value = new_value ;
2024-08-09 15:11:30 +01:00
uint16_t value = new_value / param0 ;
2024-08-03 16:52:47 +01:00
Response_P ( PSTR ( " { \" ANALOG \" :{ \" Joy%s \" :%d}} " ) , adc_channel , value ) ;
2021-04-04 11:04:36 +01:00
XdrvRulesProcess ( 0 ) ;
2020-08-04 15:33:05 +01:00
} else {
2024-08-03 16:52:47 +01:00
Adc [ channel ] . last_value = 0 ;
2020-08-04 15:33:05 +01:00
}
}
2020-08-03 17:21:34 +01:00
}
}
2020-10-08 17:27:12 +01:00
uint8_t AdcGetButton ( uint32_t pin ) {
2024-08-03 16:52:47 +01:00
for ( uint32_t channel = 0 ; channel < Adcs . present ; channel + + ) {
if ( Adc [ channel ] . pin = = pin ) {
2024-08-09 15:11:30 +01:00
uint32_t adc_type = Adc [ channel ] . type ;
uint32_t adc = AdcRead ( Adc [ channel ] . pin , 1 ) ;
uint32_t param0 = Adc [ channel ] . param [ 0 ] ;
2024-08-03 16:52:47 +01:00
if ( GPIO_ADC_BUTTON_INV = = adc_type ) {
2024-08-09 15:11:30 +01:00
return ( adc < param0 ) ;
2020-10-08 17:27:12 +01:00
}
2024-08-03 16:52:47 +01:00
else if ( GPIO_ADC_BUTTON = = adc_type ) {
2024-08-09 15:11:30 +01:00
return ( adc > param0 ) ;
2020-10-08 17:27:12 +01:00
}
}
2020-09-29 13:08:48 +01:00
}
2020-10-08 17:27:12 +01:00
return 0 ;
2020-09-29 13:08:48 +01:00
}
2024-08-09 15:11:30 +01:00
uint32_t AdcGetLux ( uint32_t channel ) {
2024-08-03 16:52:47 +01:00
int adc = AdcRead ( Adc [ channel ] . pin , 2 ) ;
2020-08-03 17:21:34 +01:00
// Source: https://www.allaboutcircuits.com/projects/design-a-luxmeter-using-a-light-dependent-resistor/
2024-08-09 15:11:30 +01:00
float resistorVoltage = ( ( float ) adc / ANALOG_RANGE ) * ANALOG_V33 ;
float ldrVoltage = ANALOG_V33 - resistorVoltage ;
float ldrResistance = ldrVoltage / resistorVoltage * ( float ) Adc [ channel ] . param [ 0 ] ;
float ldrLux = ( float ) Adc [ channel ] . param [ 1 ] * FastPrecisePowf ( ldrResistance , ( float ) Adc [ channel ] . param [ 2 ] / 10000 ) ;
2020-08-03 17:21:34 +01:00
2024-08-09 15:11:30 +01:00
return ( uint32_t ) ldrLux ;
2020-08-03 17:21:34 +01:00
}
2024-08-03 16:52:47 +01:00
void AddSampleMq ( uint32_t channel ) {
2022-12-12 09:57:21 +00:00
// AddLog(LOG_LEVEL_DEBUG, PSTR("ADC: Adding sample for mq-sensor"));
2024-08-03 16:52:47 +01:00
int _adc = AdcRead ( Adc [ channel ] . pin , 2 ) ;
2022-01-23 22:54:29 +00:00
// init af array at same value
2024-08-03 16:52:47 +01:00
if ( Adc [ channel ] . indexOfPointer = = - 1 ) {
2022-12-12 09:57:21 +00:00
// AddLog(LOG_LEVEL_DEBUG, PSTR("ADC: Init samples for mq-sensor"));
for ( int i = 0 ; i < ANALOG_MQ_SAMPLES ; i + + ) {
2024-08-03 16:52:47 +01:00
Adc [ channel ] . mq_samples [ i ] = _adc ;
2022-12-12 09:57:21 +00:00
}
} else {
2024-08-03 16:52:47 +01:00
Adc [ channel ] . mq_samples [ Adc [ channel ] . indexOfPointer ] = _adc ;
2022-12-12 09:57:21 +00:00
}
2024-08-03 16:52:47 +01:00
Adc [ channel ] . indexOfPointer + + ;
if ( Adc [ channel ] . indexOfPointer = = ANALOG_MQ_SAMPLES ) {
Adc [ channel ] . indexOfPointer = 0 ;
2022-12-12 09:57:21 +00:00
}
2022-01-23 22:54:29 +00:00
}
2024-08-03 16:52:47 +01:00
float AdcGetMq ( uint32_t channel ) {
2022-12-12 09:57:21 +00:00
// AddLog(LOG_LEVEL_DEBUG, PSTR("ADC: Getting value for mq-sensor"));
2022-01-23 16:10:35 +00:00
float avg = 0.0 ;
2022-12-12 09:57:21 +00:00
for ( int i = 0 ; i < ANALOG_MQ_SAMPLES ; i + + ) {
2024-08-03 16:52:47 +01:00
avg + = Adc [ channel ] . mq_samples [ i ] ;
2022-12-12 09:57:21 +00:00
}
2022-08-22 13:32:08 +01:00
float voltage = ( avg / ANALOG_MQ_SAMPLES ) * ANALOG_V33 / ANALOG_RANGE ;
2022-01-23 16:10:35 +00:00
2022-12-12 09:57:21 +00:00
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
2024-08-09 15:11:30 +01:00
float ppm = Adc [ channel ] . param [ 1 ] / ANALOG_MQ_DECIMAL_MULTIPLIER * FastPrecisePowf ( _ratio , Adc [ channel ] . param [ 2 ] / ANALOG_MQ_DECIMAL_MULTIPLIER ) ; // Source excel analisis https://github.com/miguel5612/MQSensorsLib_Docs/tree/master/Internal_design_documents
2022-12-12 09:57:21 +00:00
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);
2022-01-23 16:10:35 +00:00
return ppm ;
}
2024-08-03 16:52:47 +01:00
float AdcGetPh ( uint32_t channel ) {
int adc = AdcRead ( Adc [ channel ] . pin , 2 ) ;
2021-01-05 15:25:56 +00:00
2024-08-09 15:11:30 +01:00
float y1 = ( float ) Adc [ channel ] . param [ 0 ] / ANALOG_PH_DECIMAL_MULTIPLIER ;
int32_t x1 = Adc [ channel ] . param [ 1 ] ;
float y2 = ( float ) Adc [ channel ] . param [ 2 ] / ANALOG_PH_DECIMAL_MULTIPLIER ;
int32_t x2 = Adc [ channel ] . param [ 3 ] ;
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
2022-12-12 09:57:21 +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 ;
}
2024-08-03 16:52:47 +01:00
float AdcGetRange ( uint32_t channel ) {
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> )
2024-08-03 16:52:47 +01:00
int adc = AdcRead ( Adc [ channel ] . pin , 5 ) ;
2024-08-09 15:11:30 +01:00
float adcrange = ( ( ( float ) Adc [ channel ] . param [ 1 ] - ( float ) adc ) / ( ( ( float ) Adc [ channel ] . param [ 1 ] - ( float ) Adc [ channel ] . param [ 0 ] ) ) * ( ( float ) Adc [ channel ] . param [ 2 ] - ( float ) Adc [ channel ] . param [ 3 ] ) + ( float ) Adc [ channel ] . param [ 3 ] ) ;
2024-07-28 13:15:35 +01:00
return adcrange ;
2020-08-03 17:21:34 +01:00
}
2024-08-09 15:11:30 +01:00
void AdcGetCurrentPower ( uint32_t channel , 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
2024-08-09 15:11:30 +01:00
uint32_t samples = 1 < < factor ;
uint32_t analog = 0 ;
uint32_t analog_min = ANALOG_RANGE ;
uint32_t analog_max = 0 ;
2020-08-03 17:21:34 +01:00
2024-08-09 15:11:30 +01:00
if ( 0 = = Adc [ channel ] . param [ 0 ] ) {
uint32_t tstart = millis ( ) ;
2023-04-08 20:31:50 +01:00
while ( millis ( ) - tstart < 35 ) {
2024-08-09 15:11:30 +01:00
analog = AdcRead1 ( Adc [ channel ] . pin ) ;
2020-08-03 17:21:34 +01:00
if ( analog < analog_min ) {
analog_min = analog ;
}
if ( analog > analog_max ) {
analog_max = analog ;
}
}
2023-04-08 20:31:50 +01:00
//AddLog(0, PSTR("min: %u, max:%u, dif:%u"), analog_min, analog_max, analog_max-analog_min);
2024-08-09 15:11:30 +01:00
Adc [ channel ] . current = ( float ) ( analog_max - analog_min ) * ( ( float ) ( Adc [ channel ] . param [ 1 ] ) / 100000 ) ;
if ( Adc [ channel ] . current < ( ( ( float ) Adc [ channel ] . param [ 3 ] ) / 10000.0 ) ) {
Adc [ channel ] . current = 0.0 ;
2020-08-03 17:21:34 +01:00
}
2024-08-09 15:11:30 +01:00
} else {
analog = AdcRead ( Adc [ channel ] . pin , 5 ) ;
if ( analog > Adc [ channel ] . param [ 0 ] ) {
Adc [ channel ] . current = ( ( float ) ( analog ) - ( float ) Adc [ channel ] . param [ 0 ] ) * ( ( float ) ( Adc [ channel ] . param [ 1 ] ) / 100000 ) ;
} else {
2024-08-03 16:52:47 +01:00
Adc [ channel ] . current = 0 ;
2020-08-03 17:21:34 +01:00
}
}
2024-08-09 15:11:30 +01:00
float power = Adc [ channel ] . current * ( float ) ( Adc [ channel ] . param [ 2 ] ) / 10 ;
2020-08-03 17:21:34 +01:00
uint32_t current_millis = millis ( ) ;
2024-08-03 16:52:47 +01:00
Adc [ channel ] . energy = Adc [ channel ] . energy + ( ( power * ( current_millis - Adc [ channel ] . previous_millis ) ) / 3600000000 ) ;
Adc [ channel ] . previous_millis = current_millis ;
2020-08-03 17:21:34 +01:00
}
void AdcEverySecond ( void ) {
2024-08-03 16:52:47 +01:00
for ( uint32_t channel = 0 ; channel < Adcs . present ; channel + + ) {
2024-08-04 15:35:02 +01:00
uint32_t type_index = Adc [ channel ] . index ;
uint32_t adc_type = Adc [ channel ] . type ;
2024-08-09 15:11:30 +01:00
int param0 = Adc [ channel ] . param [ 0 ] ;
int param1 = Adc [ channel ] . param [ 1 ] ;
int param2 = Adc [ channel ] . param [ 2 ] ;
int param3 = Adc [ channel ] . param [ 3 ] ;
2024-08-03 16:52:47 +01:00
if ( GPIO_ADC_TEMP = = adc_type ) {
int adc = AdcRead ( Adc [ channel ] . pin , 2 ) ;
2021-11-16 22:10:25 +00:00
// Steinhart-Hart equation for thermistor as temperature sensor:
2024-08-09 15:11:30 +01:00
// float Rt = (adc * Adc[channel].param[0] * MAX_ADC_V) / (ANALOG_RANGE * ANALOG_V33 - (float)adc * MAX_ADC_V);
2021-11-16 22:10:25 +00:00
// MAX_ADC_V in ESP8266 is 1
// MAX_ADC_V in ESP32 is 3.3
2024-08-09 15:11:30 +01:00
float Rt ;
2022-09-27 22:05:53 +01:00
# ifdef ESP8266
2024-08-09 15:11:30 +01:00
if ( param3 ) { // Alternate mode
Rt = ( float ) param0 * ( ANALOG_RANGE * ANALOG_V33 - ( float ) adc ) / ( float ) adc ;
2022-09-27 22:05:53 +01:00
} else {
2024-08-09 15:11:30 +01:00
Rt = ( float ) param0 * ( float ) adc / ( ANALOG_RANGE * ANALOG_V33 - ( float ) adc ) ;
2022-09-25 21:40:29 +01:00
}
2021-11-16 22:10:25 +00:00
# else
2024-08-09 15:11:30 +01:00
if ( param3 ) { // Alternate mode
Rt = ( float ) param0 * ( ANALOG_RANGE - ( float ) adc ) / ( float ) adc ;
2022-09-27 22:05:53 +01:00
} else {
2024-08-09 15:11:30 +01:00
Rt = ( float ) param0 * ( float ) adc / ( ANALOG_RANGE - ( float ) adc ) ;
2022-09-27 22:05:53 +01:00
}
2022-01-04 15:33:35 +00:00
# endif
2024-08-09 15:11:30 +01:00
float BC = ( float ) param2 ; // Shelly param3 = 3350 (ANALOG_NTC_B_COEFFICIENT)
float T = BC / ( BC / ANALOG_T0 + TaylorLog ( Rt / ( float ) param1 ) ) ; // Shelly param2 = 10000 (ANALOG_NTC_RESISTANCE)
2024-08-03 16:52:47 +01:00
Adc [ channel ] . temperature = ConvertTemp ( TO_CELSIUS ( T ) ) ;
2020-08-03 17:21:34 +01:00
}
2024-08-03 16:52:47 +01:00
else if ( GPIO_ADC_CT_POWER = = adc_type ) {
AdcGetCurrentPower ( channel , 5 ) ;
2020-08-03 17:21:34 +01:00
}
2024-08-03 16:52:47 +01:00
else if ( GPIO_ADC_MQ = = adc_type ) {
AddSampleMq ( channel ) ;
AdcGetMq ( channel ) ;
2022-01-23 18:19:32 +00:00
}
2024-07-27 16:11:25 +01:00
}
2020-08-03 17:21:34 +01:00
}
2024-08-06 17:07:26 +01:00
/*********************************************************************************************\
* Presentation
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2020-08-04 17:01:51 +01:00
void AdcShowContinuation ( bool * jsonflg ) {
if ( * jsonflg ) {
ResponseAppend_P ( PSTR ( " , " ) ) ;
} else {
ResponseAppend_P ( PSTR ( " , \" ANALOG \" :{ " ) ) ;
* jsonflg = true ;
}
}
2024-08-03 16:52:47 +01:00
enum DomoFlagsAdc { ADC_TEMP , ADC_LIGHT , ADC_CT_POWER , ADC_END } ;
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
2024-08-03 16:52:47 +01:00
char adc_channel [ 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 ;
2024-08-03 16:52:47 +01:00
for ( uint32_t channel = 0 ; channel < Adcs . present ; channel + + ) {
2024-08-04 15:35:02 +01:00
uint32_t type_index = Adc [ channel ] . index ;
2020-09-25 17:15:31 +01:00
# ifdef ESP32
2024-08-13 14:11:51 +01:00
snprintf_P ( adc_name , sizeof ( adc_name ) , PSTR ( " ADC%d " ) , type_index + 1 ) ;
2024-08-03 16:52:47 +01:00
snprintf_P ( adc_channel , sizeof ( adc_channel ) , PSTR ( " %d " ) , type_index + 1 ) ;
2020-09-25 17:15:31 +01:00
offset = 1 ;
# endif
2024-08-04 15:35:02 +01:00
uint32_t adc_type = Adc [ channel ] . type ;
2024-08-03 16:52:47 +01:00
switch ( adc_type ) {
case GPIO_ADC_INPUT : {
Analog GPIO changes
- Analog GPIO ``ADC Input`` with ``AdcParam<x> 1,<start_range>,<end_range>,<margin>,1`` provide direct light control
- Analog GPIO ``ADC Voltage`` with ``AdcParam<x> 11,<start_range>,<end_range>,<lowest_voltage>,<highest_voltage>`` provide energy monitoring with dc voltage
- Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage
2024-07-28 17:52:38 +01:00
# ifdef USE_LIGHT
2024-08-09 15:11:30 +01:00
if ( 0 = = Adc [ channel ] . param [ 3 ] ) { // Default (0) or Direct mode (1)
Analog GPIO changes
- Analog GPIO ``ADC Input`` with ``AdcParam<x> 1,<start_range>,<end_range>,<margin>,1`` provide direct light control
- Analog GPIO ``ADC Voltage`` with ``AdcParam<x> 11,<start_range>,<end_range>,<lowest_voltage>,<highest_voltage>`` provide energy monitoring with dc voltage
- Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage
2024-07-28 17:52:38 +01:00
# endif // USE_LIGHT
2024-08-03 16:52:47 +01:00
uint16_t analog = AdcRead ( Adc [ channel ] . pin , 5 ) ;
Analog GPIO changes
- Analog GPIO ``ADC Input`` with ``AdcParam<x> 1,<start_range>,<end_range>,<margin>,1`` provide direct light control
- Analog GPIO ``ADC Voltage`` with ``AdcParam<x> 11,<start_range>,<end_range>,<lowest_voltage>,<highest_voltage>`` provide energy monitoring with dc voltage
- Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage
2024-07-28 17:52:38 +01:00
if ( json ) {
AdcShowContinuation ( & jsonflg ) ;
2024-08-03 16:52:47 +01:00
ResponseAppend_P ( PSTR ( " \" A%d \" :%d " ) , type_index + offset , analog ) ;
2020-08-03 17:21:34 +01:00
# ifdef USE_WEBSERVER
Analog GPIO changes
- Analog GPIO ``ADC Input`` with ``AdcParam<x> 1,<start_range>,<end_range>,<margin>,1`` provide direct light control
- Analog GPIO ``ADC Voltage`` with ``AdcParam<x> 11,<start_range>,<end_range>,<lowest_voltage>,<highest_voltage>`` provide energy monitoring with dc voltage
- Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage
2024-07-28 17:52:38 +01:00
} else {
2024-08-03 16:52:47 +01:00
WSContentSend_PD ( HTTP_SNS_ANALOG , " " , type_index + offset , analog ) ;
2020-08-03 17:21:34 +01:00
# endif // USE_WEBSERVER
Analog GPIO changes
- Analog GPIO ``ADC Input`` with ``AdcParam<x> 1,<start_range>,<end_range>,<margin>,1`` provide direct light control
- Analog GPIO ``ADC Voltage`` with ``AdcParam<x> 11,<start_range>,<end_range>,<lowest_voltage>,<highest_voltage>`` provide energy monitoring with dc voltage
- Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage
2024-07-28 17:52:38 +01:00
}
# ifdef USE_LIGHT
2020-08-03 17:21:34 +01:00
}
Analog GPIO changes
- Analog GPIO ``ADC Input`` with ``AdcParam<x> 1,<start_range>,<end_range>,<margin>,1`` provide direct light control
- Analog GPIO ``ADC Voltage`` with ``AdcParam<x> 11,<start_range>,<end_range>,<lowest_voltage>,<highest_voltage>`` provide energy monitoring with dc voltage
- Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage
2024-07-28 17:52:38 +01:00
# endif // USE_LIGHT
2020-08-03 17:21:34 +01:00
break ;
}
2024-08-03 16:52:47 +01:00
case GPIO_ADC_TEMP : {
2020-08-03 17:21:34 +01:00
if ( json ) {
2020-08-04 17:01:51 +01:00
AdcShowContinuation ( & jsonflg ) ;
2024-08-03 16:52:47 +01:00
ResponseAppend_P ( PSTR ( " \" " D_JSON_TEMPERATURE " %s \" :%*_f " ) , adc_channel , Settings - > flag2 . temperature_resolution , & Adc [ channel ] . 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
2024-08-03 16:52:47 +01:00
DomoticzFloatSensor ( DZ_TEMP , Adc [ channel ] . temperature ) ;
2020-08-03 17:21:34 +01:00
domo_flag [ ADC_TEMP ] = true ;
# endif // USE_DOMOTICZ
# ifdef USE_KNX
2024-08-03 16:52:47 +01:00
KnxSensor ( KNX_TEMPERATURE , Adc [ channel ] . temperature ) ;
2020-08-03 17:21:34 +01:00
# endif // USE_KNX
}
# ifdef USE_WEBSERVER
} else {
2024-08-03 16:52:47 +01:00
WSContentSend_Temp ( adc_name , Adc [ channel ] . temperature ) ;
2020-08-03 17:21:34 +01:00
# endif // USE_WEBSERVER
}
break ;
}
2024-08-03 16:52:47 +01:00
case GPIO_ADC_LIGHT : {
2024-08-09 15:11:30 +01:00
uint32_t adc_light = AdcGetLux ( channel ) ;
2020-08-03 17:21:34 +01:00
if ( json ) {
2020-08-04 17:01:51 +01:00
AdcShowContinuation ( & jsonflg ) ;
2024-08-03 16:52:47 +01:00
ResponseAppend_P ( PSTR ( " \" " D_JSON_ILLUMINANCE " %s \" :%d " ) , adc_channel , 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 ;
}
2024-08-03 16:52:47 +01:00
case GPIO_ADC_RANGE : {
float adc_range = AdcGetRange ( channel ) ;
2021-04-02 14:50:59 +01:00
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 ) ;
2024-08-03 16:52:47 +01:00
ResponseAppend_P ( PSTR ( " \" " D_JSON_RANGE " %s \" :%s " ) , adc_channel , range_chr ) ;
2020-08-03 17:21:34 +01:00
# ifdef USE_WEBSERVER
} else {
2021-04-02 14:50:59 +01:00
WSContentSend_PD ( HTTP_SNS_RANGE_CHR , adc_name , range_chr ) ;
2020-08-03 17:21:34 +01:00
# endif // USE_WEBSERVER
}
break ;
}
2024-08-03 16:52:47 +01:00
case GPIO_ADC_CT_POWER : {
AdcGetCurrentPower ( channel , 5 ) ;
2020-08-03 17:21:34 +01:00
2024-08-09 15:11:30 +01:00
float voltage = ( float ) ( Adc [ channel ] . param [ 2 ] ) / 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 ] ;
2024-08-03 16:52:47 +01:00
dtostrfd ( Adc [ channel ] . current , Settings - > flag2 . current_resolution , current_chr ) ;
2020-08-03 17:21:34 +01:00
char power_chr [ FLOATSZ ] ;
2024-08-03 16:52:47 +01:00
dtostrfd ( voltage * Adc [ channel ] . current , Settings - > flag2 . wattage_resolution , power_chr ) ;
2020-08-03 17:21:34 +01:00
char energy_chr [ FLOATSZ ] ;
2024-08-03 16:52:47 +01:00
dtostrfd ( Adc [ channel ] . 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} " ) ,
2024-08-03 16:52:47 +01:00
adc_channel , 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 ;
}
2024-08-03 16:52:47 +01:00
case GPIO_ADC_JOY : {
uint16_t new_value = AdcRead ( Adc [ channel ] . pin , 1 ) ;
2024-08-09 15:11:30 +01:00
uint16_t value = new_value / Adc [ channel ] . param [ 0 ] ;
2020-08-04 17:01:51 +01:00
if ( json ) {
AdcShowContinuation ( & jsonflg ) ;
2024-08-03 16:52:47 +01:00
ResponseAppend_P ( PSTR ( " \" Joy%s \" :%d " ) , adc_channel , value ) ;
2020-08-04 17:01:51 +01:00
}
break ;
}
2024-08-03 16:52:47 +01:00
case GPIO_ADC_PH : {
float ph = AdcGetPh ( channel ) ;
2022-12-12 09:57:21 +00:00
char ph_chr [ FLOATSZ ] ;
2021-01-05 13:31:13 +00:00
dtostrfd ( ph , 2 , ph_chr ) ;
if ( json ) {
AdcShowContinuation ( & jsonflg ) ;
2024-08-03 16:52:47 +01:00
ResponseAppend_P ( PSTR ( " \" pH%s \" :%s " ) , adc_channel , ph_chr ) ;
2024-08-13 14:11:51 +01:00
# ifdef USE_WEBSERVER
2021-01-05 13:31:13 +00:00
} else {
WSContentSend_PD ( HTTP_SNS_PH , " " , ph_chr ) ;
2024-08-13 14:11:51 +01:00
# endif // USE_WEBSERVER
2022-01-23 16:10:35 +00:00
}
break ;
}
2024-08-03 16:52:47 +01:00
case GPIO_ADC_MQ : {
float mq = AdcGetMq ( channel ) ;
2022-12-12 09:57:21 +00:00
char mq_chr [ FLOATSZ ] ;
2022-01-23 16:10:35 +00:00
dtostrfd ( mq , 2 , mq_chr ) ;
2024-08-09 15:11:30 +01:00
float mqnumber = Adc [ channel ] . param [ 0 ] ;
2022-12-12 09:57:21 +00:00
char mqnumber_chr [ FLOATSZ ] ;
2022-01-23 16:10:35 +00:00
dtostrfd ( mqnumber , 0 , mqnumber_chr ) ;
if ( json ) {
AdcShowContinuation ( & jsonflg ) ;
2024-08-09 15:11:30 +01:00
ResponseAppend_P ( PSTR ( " \" MQ%d_%d \" :%s " ) , Adc [ channel ] . param [ 0 ] , type_index + offset , mq_chr ) ;
2024-08-13 14:11:51 +01:00
# ifdef USE_WEBSERVER
2022-01-23 16:10:35 +00:00
} else {
WSContentSend_PD ( HTTP_SNS_MQ , mqnumber_chr , mq_chr ) ;
2024-08-13 14:11:51 +01:00
# endif // USE_WEBSERVER
2021-01-05 13:31:13 +00:00
}
break ;
}
2024-08-13 14:11:51 +01:00
case GPIO_ADC_VOLTAGE :
2024-08-15 11:52:12 +01:00
# if defined(ESP32) && defined(USE_ENERGY_SENSOR)
if ( TasmotaGlobal . energy_driver ! = XNRG_33 )
# endif // ESP32 and USE_ENERGY_SENSOR
{
2024-08-13 14:11:51 +01:00
float value = AdcGetRange ( channel ) / 10000 ; // Volt
if ( value < 0.0f ) { value = 0.0f ; } // Disregard negative values
if ( json ) {
AdcShowContinuation ( & jsonflg ) ;
ResponseAppend_P ( PSTR ( " \" " D_JSON_VOLTAGE " %s \" :%*_f " ) , adc_channel , Settings - > flag2 . voltage_resolution , & value ) ;
# ifdef USE_WEBSERVER
} else {
// WSContentSend_Voltage(adc_name, value);
WSContentSend_PD ( HTTP_SNS_F_VOLTAGE , adc_name , Settings - > flag2 . voltage_resolution , & value ) ;
# endif // USE_WEBSERVER
}
}
break ;
case GPIO_ADC_CURRENT :
2024-08-15 11:52:12 +01:00
# if defined(ESP32) && defined(USE_ENERGY_SENSOR)
if ( TasmotaGlobal . energy_driver ! = XNRG_33 )
# endif // ESP32 and USE_ENERGY_SENSOR
{
2024-08-13 14:11:51 +01:00
float value = AdcGetRange ( channel ) / 10000 ; // Ampere
if ( value < 0.0f ) { value = 0.0f ; } // Disregard negative values
if ( json ) {
AdcShowContinuation ( & jsonflg ) ;
ResponseAppend_P ( PSTR ( " \" " D_JSON_CURRENT " %s \" :%*_f " ) , adc_channel , Settings - > flag2 . current_resolution , & value ) ;
# ifdef USE_WEBSERVER
} else {
WSContentSend_PD ( HTTP_SNS_F_CURRENT , adc_name , Settings - > flag2 . current_resolution , & value ) ;
# 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
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2024-08-03 16:52:47 +01:00
const char kAdcCommands [ ] PROGMEM = " Adc| " // No prefix
D_CMND_ADCPARAM " | " D_CMND_ADCGPIO ;
2020-08-03 17:21:34 +01:00
void ( * const AdcCommand [ ] ) ( void ) PROGMEM = {
2024-08-03 16:52:47 +01:00
& CmndAdcParam , & CmndAdcGpio } ;
2020-08-03 17:21:34 +01:00
2024-07-28 13:15:35 +01:00
uint32_t Decimals ( int value ) {
uint32_t decimals ;
for ( decimals = 4 ; decimals > 0 ; decimals - - ) {
if ( value % 10 ) { break ; }
value / = 10 ;
}
return decimals ;
}
2024-08-03 16:52:47 +01:00
void CmndAdcGpio ( void ) {
2024-08-09 15:11:30 +01:00
// AdcGpio33 1 Set to default
2024-08-03 16:52:47 +01:00
// AdcGpio33 32000, 10000, 3350 ADC_TEMP Shelly mode
for ( uint32_t channel = 0 ; channel < Adcs . present ; channel + + ) {
2024-08-09 15:11:30 +01:00
# ifdef ESP8266
// AdcGpio 32000, 10000, 3350 ADC_TEMP Shelly mode
XdrvMailbox . index = Adc [ channel ] . pin ;
# endif
2024-08-03 16:52:47 +01:00
if ( XdrvMailbox . index = = Adc [ channel ] . pin ) {
XdrvMailbox . index = channel + 1 ;
if ( XdrvMailbox . data_len ) {
char data [ 64 ] ;
snprintf_P ( data , sizeof ( data ) , PSTR ( " 1,%s " ) , XdrvMailbox . data ) ;
XdrvMailbox . data = data ;
XdrvMailbox . data_len = strlen ( data ) ;
}
CmndAdcParam ( ) ;
break ;
}
}
}
2020-08-03 17:21:34 +01:00
void CmndAdcParam ( void ) {
2024-08-03 16:52:47 +01:00
// AdcParam 1, 0, ANALOG_RANGE, 0 ADC_INPUT rule | dimmer
// AdcParam 1, 32000, 10000, 3350 ADC_TEMP Shelly mode
// AdcParam 1, 32000, 10000, 3350, 1 ADC_TEMP Alternate mode
// AdcParam 1, 10000, 12518931, -1.405
// AdcParam 1, 128, 0, 0
// AdcParam 1, 128, 0, 0
// AdcParam 1, 0, ANALOG_RANGE, 0, 100 ADC_RANGE
// AdcParam 1, 0, 2146, 0.23
// AdcParam 1, 1000, 0, 0
// AdcParam 1, ADC_PH
// AdcParam 1, ADC_MQ
// AdcParam 1, 0, ANALOG_RANGE, 0, 3.3 ADC_VOLTAGE
// AdcParam 1, 0, ANALOG_RANGE, 0, 3.3 ADC_CURRENT
2020-08-04 15:33:05 +01:00
if ( ( XdrvMailbox . index > 0 ) & & ( XdrvMailbox . index < = MAX_ADCS ) ) {
2024-08-03 16:52:47 +01:00
uint8_t channel = XdrvMailbox . index - 1 ;
2024-08-04 15:35:02 +01:00
uint32_t adc_type = Adc [ channel ] . type ;
2020-08-04 15:33:05 +01:00
if ( XdrvMailbox . data_len ) {
2024-08-03 16:52:47 +01:00
AdcGetSettings ( channel ) ;
2024-08-04 15:35:02 +01:00
if ( ArgC ( ) > 2 ) { // Process parameter entry
2024-08-03 16:52:47 +01:00
char argument [ XdrvMailbox . data_len ] ;
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 0 ] = strtol ( ArgV ( argument , 2 ) , nullptr , 10 ) ; // param1 = int
Adc [ channel ] . param [ 1 ] = strtol ( ArgV ( argument , 3 ) , nullptr , 10 ) ; // param2 = int
2024-08-03 16:52:47 +01:00
if ( ( GPIO_ADC_INPUT = = adc_type ) | |
( GPIO_ADC_TEMP = = adc_type ) | |
( GPIO_ADC_RANGE = = adc_type ) ) {
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 2 ] = abs ( strtol ( ArgV ( argument , 4 ) , nullptr , 10 ) ) ; // param3 = abs(int)
Adc [ channel ] . param [ 3 ] = abs ( strtol ( ArgV ( argument , 5 ) , nullptr , 10 ) ) ; // param4 = abs(int)
2024-08-03 16:52:47 +01:00
} else {
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 2 ] = ( int ) ( CharToFloat ( ArgV ( argument , 4 ) ) * 10000 ) ; // param3 = float
2024-08-03 16:52:47 +01:00
if ( ArgC ( ) > 4 ) {
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 3 ] = ( int ) ( CharToFloat ( ArgV ( argument , 5 ) ) * 10000 ) ; // param4 = float
2020-08-04 15:33:05 +01:00
} else {
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 3 ] = 0 ; // param4 = fixed 0
2021-01-05 13:31:13 +00:00
}
2024-08-03 16:52:47 +01:00
}
if ( GPIO_ADC_PH = = adc_type ) {
float phLow = CharToFloat ( ArgV ( argument , 2 ) ) ;
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 0 ] = phLow * ANALOG_PH_DECIMAL_MULTIPLIER ; // param1 = float
// Adc[channel].param[1] = strtol(ArgV(argument, 3), nullptr, 10); // param2 = int
2024-08-03 16:52:47 +01:00
float phHigh = CharToFloat ( ArgV ( argument , 4 ) ) ;
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 2 ] = phHigh * ANALOG_PH_DECIMAL_MULTIPLIER ; // param3 = float
Adc [ channel ] . param [ 3 ] = strtol ( ArgV ( argument , 5 ) , nullptr , 10 ) ; // param4 = int
2024-08-03 16:52:47 +01:00
2024-08-09 15:11:30 +01:00
// 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[channel].param[1], &phHigh, Adc[channel].param[3]);
2024-08-03 16:52:47 +01:00
}
if ( GPIO_ADC_CT_POWER = = adc_type ) {
2024-08-09 15:11:30 +01:00
if ( ( ( 1 = = Adc [ channel ] . param [ 0 ] ) & CT_FLAG_ENERGY_RESET ) > 0 ) { // param1 = int
2024-08-03 16:52:47 +01:00
for ( uint32_t i = 0 ; i < Adcs . present ; i + + ) {
Adc [ i ] . energy = 0 ;
2020-08-03 17:21:34 +01:00
}
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 0 ] ^ = CT_FLAG_ENERGY_RESET ; // Cancel energy reset flag
2020-08-03 17:21:34 +01:00
}
2024-08-03 16:52:47 +01:00
}
if ( GPIO_ADC_MQ = = adc_type ) {
float a = CharToFloat ( ArgV ( argument , 3 ) ) ; // param2 = float
float b = CharToFloat ( ArgV ( argument , 4 ) ) ; // param3 = float
float ratioMQCleanAir = CharToFloat ( ArgV ( argument , 5 ) ) ; // param4 = float
if ( ( 0 = = a ) & & ( 0 = = b ) & & ( 0 = = ratioMQCleanAir ) ) {
2024-08-09 15:11:30 +01:00
if ( 2 = = Adc [ channel ] . param [ 0 ] ) { // param1 = int
2024-08-03 16:52:47 +01:00
a = 574.25 ;
b = - 2.222 ;
ratioMQCleanAir = 9.83 ;
}
2024-08-09 15:11:30 +01:00
else if ( 4 = = Adc [ channel ] . param [ 0 ] ) {
2024-08-03 16:52:47 +01:00
a = 1012.7 ;
b = - 2.786 ;
ratioMQCleanAir = 4.4 ;
}
2024-08-09 15:11:30 +01:00
else if ( 7 = = Adc [ channel ] . param [ 0 ] ) {
2024-08-03 16:52:47 +01:00
a = 99.042 ;
b = - 1.518 ;
ratioMQCleanAir = 27.5 ;
}
2024-08-09 15:11:30 +01:00
if ( 131 = = Adc [ channel ] . param [ 0 ] ) {
2024-08-03 16:52:47 +01:00
a = 23.943 ;
b = - 1.11 ;
ratioMQCleanAir = 15 ;
2022-01-23 16:10:35 +00:00
}
}
2024-08-09 15:11:30 +01:00
Adc [ channel ] . param [ 1 ] = ( int ) ( a * ANALOG_MQ_DECIMAL_MULTIPLIER ) ; // Exponential regression
Adc [ channel ] . param [ 2 ] = ( int ) ( b * ANALOG_MQ_DECIMAL_MULTIPLIER ) ; // Exponential regression
Adc [ channel ] . param [ 3 ] = ( int ) ( ratioMQCleanAir * ANALOG_MQ_DECIMAL_MULTIPLIER ) ; // Exponential regression
2024-08-03 16:52:47 +01:00
2024-08-09 15:11:30 +01:00
// AddLog(LOG_LEVEL_INFO, PSTR("ADC: MQ reset mq%d, a = %2_f, b = %2_f, ratioMQCleanAir = %2_f"), Adc[channel].param[0], &a, &b, &ratioMQCleanAir);
2020-08-03 17:21:34 +01:00
}
2024-08-03 16:52:47 +01:00
} else { // Set default values based on current adc type
// AdcParam 1
AdcInitParams ( channel ) ;
2020-08-03 17:21:34 +01:00
}
2024-08-03 16:52:47 +01:00
AdcSaveSettings ( channel ) ;
2020-08-03 17:21:34 +01:00
}
2024-08-03 16:52:47 +01:00
// AdcParam / AdcGpio
AdcGetSettings ( channel ) ;
Response_P ( PSTR ( " { \" %s " ) , XdrvMailbox . command ) ; // {"AdcParam or {"AdcGpio
if ( strstr_P ( XdrvMailbox . command , PSTR ( D_CMND_ADCGPIO ) ) ) {
2024-08-09 15:11:30 +01:00
# ifdef ESP8266
ResponseAppend_P ( PSTR ( " \" :[ " ) ) ;
# else
2024-08-03 16:52:47 +01:00
ResponseAppend_P ( PSTR ( " %d \" :[ " ) , Adc [ channel ] . pin ) ;
2024-08-09 15:11:30 +01:00
# endif
2024-08-03 16:52:47 +01:00
} else {
ResponseAppend_P ( PSTR ( " %d \" :[%d, " ) , channel + 1 , Adc [ channel ] . pin ) ;
}
2024-08-09 15:11:30 +01:00
ResponseAppend_P ( PSTR ( " %d,%d " ) , Adc [ channel ] . param [ 0 ] , Adc [ channel ] . param [ 1 ] ) ;
2024-08-03 16:52:47 +01:00
if ( ( GPIO_ADC_INPUT = = adc_type ) | |
( GPIO_ADC_TEMP = = adc_type ) | |
( GPIO_ADC_RANGE = = adc_type ) | |
( GPIO_ADC_MQ = = adc_type ) ) {
2024-08-09 15:11:30 +01:00
ResponseAppend_P ( PSTR ( " ,%d,%d " ) , Adc [ channel ] . param [ 2 ] , Adc [ channel ] . param [ 3 ] ) ; // param3 = int, param4 = int
2024-07-28 13:15:35 +01:00
}
else {
2024-08-09 15:11:30 +01:00
float param = ( float ) Adc [ channel ] . param [ 2 ] / 10000 ;
ResponseAppend_P ( PSTR ( " ,%*_f " ) , Decimals ( Adc [ channel ] . param [ 2 ] ) , & param ) ; // param3 = float
2024-08-03 16:52:47 +01:00
if ( ( GPIO_ADC_CT_POWER = = adc_type ) | |
( GPIO_ADC_VOLTAGE = = adc_type ) | |
( GPIO_ADC_CURRENT = = adc_type ) ) {
2024-08-09 15:11:30 +01:00
param = ( float ) Adc [ channel ] . param [ 3 ] / 10000 ;
ResponseAppend_P ( PSTR ( " ,%*_f " ) , Decimals ( Adc [ channel ] . param [ 3 ] ) , & param ) ; // param4 = float
2024-08-03 16:52:47 +01:00
} else {
2024-08-09 15:11:30 +01:00
ResponseAppend_P ( PSTR ( " ,%d " ) , Adc [ channel ] . param [ 3 ] ) ; // param4 = int
2024-07-28 13:15:35 +01:00
}
2020-08-03 17:21:34 +01:00
}
2020-08-04 15:33:05 +01:00
ResponseAppend_P ( PSTR ( " ]} " ) ) ;
2020-08-03 17:21:34 +01:00
}
}
/*********************************************************************************************\
2024-07-27 16:11:25 +01:00
* Energy Interface
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2024-08-15 11:52:12 +01:00
# if defined(ESP32) && defined(USE_ENERGY_SENSOR)
2024-08-06 17:07:26 +01:00
void AdcEnergyEverySecond ( void ) {
uint32_t voltage_count = 0 ;
uint32_t current_count = 0 ;
for ( uint32_t channel = 0 ; channel < Adcs . present ; channel + + ) {
uint32_t type_index = Adc [ channel ] . index ;
uint32_t adc_type = Adc [ channel ] . type ;
if ( GPIO_ADC_VOLTAGE = = adc_type ) {
Energy - > voltage_available = true ;
2024-08-10 16:01:14 +01:00
float value = AdcGetRange ( channel ) / 10000 ; // Volt
Energy - > voltage [ type_index ] = ( value < 0.0f ) ? 0.0f : value ; // Disregard negative values
2024-08-06 17:07:26 +01:00
voltage_count + + ;
}
else if ( GPIO_ADC_CURRENT = = adc_type ) {
Energy - > current_available = true ;
2024-08-10 16:01:14 +01:00
float value = AdcGetRange ( channel ) / 10000 ; // Ampere
Energy - > current [ type_index ] = ( value < 0.0f ) ? 0.0f : value ; // Disregard negative values
2024-08-06 17:07:26 +01:00
current_count + + ;
}
}
2024-08-10 16:01:14 +01:00
for ( uint32_t phase = 0 ; phase < Energy - > phase_count ; phase + + ) {
2024-08-06 17:07:26 +01:00
uint32_t voltage_phase = ( voltage_count = = current_count ) ? phase : 0 ;
Energy - > active_power [ phase ] = Energy - > voltage [ voltage_phase ] * Energy - > current [ phase ] ; // Watt
Energy - > kWhtoday_delta [ phase ] + = ( uint32_t ) ( Energy - > active_power [ phase ] * 1000 ) / 36 ; // deca_microWh
Energy - > data_valid [ phase ] = 0 ;
}
2024-08-10 16:01:14 +01:00
// float delta = (float)Energy->kWhtoday_delta[0] / 100;
// AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("ADC: %3_fV, %3_fA, %3_fW, %2_fmWh"), &Energy->voltage[0], &Energy->current[0], &Energy->active_power[0], &delta);
2024-08-06 17:07:26 +01:00
EnergyUpdateToday ( ) ;
}
2024-07-27 16:11:25 +01:00
bool Xnrg33 ( uint32_t function ) {
bool result = false ;
2024-08-06 17:07:26 +01:00
switch ( function ) {
case FUNC_ENERGY_EVERY_SECOND :
AdcEnergyEverySecond ( ) ;
break ;
case FUNC_PRE_INIT : {
uint32_t voltage_count = 0 ;
uint32_t current_count = 0 ;
for ( uint32_t channel = 0 ; channel < Adcs . present ; channel + + ) {
uint32_t adc_type = Adc [ channel ] . type ;
if ( GPIO_ADC_VOLTAGE = = adc_type ) { voltage_count + + ; }
if ( GPIO_ADC_CURRENT = = adc_type ) { current_count + + ; }
}
2024-08-13 14:11:51 +01:00
if ( voltage_count & & current_count ) {
2024-08-06 17:07:26 +01:00
Energy - > type_dc = true ;
Energy - > voltage_common = ( 1 = = voltage_count ) ;
Energy - > phase_count = ( voltage_count > current_count ) ? voltage_count : current_count ;
Energy - > voltage_available = false ;
Energy - > current_available = false ;
Energy - > use_overtemp = true ; // Use global temperature for overtemp detection
TasmotaGlobal . energy_driver = XNRG_33 ;
}
}
break ;
2024-07-27 16:11:25 +01:00
}
return result ;
}
2024-08-15 11:52:12 +01:00
# endif // ESP32 and USE_ENERGY_SENSOR
2024-07-27 16:11:25 +01:00
/*********************************************************************************************\
* Sensor Interface
2020-08-03 17:21:34 +01:00
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
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 ;
2023-03-11 14:52:02 +00:00
case FUNC_SETUP_RING2 :
2020-08-03 17:21:34 +01:00
AdcInit ( ) ;
break ;
default :
2020-08-04 15:33:05 +01:00
if ( Adcs . present ) {
2020-08-03 17:21:34 +01:00
switch ( function ) {
case FUNC_EVERY_250_MSECOND :
AdcEvery250ms ( ) ;
break ;
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
2024-05-04 14:34:11 +01:00
# endif // FIRMWARE_MINIMAL