Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. COUNTER MODULE
  3. Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if COUNTER_SUPPORT
  6. volatile unsigned long _counterCurrent = 0;
  7. volatile unsigned long _counterLast = 0;
  8. unsigned long _counterBuffer[COUNTER_REPORT_EVERY] = {0};
  9. unsigned char _counterBufferPointer = 0;
  10. unsigned long _counterValue = 0;
  11. // -----------------------------------------------------------------------------
  12. // COUNTER
  13. // -----------------------------------------------------------------------------
  14. void ICACHE_RAM_ATTR _counterISR() {
  15. if (millis() - _counterLast > COUNTER_DEBOUNCE) {
  16. ++_counterCurrent;
  17. _counterLast = millis();
  18. }
  19. }
  20. void _counterWebSocketOnSend(JsonObject& root) {
  21. root["counterVisible"] = 1;
  22. root["counterValue"] = getCounter();
  23. }
  24. // -----------------------------------------------------------------------------
  25. unsigned long getCounter() {
  26. return _counterValue;
  27. }
  28. void counterSetup() {
  29. pinMode(COUNTER_PIN, COUNTER_PIN_MODE);
  30. attachInterrupt(COUNTER_PIN, _counterISR, COUNTER_INTERRUPT_MODE);
  31. #if WEB_SUPPORT
  32. // Websockets
  33. wsOnSendRegister(_counterWebSocketOnSend);
  34. // API
  35. apiRegister(COUNTER_TOPIC, COUNTER_TOPIC, [](char * buffer, size_t len) {
  36. snprintf_P(buffer, len, PSTR("%d"), getCounter());
  37. });
  38. #endif
  39. DEBUG_MSG_P(PSTR("[COUNTER] Counter on GPIO %d\n"), COUNTER_PIN);
  40. }
  41. void counterLoop() {
  42. // Check if we should read new data
  43. static unsigned long last_update = 0;
  44. if ((millis() - last_update) < COUNTER_UPDATE_INTERVAL) return;
  45. last_update = millis();
  46. // Update buffer counts
  47. _counterValue = _counterValue - _counterBuffer[_counterBufferPointer] + _counterCurrent;
  48. _counterBuffer[_counterBufferPointer] = _counterCurrent;
  49. _counterCurrent = 0;
  50. _counterBufferPointer = (_counterBufferPointer + 1) % COUNTER_REPORT_EVERY;
  51. DEBUG_MSG_P(PSTR("[COUNTER] Value: %d\n"), _counterValue);
  52. // Update websocket clients
  53. #if WEB_SUPPORT
  54. wsSend(_counterWebSocketOnSend);
  55. #endif
  56. // Do we have to report?
  57. if (_counterBufferPointer == 0) {
  58. // Send MQTT messages
  59. #if MQTT_SUPPORT
  60. mqttSend(getSetting("counterTopic", COUNTER_TOPIC).c_str(), String(_counterValue).c_str());
  61. #endif
  62. // Send to Domoticz
  63. #if DOMOTICZ_SUPPORT
  64. domoticzSend("dczCountIdx", 0, String(_counterValue).c_str());
  65. #endif
  66. // Send to InfluxDB
  67. #if INFLUXDB_SUPPORT
  68. idbSend(COUNTER_TOPIC, _counterValue);
  69. #endif
  70. }
  71. }
  72. #endif // COUNTER_SUPPORT