2017-05-20 13:03:34 +01:00
|
|
|
/*
|
2017-11-07 14:57:24 +00:00
|
|
|
xsns_01_counter.ino - Counter sensors (water meters, electricity meters etc.) sensor support for Sonoff-Tasmota
|
2017-05-20 13:03:34 +01:00
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Maarten Damen and Theo Arends
|
2017-05-20 13:03: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/>.
|
|
|
|
*/
|
|
|
|
|
2019-06-16 15:43:23 +01:00
|
|
|
#ifdef USE_COUNTER
|
2017-05-20 13:03:34 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Counter sensors (water meters, electricity meters etc.)
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_01 1
|
|
|
|
|
2018-08-09 12:24:13 +01:00
|
|
|
unsigned long last_counter_timer[MAX_COUNTERS]; // Last counter time in micro seconds
|
2017-05-20 13:03:34 +01:00
|
|
|
|
2019-05-22 12:51:33 +01:00
|
|
|
#ifndef ARDUINO_ESP8266_RELEASE_2_3_0 // Fix core 2.5.x ISR not in IRAM Exception
|
|
|
|
void CounterUpdate(uint8_t index) ICACHE_RAM_ATTR;
|
|
|
|
void CounterUpdate1(void) ICACHE_RAM_ATTR;
|
|
|
|
void CounterUpdate2(void) ICACHE_RAM_ATTR;
|
|
|
|
void CounterUpdate3(void) ICACHE_RAM_ATTR;
|
|
|
|
void CounterUpdate4(void) ICACHE_RAM_ATTR;
|
|
|
|
#endif // ARDUINO_ESP8266_RELEASE_2_3_0
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void CounterUpdate(uint8_t index)
|
2017-05-20 13:03:34 +01:00
|
|
|
{
|
2018-08-09 12:24:13 +01:00
|
|
|
unsigned long counter_debounce_time = micros() - last_counter_timer[index -1];
|
|
|
|
if (counter_debounce_time > Settings.pulse_counter_debounce * 1000) {
|
|
|
|
last_counter_timer[index -1] = micros();
|
2017-10-18 17:22:34 +01:00
|
|
|
if (bitRead(Settings.pulse_counter_type, index -1)) {
|
|
|
|
RtcSettings.pulse_counter[index -1] = counter_debounce_time;
|
2017-05-20 13:03:34 +01:00
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
RtcSettings.pulse_counter[index -1]++;
|
2017-05-20 13:03:34 +01:00
|
|
|
}
|
|
|
|
|
2019-03-08 14:15:42 +00:00
|
|
|
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("CNTR: Interrupt %d"), index);
|
2017-05-20 13:03:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void CounterUpdate1(void)
|
2017-05-20 13:03:34 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
CounterUpdate(1);
|
2017-05-20 13:03:34 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void CounterUpdate2(void)
|
2017-05-20 13:03:34 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
CounterUpdate(2);
|
2017-05-20 13:03:34 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void CounterUpdate3(void)
|
2017-05-20 13:03:34 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
CounterUpdate(3);
|
2017-05-20 13:03:34 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void CounterUpdate4(void)
|
2017-05-20 13:03:34 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
CounterUpdate(4);
|
2017-05-20 13:03:34 +01:00
|
|
|
}
|
|
|
|
|
2017-12-16 19:11:12 +00:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void CounterSaveState(void)
|
2017-05-20 13:03:34 +01:00
|
|
|
{
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < MAX_COUNTERS; i++) {
|
2018-08-25 12:08:06 +01:00
|
|
|
if (pin[GPIO_CNTR1 +i] < 99) {
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.pulse_counter[i] = RtcSettings.pulse_counter[i];
|
2017-05-20 13:03:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void CounterInit(void)
|
2017-05-20 13:03:34 +01:00
|
|
|
{
|
|
|
|
typedef void (*function) () ;
|
2017-10-18 17:22:34 +01:00
|
|
|
function counter_callbacks[] = { CounterUpdate1, CounterUpdate2, CounterUpdate3, CounterUpdate4 };
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < MAX_COUNTERS; i++) {
|
2017-05-20 13:03:34 +01:00
|
|
|
if (pin[GPIO_CNTR1 +i] < 99) {
|
2018-08-25 12:08:06 +01:00
|
|
|
pinMode(pin[GPIO_CNTR1 +i], bitRead(counter_no_pullup, i) ? INPUT : INPUT_PULLUP);
|
2017-05-20 13:03:34 +01:00
|
|
|
attachInterrupt(pin[GPIO_CNTR1 +i], counter_callbacks[i], FALLING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
const char HTTP_SNS_COUNTER[] PROGMEM =
|
2019-03-19 16:31:43 +00:00
|
|
|
"{s}" D_COUNTER "%d{m}%s%s{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
|
2017-11-04 15:36:51 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2017-05-20 13:03:34 +01:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void CounterShow(bool json)
|
2017-05-20 13:03:34 +01:00
|
|
|
{
|
2018-04-28 14:55:38 +01:00
|
|
|
char stemp[10];
|
2017-05-20 13:03:34 +01:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t dsxflg = 0;
|
|
|
|
uint8_t header = 0;
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < MAX_COUNTERS; i++) {
|
2018-08-25 12:08:06 +01:00
|
|
|
if (pin[GPIO_CNTR1 +i] < 99) {
|
2018-12-21 15:17:06 +00:00
|
|
|
char counter[33];
|
2017-10-18 17:22:34 +01:00
|
|
|
if (bitRead(Settings.pulse_counter_type, i)) {
|
2018-08-09 12:24:13 +01:00
|
|
|
dtostrfd((double)RtcSettings.pulse_counter[i] / 1000000, 6, counter);
|
2017-05-20 13:03:34 +01:00
|
|
|
} else {
|
|
|
|
dsxflg++;
|
2019-02-18 10:06:16 +00:00
|
|
|
snprintf_P(counter, sizeof(counter), PSTR("%lu"), RtcSettings.pulse_counter[i]);
|
2017-05-20 13:03:34 +01:00
|
|
|
}
|
2017-11-04 15:36:51 +00:00
|
|
|
|
|
|
|
if (json) {
|
2018-04-28 14:55:38 +01:00
|
|
|
if (!header) {
|
2019-03-23 16:57:31 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"COUNTER\":{"));
|
2018-04-28 14:55:38 +01:00
|
|
|
stemp[0] = '\0';
|
|
|
|
}
|
|
|
|
header++;
|
2019-03-23 16:57:31 +00:00
|
|
|
ResponseAppend_P(PSTR("%s\"C%d\":%s"), stemp, i +1, counter);
|
2018-09-02 10:52:24 +01:00
|
|
|
strlcpy(stemp, ",", sizeof(stemp));
|
2017-05-20 13:03:34 +01:00
|
|
|
#ifdef USE_DOMOTICZ
|
2018-04-11 09:11:20 +01:00
|
|
|
if ((0 == tele_period) && (1 == dsxflg)) {
|
2017-11-04 15:36:51 +00:00
|
|
|
DomoticzSensor(DZ_COUNT, RtcSettings.pulse_counter[i]);
|
|
|
|
dsxflg++;
|
|
|
|
}
|
2017-05-20 13:03:34 +01:00
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2019-03-19 16:31:43 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_COUNTER, i +1, counter, (bitRead(Settings.pulse_counter_type, i)) ? " " D_UNIT_SECOND : "");
|
2017-11-04 15:36:51 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2017-05-20 13:03:34 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-23 15:05:51 +01:00
|
|
|
if (bitRead(Settings.pulse_counter_type, i)) {
|
|
|
|
RtcSettings.pulse_counter[i] = 0xFFFFFFFF; // Set Timer to max in case of no more interrupts due to stall of measured device
|
|
|
|
}
|
2017-05-20 13:03:34 +01:00
|
|
|
}
|
2018-04-28 14:55:38 +01:00
|
|
|
if (json) {
|
|
|
|
if (header) {
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
ResponseJsonEnd();
|
2018-04-28 14:55:38 +01:00
|
|
|
}
|
|
|
|
}
|
2017-05-20 13:03:34 +01:00
|
|
|
}
|
|
|
|
|
2019-07-26 09:21:36 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Commands
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
enum CounterCommands { CMND_COUNTER, CMND_COUNTERTYPE, CMND_COUNTERDEBOUNCE };
|
|
|
|
const char kCounterCommands[] PROGMEM = D_CMND_COUNTER "|" D_CMND_COUNTERTYPE "|" D_CMND_COUNTERDEBOUNCE ;
|
|
|
|
|
|
|
|
bool CounterCommand(void)
|
|
|
|
{
|
|
|
|
char command[CMDSZ];
|
|
|
|
bool serviced = true;
|
|
|
|
|
|
|
|
int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, kCounterCommands);
|
|
|
|
if (CMND_COUNTER == command_code) {
|
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_COUNTERS)) {
|
|
|
|
if ((XdrvMailbox.data_len > 0) && (pin[GPIO_CNTR1 + XdrvMailbox.index -1] < 99)) {
|
|
|
|
if ((XdrvMailbox.data[0] == '-') || (XdrvMailbox.data[0] == '+')) {
|
2019-07-27 10:13:41 +01:00
|
|
|
RtcSettings.pulse_counter[XdrvMailbox.index -1] += XdrvMailbox.payload;
|
|
|
|
Settings.pulse_counter[XdrvMailbox.index -1] += XdrvMailbox.payload;
|
2019-07-26 09:21:36 +01:00
|
|
|
} else {
|
2019-07-27 10:13:41 +01:00
|
|
|
RtcSettings.pulse_counter[XdrvMailbox.index -1] = XdrvMailbox.payload;
|
|
|
|
Settings.pulse_counter[XdrvMailbox.index -1] = XdrvMailbox.payload;
|
2019-07-26 09:21:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Response_P(S_JSON_COMMAND_INDEX_LVALUE, command, XdrvMailbox.index, RtcSettings.pulse_counter[XdrvMailbox.index -1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (CMND_COUNTERTYPE == command_code) {
|
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_COUNTERS)) {
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 1) && (pin[GPIO_CNTR1 + XdrvMailbox.index -1] < 99)) {
|
|
|
|
bitWrite(Settings.pulse_counter_type, XdrvMailbox.index -1, XdrvMailbox.payload &1);
|
|
|
|
RtcSettings.pulse_counter[XdrvMailbox.index -1] = 0;
|
|
|
|
Settings.pulse_counter[XdrvMailbox.index -1] = 0;
|
|
|
|
}
|
|
|
|
Response_P(S_JSON_COMMAND_INDEX_NVALUE, command, XdrvMailbox.index, bitRead(Settings.pulse_counter_type, XdrvMailbox.index -1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (CMND_COUNTERDEBOUNCE == command_code) {
|
2019-07-27 10:13:41 +01:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 32001)) {
|
|
|
|
Settings.pulse_counter_debounce = XdrvMailbox.payload;
|
2019-07-26 09:21:36 +01:00
|
|
|
}
|
|
|
|
Response_P(S_JSON_COMMAND_NVALUE, command, Settings.pulse_counter_debounce);
|
|
|
|
}
|
|
|
|
else serviced = false; // Unknown command
|
|
|
|
|
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:07:25 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns01(uint8_t function)
|
2017-11-03 17:07:25 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2017-11-03 17:07:25 +00:00
|
|
|
|
|
|
|
switch (function) {
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_INIT:
|
2017-11-03 17:07:25 +00:00
|
|
|
CounterInit();
|
|
|
|
break;
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
2017-11-04 15:36:51 +00:00
|
|
|
CounterShow(1);
|
2017-11-03 17:07:25 +00:00
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
2019-03-19 16:31:43 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
2017-11-04 15:36:51 +00:00
|
|
|
CounterShow(0);
|
2017-11-03 17:07:25 +00:00
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_SAVE_BEFORE_RESTART:
|
2019-06-16 15:43:23 +01:00
|
|
|
case FUNC_SAVE_AT_MIDNIGHT:
|
2017-12-16 19:11:12 +00:00
|
|
|
CounterSaveState();
|
|
|
|
break;
|
2019-07-26 09:21:36 +01:00
|
|
|
case FUNC_COMMAND:
|
|
|
|
result = CounterCommand();
|
|
|
|
break;
|
2017-11-03 17:07:25 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-06-16 15:43:23 +01:00
|
|
|
|
|
|
|
#endif // USE_COUNTER
|