From 44598e5fc4a2d1a12dc86d721272587c8394c66f Mon Sep 17 00:00:00 2001 From: Theo Arends Date: Sat, 28 Apr 2018 18:18:37 +0200 Subject: [PATCH] Add ANALOG#A0div10 trigger to rules 5.12.0n * Add ANALOG#A0div10 trigger to rules to be used in rule like on analog#a0div10 do publish cmnd/ailight/dimmer %value% endon --- sonoff/_releasenotes.ino | 1 + sonoff/support.ino | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index ec42dee40..d7bed584e 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -2,6 +2,7 @@ * Change ESP8266 Analog JSON message from {"Analog0:123"} to {"ANALOG":{"A0:123"}} to accomodate rules (#2560) * Change Counter JSON message from {"Counter1":0,"Counter3":0} to {"COUNTER":{"C1":0,"C3":0}} to accomodate rules * Change ADS1115 JSON message from {"ADS1115":{"Analog0":123,"Analog1":123}} to {"ADS1115":{"A0":123,"A1":123}} + * Add ANALOG#A0div10 trigger to rules to be used in rule like on analog#a0div10 do publish cmnd/ailight/dimmer %value% endon * * 5.12.0m * Reinit timers to accomodate random window (#2447) diff --git a/sonoff/support.ino b/sonoff/support.ino index 7ff973b2d..da95e4f25 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -1386,7 +1386,10 @@ void RtcInit() * ADC support \*********************************************************************************************/ -void AdcShow(boolean json) +uint8_t adc_counter = 0; +uint16_t adc_last_value = 0; + +uint16_t AdcRead() { uint16_t analog = 0; for (byte i = 0; i < 32; i++) { @@ -1394,6 +1397,26 @@ void AdcShow(boolean json) delay(1); } analog >>= 5; + return analog; +} + +void AdcEvery50ms() +{ + adc_counter++; + if (!(adc_counter % 4)) { + uint16_t new_value = AdcRead(); + if ((new_value < adc_last_value -10) || (new_value > adc_last_value +10)) { + adc_last_value = new_value; + uint16_t value = adc_last_value / 10; + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"ANALOG\":{\"A0div10\":%d}}"), (0 == value) ? 1 : (value > 99) ? 100 : value); + RulesProcess(); + } + } +} + +void AdcShow(boolean json) +{ + uint16_t analog = AdcRead(); if (json) { snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"ANALOG\":{\"A0\":%d}"), mqtt_data, analog); @@ -1416,6 +1439,9 @@ boolean Xsns02(byte function) if (pin[GPIO_ADC0] < 99) { switch (function) { + case FUNC_EVERY_50_MSECOND: + AdcEvery50ms(); + break; case FUNC_JSON_APPEND: AdcShow(1); break;