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@5:5bccf48799d4, 2015-02-17 (annotated)
- Committer:
- Bobty
- Date:
- Tue Feb 17 21:33:39 2015 +0000
- Revision:
- 5:5bccf48799d4
- Child:
- 19:0367cb46d003
Tidied up - but is unstable - web server crashes after some time
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bobty | 5:5bccf48799d4 | 1 | #include "GasUseCounter.h" |
Bobty | 5:5bccf48799d4 | 2 | |
Bobty | 5:5bccf48799d4 | 3 | // Callback from web server to handle getting current gas count |
Bobty | 5:5bccf48799d4 | 4 | char* GasUseCounter::getGasUseCallback(char* cmdStr, char* argStr) |
Bobty | 5:5bccf48799d4 | 5 | { |
Bobty | 5:5bccf48799d4 | 6 | sprintf(_gasCountStr, "{\"gasUseCount\":\"%d\"}", GetCount()); |
Bobty | 5:5bccf48799d4 | 7 | return _gasCountStr; |
Bobty | 5:5bccf48799d4 | 8 | } |
Bobty | 5:5bccf48799d4 | 9 | |
Bobty | 5:5bccf48799d4 | 10 | // Init |
Bobty | 5:5bccf48799d4 | 11 | void GasUseCounter::Init() |
Bobty | 5:5bccf48799d4 | 12 | { |
Bobty | 5:5bccf48799d4 | 13 | GetGasCountFromSD(); |
Bobty | 5:5bccf48799d4 | 14 | } |
Bobty | 5:5bccf48799d4 | 15 | |
Bobty | 5:5bccf48799d4 | 16 | // Service function |
Bobty | 5:5bccf48799d4 | 17 | bool GasUseCounter::Service() |
Bobty | 5:5bccf48799d4 | 18 | { |
Bobty | 5:5bccf48799d4 | 19 | // Check for an edge |
Bobty | 5:5bccf48799d4 | 20 | bool edgeDetected = _pulseDetector->Service(); |
Bobty | 5:5bccf48799d4 | 21 | if (edgeDetected) |
Bobty | 5:5bccf48799d4 | 22 | { |
Bobty | 5:5bccf48799d4 | 23 | // Check if we need to store in non-volatile storage |
Bobty | 5:5bccf48799d4 | 24 | if (GetCount() >= _lastWrittenGasCount + MAX_PULSES_BEFORE_STORE_NV) |
Bobty | 5:5bccf48799d4 | 25 | { |
Bobty | 5:5bccf48799d4 | 26 | WriteGasCountToSD(); |
Bobty | 5:5bccf48799d4 | 27 | _lastWrittenGasCount = GetCount(); |
Bobty | 5:5bccf48799d4 | 28 | } |
Bobty | 5:5bccf48799d4 | 29 | |
Bobty | 5:5bccf48799d4 | 30 | // Show count |
Bobty | 5:5bccf48799d4 | 31 | _pc.printf("Count %d Rate(ms) %d\r\n", _pulseDetector->GetPulseCount(), _pulseDetector->GetPulseRateMs()); |
Bobty | 5:5bccf48799d4 | 32 | } |
Bobty | 5:5bccf48799d4 | 33 | return edgeDetected; |
Bobty | 5:5bccf48799d4 | 34 | } |
Bobty | 5:5bccf48799d4 | 35 | |
Bobty | 5:5bccf48799d4 | 36 | // Get the current usage count from SD card |
Bobty | 5:5bccf48799d4 | 37 | void GasUseCounter::GetGasCountFromSD() |
Bobty | 5:5bccf48799d4 | 38 | { |
Bobty | 5:5bccf48799d4 | 39 | FILE* fp = fopen(_gasUseFilename, "r"); |
Bobty | 5:5bccf48799d4 | 40 | if (fp == NULL) |
Bobty | 5:5bccf48799d4 | 41 | { |
Bobty | 5:5bccf48799d4 | 42 | _pc.printf ("Read Gas ... Filename %s not found\r\n", _gasUseFilename); |
Bobty | 5:5bccf48799d4 | 43 | } |
Bobty | 5:5bccf48799d4 | 44 | else |
Bobty | 5:5bccf48799d4 | 45 | { |
Bobty | 5:5bccf48799d4 | 46 | int curCount = 0; |
Bobty | 5:5bccf48799d4 | 47 | int retVal = fscanf(fp, "%d", &curCount); |
Bobty | 5:5bccf48799d4 | 48 | fclose(fp); |
Bobty | 5:5bccf48799d4 | 49 | if (retVal == 1) |
Bobty | 5:5bccf48799d4 | 50 | _pc.printf ("Read from SD last gas count = %d\r\n", curCount); |
Bobty | 5:5bccf48799d4 | 51 | else |
Bobty | 5:5bccf48799d4 | 52 | _pc.printf ("Failed to read gas count from SD card\r\n"); |
Bobty | 5:5bccf48799d4 | 53 | _lastWrittenGasCount = curCount; |
Bobty | 5:5bccf48799d4 | 54 | _pulseDetector->SetPulseCount(curCount); |
Bobty | 5:5bccf48799d4 | 55 | } |
Bobty | 5:5bccf48799d4 | 56 | } |
Bobty | 5:5bccf48799d4 | 57 | |
Bobty | 5:5bccf48799d4 | 58 | void GasUseCounter::WriteGasCountToSD() |
Bobty | 5:5bccf48799d4 | 59 | { |
Bobty | 5:5bccf48799d4 | 60 | FILE* fp = fopen(_gasUseFilename, "w"); |
Bobty | 5:5bccf48799d4 | 61 | if (fp == NULL) |
Bobty | 5:5bccf48799d4 | 62 | { |
Bobty | 5:5bccf48799d4 | 63 | _pc.printf ("WriteGas ... Filename %s not found\r\n", _gasUseFilename); |
Bobty | 5:5bccf48799d4 | 64 | } |
Bobty | 5:5bccf48799d4 | 65 | else |
Bobty | 5:5bccf48799d4 | 66 | { |
Bobty | 5:5bccf48799d4 | 67 | fprintf(fp, "%d", GetCount()); |
Bobty | 5:5bccf48799d4 | 68 | _pc.printf ("Written to SD last gas count = %d\r\n", GetCount()); |
Bobty | 5:5bccf48799d4 | 69 | fclose(fp); |
Bobty | 5:5bccf48799d4 | 70 | } |
Bobty | 5:5bccf48799d4 | 71 | } |