Monitor for central heating system (e.g. 2zones+hw) Supports up to 15 temp probes (DS18B20/DS18S20) 3 valve monitors Gas pulse meter recording Use stand-alone or with nodeEnergyServer See http://robdobson.com/2015/09/central-heating-monitor
Dependencies: EthernetInterfacePlusHostname NTPClient Onewire RdWebServer SDFileSystem-RTOS mbed-rtos mbed-src
Diff: GasUseCounter.cpp
- Revision:
- 19:0367cb46d003
- Parent:
- 5:5bccf48799d4
- Child:
- 20:7933076df5af
--- a/GasUseCounter.cpp Mon Sep 28 11:16:46 2015 +0000 +++ b/GasUseCounter.cpp Mon Oct 05 14:05:33 2015 +0000 @@ -1,3 +1,6 @@ +// Gas usage based on pulses from a gas meter +// Rob Dobson 2015 + #include "GasUseCounter.h" // Callback from web server to handle getting current gas count @@ -36,36 +39,59 @@ // Get the current usage count from SD card void GasUseCounter::GetGasCountFromSD() { - FILE* fp = fopen(_gasUseFilename, "r"); - if (fp == NULL) - { - _pc.printf ("Read Gas ... Filename %s not found\r\n", _gasUseFilename); - } - else + int count0 = 0; + int count1 = 0; + for (int i = 0; i < 2; i++) { - int curCount = 0; - int retVal = fscanf(fp, "%d", &curCount); - fclose(fp); - if (retVal == 1) - _pc.printf ("Read from SD last gas count = %d\r\n", curCount); + const char* fname = _gasUseFilename1; + if (i == 1) + fname = _gasUseFilename2; + FILE* fp = fopen(fname, "r"); + if (fp == NULL) + { + _pc.printf ("Read Gas ... Filename %s not found\r\n", fname); + } else - _pc.printf ("Failed to read gas count from SD card\r\n"); - _lastWrittenGasCount = curCount; - _pulseDetector->SetPulseCount(curCount); + { + int curCount = 0; + int retVal = fscanf(fp, "%d", &curCount); + fclose(fp); + if (retVal == 1) + _pc.printf ("Read from file %s last gas count = %d\r\n", fname, curCount); + else + _pc.printf ("Failed to read gas count from file %s\r\n", fname); + if (i == 0) + count0 = curCount; + else + count1 = curCount; + } } + // Find the highest count + int maxCount = count0; + if (count1 > count0) + maxCount = count1; + _lastWrittenGasCount = maxCount; + _pulseDetector->SetPulseCount(maxCount); + _pc.printf ("Pulse count set to %d\r\n", maxCount); } void GasUseCounter::WriteGasCountToSD() { - FILE* fp = fopen(_gasUseFilename, "w"); - if (fp == NULL) + for (int i = 0; i < 2; i++) { - _pc.printf ("WriteGas ... Filename %s not found\r\n", _gasUseFilename); - } - else - { - fprintf(fp, "%d", GetCount()); - _pc.printf ("Written to SD last gas count = %d\r\n", GetCount()); - fclose(fp); + const char* fname = _gasUseFilename1; + if (i == 1) + fname = _gasUseFilename2; + FILE* fp = fopen(fname, "w"); + if (fp == NULL) + { + _pc.printf ("WriteGas ... Filename %s not found\r\n", fname); + } + else + { + fprintf(fp, "%d", GetCount()); + _pc.printf ("Written to %s last gas count = %d\r\n", fname, GetCount()); + fclose(fp); + } } }