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
GasUseCounter.cpp@19:0367cb46d003, 2015-10-05 (annotated)
- Committer:
- Bobty
- Date:
- Mon Oct 05 14:05:33 2015 +0000
- Revision:
- 19:0367cb46d003
- Parent:
- 5:5bccf48799d4
- Child:
- 20:7933076df5af
Added retries on getting addresses from thermometers; Doubled up on non-volatile storage of latest gas count
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bobty | 19:0367cb46d003 | 1 | // Gas usage based on pulses from a gas meter |
Bobty | 19:0367cb46d003 | 2 | // Rob Dobson 2015 |
Bobty | 19:0367cb46d003 | 3 | |
Bobty | 5:5bccf48799d4 | 4 | #include "GasUseCounter.h" |
Bobty | 5:5bccf48799d4 | 5 | |
Bobty | 5:5bccf48799d4 | 6 | // Callback from web server to handle getting current gas count |
Bobty | 5:5bccf48799d4 | 7 | char* GasUseCounter::getGasUseCallback(char* cmdStr, char* argStr) |
Bobty | 5:5bccf48799d4 | 8 | { |
Bobty | 5:5bccf48799d4 | 9 | sprintf(_gasCountStr, "{\"gasUseCount\":\"%d\"}", GetCount()); |
Bobty | 5:5bccf48799d4 | 10 | return _gasCountStr; |
Bobty | 5:5bccf48799d4 | 11 | } |
Bobty | 5:5bccf48799d4 | 12 | |
Bobty | 5:5bccf48799d4 | 13 | // Init |
Bobty | 5:5bccf48799d4 | 14 | void GasUseCounter::Init() |
Bobty | 5:5bccf48799d4 | 15 | { |
Bobty | 5:5bccf48799d4 | 16 | GetGasCountFromSD(); |
Bobty | 5:5bccf48799d4 | 17 | } |
Bobty | 5:5bccf48799d4 | 18 | |
Bobty | 5:5bccf48799d4 | 19 | // Service function |
Bobty | 5:5bccf48799d4 | 20 | bool GasUseCounter::Service() |
Bobty | 5:5bccf48799d4 | 21 | { |
Bobty | 5:5bccf48799d4 | 22 | // Check for an edge |
Bobty | 5:5bccf48799d4 | 23 | bool edgeDetected = _pulseDetector->Service(); |
Bobty | 5:5bccf48799d4 | 24 | if (edgeDetected) |
Bobty | 5:5bccf48799d4 | 25 | { |
Bobty | 5:5bccf48799d4 | 26 | // Check if we need to store in non-volatile storage |
Bobty | 5:5bccf48799d4 | 27 | if (GetCount() >= _lastWrittenGasCount + MAX_PULSES_BEFORE_STORE_NV) |
Bobty | 5:5bccf48799d4 | 28 | { |
Bobty | 5:5bccf48799d4 | 29 | WriteGasCountToSD(); |
Bobty | 5:5bccf48799d4 | 30 | _lastWrittenGasCount = GetCount(); |
Bobty | 5:5bccf48799d4 | 31 | } |
Bobty | 5:5bccf48799d4 | 32 | |
Bobty | 5:5bccf48799d4 | 33 | // Show count |
Bobty | 5:5bccf48799d4 | 34 | _pc.printf("Count %d Rate(ms) %d\r\n", _pulseDetector->GetPulseCount(), _pulseDetector->GetPulseRateMs()); |
Bobty | 5:5bccf48799d4 | 35 | } |
Bobty | 5:5bccf48799d4 | 36 | return edgeDetected; |
Bobty | 5:5bccf48799d4 | 37 | } |
Bobty | 5:5bccf48799d4 | 38 | |
Bobty | 5:5bccf48799d4 | 39 | // Get the current usage count from SD card |
Bobty | 5:5bccf48799d4 | 40 | void GasUseCounter::GetGasCountFromSD() |
Bobty | 5:5bccf48799d4 | 41 | { |
Bobty | 19:0367cb46d003 | 42 | int count0 = 0; |
Bobty | 19:0367cb46d003 | 43 | int count1 = 0; |
Bobty | 19:0367cb46d003 | 44 | for (int i = 0; i < 2; i++) |
Bobty | 5:5bccf48799d4 | 45 | { |
Bobty | 19:0367cb46d003 | 46 | const char* fname = _gasUseFilename1; |
Bobty | 19:0367cb46d003 | 47 | if (i == 1) |
Bobty | 19:0367cb46d003 | 48 | fname = _gasUseFilename2; |
Bobty | 19:0367cb46d003 | 49 | FILE* fp = fopen(fname, "r"); |
Bobty | 19:0367cb46d003 | 50 | if (fp == NULL) |
Bobty | 19:0367cb46d003 | 51 | { |
Bobty | 19:0367cb46d003 | 52 | _pc.printf ("Read Gas ... Filename %s not found\r\n", fname); |
Bobty | 19:0367cb46d003 | 53 | } |
Bobty | 5:5bccf48799d4 | 54 | else |
Bobty | 19:0367cb46d003 | 55 | { |
Bobty | 19:0367cb46d003 | 56 | int curCount = 0; |
Bobty | 19:0367cb46d003 | 57 | int retVal = fscanf(fp, "%d", &curCount); |
Bobty | 19:0367cb46d003 | 58 | fclose(fp); |
Bobty | 19:0367cb46d003 | 59 | if (retVal == 1) |
Bobty | 19:0367cb46d003 | 60 | _pc.printf ("Read from file %s last gas count = %d\r\n", fname, curCount); |
Bobty | 19:0367cb46d003 | 61 | else |
Bobty | 19:0367cb46d003 | 62 | _pc.printf ("Failed to read gas count from file %s\r\n", fname); |
Bobty | 19:0367cb46d003 | 63 | if (i == 0) |
Bobty | 19:0367cb46d003 | 64 | count0 = curCount; |
Bobty | 19:0367cb46d003 | 65 | else |
Bobty | 19:0367cb46d003 | 66 | count1 = curCount; |
Bobty | 19:0367cb46d003 | 67 | } |
Bobty | 5:5bccf48799d4 | 68 | } |
Bobty | 19:0367cb46d003 | 69 | // Find the highest count |
Bobty | 19:0367cb46d003 | 70 | int maxCount = count0; |
Bobty | 19:0367cb46d003 | 71 | if (count1 > count0) |
Bobty | 19:0367cb46d003 | 72 | maxCount = count1; |
Bobty | 19:0367cb46d003 | 73 | _lastWrittenGasCount = maxCount; |
Bobty | 19:0367cb46d003 | 74 | _pulseDetector->SetPulseCount(maxCount); |
Bobty | 19:0367cb46d003 | 75 | _pc.printf ("Pulse count set to %d\r\n", maxCount); |
Bobty | 5:5bccf48799d4 | 76 | } |
Bobty | 5:5bccf48799d4 | 77 | |
Bobty | 5:5bccf48799d4 | 78 | void GasUseCounter::WriteGasCountToSD() |
Bobty | 5:5bccf48799d4 | 79 | { |
Bobty | 19:0367cb46d003 | 80 | for (int i = 0; i < 2; i++) |
Bobty | 5:5bccf48799d4 | 81 | { |
Bobty | 19:0367cb46d003 | 82 | const char* fname = _gasUseFilename1; |
Bobty | 19:0367cb46d003 | 83 | if (i == 1) |
Bobty | 19:0367cb46d003 | 84 | fname = _gasUseFilename2; |
Bobty | 19:0367cb46d003 | 85 | FILE* fp = fopen(fname, "w"); |
Bobty | 19:0367cb46d003 | 86 | if (fp == NULL) |
Bobty | 19:0367cb46d003 | 87 | { |
Bobty | 19:0367cb46d003 | 88 | _pc.printf ("WriteGas ... Filename %s not found\r\n", fname); |
Bobty | 19:0367cb46d003 | 89 | } |
Bobty | 19:0367cb46d003 | 90 | else |
Bobty | 19:0367cb46d003 | 91 | { |
Bobty | 19:0367cb46d003 | 92 | fprintf(fp, "%d", GetCount()); |
Bobty | 19:0367cb46d003 | 93 | _pc.printf ("Written to %s last gas count = %d\r\n", fname, GetCount()); |
Bobty | 19:0367cb46d003 | 94 | fclose(fp); |
Bobty | 19:0367cb46d003 | 95 | } |
Bobty | 5:5bccf48799d4 | 96 | } |
Bobty | 5:5bccf48799d4 | 97 | } |