Document Analog support

Document Analog support
This commit is contained in:
Theo Arends 2019-04-12 11:01:37 +02:00
parent f583652a7a
commit 5e5580c785
1 changed files with 20 additions and 12 deletions

View File

@ -27,22 +27,30 @@
#define TO_CELSIUS(x) ((x) - 273.15) #define TO_CELSIUS(x) ((x) - 273.15)
#define TO_KELVIN(x) ((x) + 273.15) #define TO_KELVIN(x) ((x) + 273.15)
#define ANALOG_V33 3.3 // Parameters for Steinhart-Hart equation
#define ANALOG_R21 32000.0 #define ANALOG_V33 3.3 // ESP8266 Analog voltage
#define ANALOG_R0 10000.0 #define ANALOG_T0 TO_KELVIN(25.0) // 25 degrees Celcius in Kelvin (= 298.15)
#define ANALOG_T0 TO_KELVIN(25.0) // Shelly 2.5 thermistor
#define ANALOG_B 3350.0 #define ANALOG_R21 32000.0 // Voltage bridge resistor
#define ANALOG_R0 10000.0 // Thermistor resistance
#define ANALOG_B 3350.0 // Thermistor Beta Coefficient
uint16_t adc_last_value = 0; uint16_t adc_last_value = 0;
uint16_t AdcRead(void) uint16_t AdcRead(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 = 0;
for (uint8_t i = 0; i < 32; i++) { for (uint8_t i = 0; i < samples; i++) {
analog += analogRead(A0); analog += analogRead(A0);
delay(1); delay(1);
} }
analog >>= 5; analog >>= factor;
return analog; return analog;
} }
@ -50,7 +58,7 @@ uint16_t AdcRead(void)
void AdcEvery250ms(void) void AdcEvery250ms(void)
{ {
if (my_module_flag.adc0) { if (my_module_flag.adc0) {
uint16_t new_value = AdcRead(); uint16_t new_value = AdcRead(5);
if ((new_value < adc_last_value -10) || (new_value > adc_last_value +10)) { if ((new_value < adc_last_value -10) || (new_value > adc_last_value +10)) {
adc_last_value = new_value; adc_last_value = new_value;
uint16_t value = adc_last_value / 10; uint16_t value = adc_last_value / 10;
@ -64,7 +72,7 @@ void AdcEvery250ms(void)
void AdcShow(bool json) void AdcShow(bool json)
{ {
if (my_module_flag.adc0) { if (my_module_flag.adc0) {
uint16_t analog = AdcRead(); uint16_t analog = AdcRead(5);
if (json) { if (json) {
ResponseAppend_P(PSTR(",\"ANALOG\":{\"A0\":%d}"), analog); ResponseAppend_P(PSTR(",\"ANALOG\":{\"A0\":%d}"), analog);
@ -75,8 +83,8 @@ void AdcShow(bool json)
} }
} }
if (my_module_flag.adc0_temp) { if (my_module_flag.adc0_temp) {
int adc = analogRead(A0); int adc = AdcRead(2);
// Formule used by Shelly 2.5 analog temperature sensor // Steinhart-Hart equation for thermistor as temperature sensor
double Rt = (adc * ANALOG_R21) / (1024.0 * ANALOG_V33 - (double)adc); double Rt = (adc * ANALOG_R21) / (1024.0 * ANALOG_V33 - (double)adc);
double T = ANALOG_B / (ANALOG_B/ANALOG_T0 + log(Rt/ANALOG_R0)); double T = ANALOG_B / (ANALOG_B/ANALOG_T0 + log(Rt/ANALOG_R0));
double temp = ConvertTemp(TO_CELSIUS(T)); double temp = ConvertTemp(TO_CELSIUS(T));