2017-05-20 13:03:34 +01:00
|
|
|
/*
|
2017-09-02 13:37:02 +01:00
|
|
|
xsns_counter.ino - Counter sensors (water meters, electricity meters etc.) sensor support for Sonoff-Tasmota
|
2017-05-20 13:03:34 +01:00
|
|
|
|
|
|
|
Copyright (C) 2017 Maarten Damen and Theo Arends
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Counter sensors (water meters, electricity meters etc.)
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
unsigned long pTimeLast[MAX_COUNTERS]; // Last counter time in milli seconds
|
|
|
|
|
|
|
|
void counter_update(byte index)
|
|
|
|
{
|
|
|
|
unsigned long pTime = millis() - pTimeLast[index -1];
|
|
|
|
if (pTime > sysCfg.pCounterDebounce) {
|
|
|
|
pTimeLast[index -1] = millis();
|
|
|
|
if (bitRead(sysCfg.pCounterType, index -1)) {
|
|
|
|
rtcMem.pCounter[index -1] = pTime;
|
|
|
|
} else {
|
|
|
|
rtcMem.pCounter[index -1]++;
|
|
|
|
}
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
// snprintf_P(log_data, sizeof(log_data), PSTR("CNTR: Interrupt %d"), index);
|
|
|
|
// addLog(LOG_LEVEL_DEBUG);
|
2017-05-20 13:03:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void counter_update1()
|
|
|
|
{
|
|
|
|
counter_update(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void counter_update2()
|
|
|
|
{
|
|
|
|
counter_update(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void counter_update3()
|
|
|
|
{
|
|
|
|
counter_update(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void counter_update4()
|
|
|
|
{
|
|
|
|
counter_update(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void counter_savestate()
|
|
|
|
{
|
|
|
|
for (byte i = 0; i < MAX_COUNTERS; i++) {
|
|
|
|
if (pin[GPIO_CNTR1 +i] < 99) {
|
|
|
|
sysCfg.pCounter[i] = rtcMem.pCounter[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void counter_init()
|
|
|
|
{
|
|
|
|
typedef void (*function) () ;
|
|
|
|
function counter_callbacks[] = { counter_update1, counter_update2, counter_update3, counter_update4 };
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-05-20 13:03:34 +01:00
|
|
|
for (byte i = 0; i < MAX_COUNTERS; i++) {
|
|
|
|
if (pin[GPIO_CNTR1 +i] < 99) {
|
|
|
|
pinMode(pin[GPIO_CNTR1 +i], INPUT_PULLUP);
|
|
|
|
attachInterrupt(pin[GPIO_CNTR1 +i], counter_callbacks[i], FALLING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Presentation
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
void counter_mqttPresent(uint8_t* djson)
|
2017-05-20 13:03:34 +01:00
|
|
|
{
|
|
|
|
char stemp[16];
|
|
|
|
|
|
|
|
byte dsxflg = 0;
|
|
|
|
for (byte i = 0; i < MAX_COUNTERS; i++) {
|
|
|
|
if (pin[GPIO_CNTR1 +i] < 99) {
|
|
|
|
if (bitRead(sysCfg.pCounterType, i)) {
|
2017-09-02 13:37:02 +01:00
|
|
|
dtostrfd((double)rtcMem.pCounter[i] / 1000, 3, stemp);
|
2017-05-20 13:03:34 +01:00
|
|
|
} else {
|
|
|
|
dsxflg++;
|
2017-09-02 13:37:02 +01:00
|
|
|
dtostrfd(rtcMem.pCounter[i], 0, stemp);
|
2017-05-20 13:03:34 +01:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"" D_COUNTER "%d\":%s"), mqtt_data, i +1, stemp);
|
2017-05-20 13:03:34 +01:00
|
|
|
*djson = 1;
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
if (1 == dsxflg) {
|
|
|
|
domoticz_sensor6(rtcMem.pCounter[i]);
|
|
|
|
dsxflg++;
|
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
const char HTTP_SNS_COUNTER[] PROGMEM =
|
2017-09-02 13:37:02 +01:00
|
|
|
"<tr><th>" D_COUNTER "%d</th><td>%s%s</td></tr>";
|
2017-05-20 13:03:34 +01:00
|
|
|
|
|
|
|
String counter_webPresent()
|
|
|
|
{
|
|
|
|
String page = "";
|
|
|
|
char stemp[16];
|
|
|
|
char sensor[80];
|
|
|
|
|
|
|
|
for (byte i = 0; i < MAX_COUNTERS; i++) {
|
|
|
|
if (pin[GPIO_CNTR1 +i] < 99) {
|
|
|
|
if (bitRead(sysCfg.pCounterType, i)) {
|
2017-09-02 13:37:02 +01:00
|
|
|
dtostrfi((double)rtcMem.pCounter[i] / 1000, 3, stemp);
|
2017-05-20 13:03:34 +01:00
|
|
|
} else {
|
2017-09-02 13:37:02 +01:00
|
|
|
dtostrfi(rtcMem.pCounter[i], 0, stemp);
|
2017-05-20 13:03:34 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_COUNTER, i+1, stemp, (bitRead(sysCfg.pCounterType, i)) ? " " D_UNIT_SECOND : "");
|
2017-05-20 13:03:34 +01:00
|
|
|
page += sensor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
|