xsns_01_counter.ino 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. xsns_01_counter.ino - Counter sensors (water meters, electricity meters etc.) sensor support for Sonoff-Tasmota
  3. Copyright (C) 2018 Maarten Damen and Theo Arends
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*********************************************************************************************\
  16. * Counter sensors (water meters, electricity meters etc.)
  17. \*********************************************************************************************/
  18. #define XSNS_01 1
  19. unsigned long last_counter_timer[MAX_COUNTERS]; // Last counter time in micro seconds
  20. void CounterUpdate(byte index)
  21. {
  22. unsigned long counter_debounce_time = micros() - last_counter_timer[index -1];
  23. if (counter_debounce_time > Settings.pulse_counter_debounce * 1000) {
  24. last_counter_timer[index -1] = micros();
  25. if (bitRead(Settings.pulse_counter_type, index -1)) {
  26. RtcSettings.pulse_counter[index -1] = counter_debounce_time;
  27. } else {
  28. RtcSettings.pulse_counter[index -1]++;
  29. }
  30. // snprintf_P(log_data, sizeof(log_data), PSTR("CNTR: Interrupt %d"), index);
  31. // AddLog(LOG_LEVEL_DEBUG);
  32. }
  33. }
  34. void CounterUpdate1(void)
  35. {
  36. CounterUpdate(1);
  37. }
  38. void CounterUpdate2(void)
  39. {
  40. CounterUpdate(2);
  41. }
  42. void CounterUpdate3(void)
  43. {
  44. CounterUpdate(3);
  45. }
  46. void CounterUpdate4(void)
  47. {
  48. CounterUpdate(4);
  49. }
  50. /********************************************************************************************/
  51. void CounterSaveState(void)
  52. {
  53. for (byte i = 0; i < MAX_COUNTERS; i++) {
  54. if (pin[GPIO_CNTR1 +i] < 99) {
  55. Settings.pulse_counter[i] = RtcSettings.pulse_counter[i];
  56. }
  57. }
  58. }
  59. void CounterInit(void)
  60. {
  61. typedef void (*function) () ;
  62. function counter_callbacks[] = { CounterUpdate1, CounterUpdate2, CounterUpdate3, CounterUpdate4 };
  63. for (byte i = 0; i < MAX_COUNTERS; i++) {
  64. if (pin[GPIO_CNTR1 +i] < 99) {
  65. pinMode(pin[GPIO_CNTR1 +i], bitRead(counter_no_pullup, i) ? INPUT : INPUT_PULLUP);
  66. attachInterrupt(pin[GPIO_CNTR1 +i], counter_callbacks[i], FALLING);
  67. }
  68. }
  69. }
  70. #ifdef USE_WEBSERVER
  71. const char HTTP_SNS_COUNTER[] PROGMEM =
  72. "%s{s}" D_COUNTER "%d{m}%s%s{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
  73. #endif // USE_WEBSERVER
  74. void CounterShow(boolean json)
  75. {
  76. char stemp[10];
  77. byte dsxflg = 0;
  78. byte header = 0;
  79. for (byte i = 0; i < MAX_COUNTERS; i++) {
  80. if (pin[GPIO_CNTR1 +i] < 99) {
  81. char counter[33];
  82. if (bitRead(Settings.pulse_counter_type, i)) {
  83. dtostrfd((double)RtcSettings.pulse_counter[i] / 1000000, 6, counter);
  84. } else {
  85. dsxflg++;
  86. dtostrfd(RtcSettings.pulse_counter[i], 0, counter);
  87. }
  88. if (json) {
  89. if (!header) {
  90. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"COUNTER\":{"), mqtt_data);
  91. stemp[0] = '\0';
  92. }
  93. header++;
  94. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s\"C%d\":%s"), mqtt_data, stemp, i +1, counter);
  95. strlcpy(stemp, ",", sizeof(stemp));
  96. #ifdef USE_DOMOTICZ
  97. if ((0 == tele_period) && (1 == dsxflg)) {
  98. DomoticzSensor(DZ_COUNT, RtcSettings.pulse_counter[i]);
  99. dsxflg++;
  100. }
  101. #endif // USE_DOMOTICZ
  102. #ifdef USE_WEBSERVER
  103. } else {
  104. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_COUNTER, mqtt_data, i +1, counter, (bitRead(Settings.pulse_counter_type, i)) ? " " D_UNIT_SECOND : "");
  105. #endif // USE_WEBSERVER
  106. }
  107. }
  108. if (bitRead(Settings.pulse_counter_type, i)) {
  109. RtcSettings.pulse_counter[i] = 0xFFFFFFFF; // Set Timer to max in case of no more interrupts due to stall of measured device
  110. }
  111. }
  112. if (json) {
  113. if (header) {
  114. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
  115. }
  116. }
  117. }
  118. /*********************************************************************************************\
  119. * Interface
  120. \*********************************************************************************************/
  121. boolean Xsns01(byte function)
  122. {
  123. boolean result = false;
  124. switch (function) {
  125. case FUNC_INIT:
  126. CounterInit();
  127. break;
  128. case FUNC_JSON_APPEND:
  129. CounterShow(1);
  130. break;
  131. #ifdef USE_WEBSERVER
  132. case FUNC_WEB_APPEND:
  133. CounterShow(0);
  134. break;
  135. #endif // USE_WEBSERVER
  136. case FUNC_SAVE_BEFORE_RESTART:
  137. CounterSaveState();
  138. break;
  139. }
  140. return result;
  141. }