counterFunctions.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. void increaseCounter(byte _cNum) {
  2. unsigned long _deltaTime;
  3. unsigned long _currMillis;
  4. _currMillis = millis();
  5. _deltaTime = _currMillis - pulseInLastMillis[_cNum];
  6. pulseInLastMillis[_cNum] = _currMillis;
  7. currentReadingImpulses[_cNum]++;
  8. if (currentReadingImpulses[_cNum] >= meter_impPerUnit[_cNum]) {
  9. currentReading[_cNum]++;
  10. currentReadingImpulses[_cNum] = 0;
  11. storeCurrentReading(_cNum, false);
  12. }
  13. printCurrentReading(_cNum, _deltaTime, true);
  14. }
  15. void printCurrentReadings() {
  16. for (byte i = 0; i < COUNTERS_COUNT; i++) {
  17. printCurrentReading(i, 0, false);
  18. }
  19. }
  20. void printCurrentReading(byte _cNum, unsigned long _deltaTime, bool _sendDeltaTime) {
  21. if (output_json) {
  22. Serial.print(F("{\"C\":"));
  23. Serial.print(_cNum + 1);
  24. Serial.print(F(", \"reading\":"));
  25. Serial.print(currentReading[_cNum]);
  26. Serial.print(".");
  27. if (meter_impPerUnit[_cNum] == 100 && currentReadingImpulses[_cNum] < 10) Serial.print("0");
  28. else if (meter_impPerUnit[_cNum] == 1000 && currentReadingImpulses[_cNum] < 10) Serial.print("00");
  29. else if (meter_impPerUnit[_cNum] == 1000 && currentReadingImpulses[_cNum] < 100) Serial.print("0");
  30. Serial.print(currentReadingImpulses[_cNum]);
  31. Serial.print(F(", \"impPerU\":"));
  32. Serial.print(meter_impPerUnit[_cNum]);
  33. if (_sendDeltaTime) {
  34. Serial.print(F(", \"dTime\":"));
  35. Serial.print(_deltaTime);
  36. }
  37. Serial.println("}");
  38. }
  39. else {
  40. Serial.print("C");
  41. Serial.print(_cNum + 1);
  42. Serial.print("=");
  43. Serial.print(currentReading[_cNum]);
  44. Serial.print(".");
  45. if (meter_impPerUnit[_cNum] == 100 && currentReadingImpulses[_cNum] < 10) Serial.print("0");
  46. else if (meter_impPerUnit[_cNum] == 1000 && currentReadingImpulses[_cNum] < 10) Serial.print("00");
  47. else if (meter_impPerUnit[_cNum] == 1000 && currentReadingImpulses[_cNum] < 100) Serial.print("0");
  48. Serial.print(currentReadingImpulses[_cNum]);
  49. Serial.print(";");
  50. Serial.print(meter_impPerUnit[_cNum]);
  51. if (_sendDeltaTime) {
  52. Serial.print(";");
  53. Serial.print(_deltaTime);
  54. }
  55. Serial.println();
  56. }
  57. lastDataSent_seconds[_cNum] = seconds;
  58. }
  59. void onNoImpulseTimeout() {
  60. for (byte i = 0; i < COUNTERS_COUNT; i++) {
  61. if (meter_noImpulseTimeout_seconds[i] > 0) {
  62. bool _sendDataNow = false;
  63. // store counter to EEPROM if value changed
  64. if ( (currentReadingImpulses_saved[i] != currentReadingImpulses[i]) && ((millis() - pulseInLastMillis[i]) >= (meter_noImpulseTimeout_seconds[i] * 1000)) ) {
  65. currentReadingImpulses_saved[i] = currentReadingImpulses[i];
  66. storeCurrentImpulses(i, false);
  67. Serial.print(F("INFO: saved impCount C"));
  68. Serial.print(i + 1);
  69. Serial.print(F(" (noImpTout)"));
  70. Serial.println();
  71. _sendDataNow = true;
  72. }
  73. // send current data with current usage=0 on interval
  74. if ( _sendDataNow || (seconds - lastDataSent_seconds[i]) >= meter_noImpulseTimeout_seconds[i] ) {
  75. if(debug) {
  76. Serial.print(F("send data on noImpTout C"));
  77. Serial.print(i + 1);
  78. Serial.print("(");
  79. Serial.print(meter_noImpulseTimeout_seconds[i]);
  80. Serial.println("s)");
  81. }
  82. printCurrentReading(i, 0, true);
  83. lastDataSent_seconds[i] = seconds;
  84. }
  85. }
  86. }
  87. }
  88. void saveDataOnInterval() {
  89. for (byte i = 0; i < COUNTERS_COUNT; i++) {
  90. if (meter_savePulsesOnInterval_mins[i] > 0 && (currentReadingImpulses_saved[i] != currentReadingImpulses[i])) {
  91. if ((seconds - pulsesLastSaved_seconds[i]) >= (meter_savePulsesOnInterval_mins[i] * 60)) {
  92. storeCurrentImpulses(i, false);
  93. pulsesLastSaved_seconds[i] = seconds;
  94. Serial.print(F("INFO: saved impCount C"));
  95. Serial.print(i + 1);
  96. Serial.print(F(" (fixed interval)"));
  97. Serial.println();
  98. }
  99. }
  100. }
  101. }
  102. /*void printImpPerUnit(byte _cNum) {
  103. Serial.print("C");
  104. Serial.print(_cNum + 1);
  105. Serial.print(F(": imp/unit="));
  106. Serial.println(meter_impPerUnit[_cNum]);
  107. }*/