123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- void increaseCounter(byte _cNum) {
- unsigned long _deltaTime;
- unsigned long _currMillis;
- _currMillis = millis();
- _deltaTime = _currMillis - pulseInLastMillis[_cNum];
- pulseInLastMillis[_cNum] = _currMillis;
- currentReadingImpulses[_cNum]++;
- if (currentReadingImpulses[_cNum] >= meter_impPerUnit[_cNum]) {
- currentReading[_cNum]++;
- currentReadingImpulses[_cNum] = 0;
- storeCurrentReading(_cNum, false);
- }
- printCurrentReading(_cNum, _deltaTime, true);
- }
- void printCurrentReadings() {
- for (byte i = 0; i < COUNTERS_COUNT; i++) {
- printCurrentReading(i, 0, false);
- }
- }
- void printCurrentReading(byte _cNum, unsigned long _deltaTime, bool _sendDeltaTime) {
- if (output_json) {
- Serial.print(F("{\"C\":"));
- Serial.print(_cNum + 1);
- Serial.print(F(", \"reading\":"));
- Serial.print(currentReading[_cNum]);
- Serial.print(".");
- if (meter_impPerUnit[_cNum] == 100 && currentReadingImpulses[_cNum] < 10) Serial.print("0");
- else if (meter_impPerUnit[_cNum] == 1000 && currentReadingImpulses[_cNum] < 10) Serial.print("00");
- else if (meter_impPerUnit[_cNum] == 1000 && currentReadingImpulses[_cNum] < 100) Serial.print("0");
- Serial.print(currentReadingImpulses[_cNum]);
- Serial.print(F(", \"impPerU\":"));
- Serial.print(meter_impPerUnit[_cNum]);
-
- if (_sendDeltaTime) {
- Serial.print(F(", \"dTime\":"));
- Serial.print(_deltaTime);
- }
- Serial.println("}");
- }
- else {
- Serial.print("C");
- Serial.print(_cNum + 1);
- Serial.print("=");
- Serial.print(currentReading[_cNum]);
- Serial.print(".");
- if (meter_impPerUnit[_cNum] == 100 && currentReadingImpulses[_cNum] < 10) Serial.print("0");
- else if (meter_impPerUnit[_cNum] == 1000 && currentReadingImpulses[_cNum] < 10) Serial.print("00");
- else if (meter_impPerUnit[_cNum] == 1000 && currentReadingImpulses[_cNum] < 100) Serial.print("0");
- Serial.print(currentReadingImpulses[_cNum]);
- Serial.print(";");
- Serial.print(meter_impPerUnit[_cNum]);
- if (_sendDeltaTime) {
- Serial.print(";");
- Serial.print(_deltaTime);
- }
- Serial.println();
- }
- lastDataSent_seconds[_cNum] = seconds;
- }
- void onNoImpulseTimeout() {
- for (byte i = 0; i < COUNTERS_COUNT; i++) {
- if (meter_noImpulseTimeout_seconds[i] > 0) {
- bool _sendDataNow = false;
-
- // store counter to EEPROM if value changed
- if ( (currentReadingImpulses_saved[i] != currentReadingImpulses[i]) && ((millis() - pulseInLastMillis[i]) >= (meter_noImpulseTimeout_seconds[i] * 1000)) ) {
- currentReadingImpulses_saved[i] = currentReadingImpulses[i];
- storeCurrentImpulses(i, false);
- Serial.print(F("INFO: saved impCount C"));
- Serial.print(i + 1);
- Serial.print(F(" (noImpTout)"));
- Serial.println();
- _sendDataNow = true;
- }
- // send current data with current usage=0 on interval
- if ( _sendDataNow || (seconds - lastDataSent_seconds[i]) >= meter_noImpulseTimeout_seconds[i] ) {
- if(debug) {
- Serial.print(F("send data on noImpTout C"));
- Serial.print(i + 1);
- Serial.print("(");
- Serial.print(meter_noImpulseTimeout_seconds[i]);
- Serial.println("s)");
- }
- printCurrentReading(i, 0, true);
- lastDataSent_seconds[i] = seconds;
- }
- }
- }
- }
- void saveDataOnInterval() {
- for (byte i = 0; i < COUNTERS_COUNT; i++) {
- if (meter_savePulsesOnInterval_mins[i] > 0 && (currentReadingImpulses_saved[i] != currentReadingImpulses[i])) {
- if ((seconds - pulsesLastSaved_seconds[i]) >= (meter_savePulsesOnInterval_mins[i] * 60)) {
- storeCurrentImpulses(i, false);
- pulsesLastSaved_seconds[i] = seconds;
- Serial.print(F("INFO: saved impCount C"));
- Serial.print(i + 1);
- Serial.print(F(" (fixed interval)"));
- Serial.println();
- }
- }
- }
- }
- /*void printImpPerUnit(byte _cNum) {
- Serial.print("C");
- Serial.print(_cNum + 1);
- Serial.print(F(": imp/unit="));
- Serial.println(meter_impPerUnit[_cNum]);
- }*/
|