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

Committer:
Bobty
Date:
Fri Oct 16 09:07:04 2015 +0000
Revision:
23:fd5a5a9f30bc
Parent:
20:7933076df5af
Added index.html file to project for completeness

Who changed what in which revision?

UserRevisionLine numberNew 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 20:7933076df5af 34 _logger.LogDebug("GasCount %d Rate(ms) %d", _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 20:7933076df5af 49
Bobty 20:7933076df5af 50 // Obtain lock to access sd card
Bobty 20:7933076df5af 51 _sdCardMutex.lock();
Bobty 20:7933076df5af 52
Bobty 19:0367cb46d003 53 FILE* fp = fopen(fname, "r");
Bobty 19:0367cb46d003 54 if (fp == NULL)
Bobty 19:0367cb46d003 55 {
Bobty 20:7933076df5af 56 // Release lock on sd card
Bobty 20:7933076df5af 57 _sdCardMutex.unlock();
Bobty 20:7933076df5af 58
Bobty 20:7933076df5af 59 _logger.LogDebug("GasCount Read Filename %s not found", fname);
Bobty 19:0367cb46d003 60 }
Bobty 5:5bccf48799d4 61 else
Bobty 19:0367cb46d003 62 {
Bobty 19:0367cb46d003 63 int curCount = 0;
Bobty 19:0367cb46d003 64 int retVal = fscanf(fp, "%d", &curCount);
Bobty 19:0367cb46d003 65 fclose(fp);
Bobty 20:7933076df5af 66
Bobty 20:7933076df5af 67 // Release lock on sd card
Bobty 20:7933076df5af 68 _sdCardMutex.unlock();
Bobty 20:7933076df5af 69
Bobty 20:7933076df5af 70 // Handle result
Bobty 19:0367cb46d003 71 if (retVal == 1)
Bobty 20:7933076df5af 72 _logger.LogDebug("GasCount read from file %s gas count = %d", fname, curCount);
Bobty 19:0367cb46d003 73 else
Bobty 20:7933076df5af 74 _logger.LogDebug("GasCount Failed to read gas count from file %s", fname);
Bobty 19:0367cb46d003 75 if (i == 0)
Bobty 19:0367cb46d003 76 count0 = curCount;
Bobty 19:0367cb46d003 77 else
Bobty 19:0367cb46d003 78 count1 = curCount;
Bobty 19:0367cb46d003 79 }
Bobty 5:5bccf48799d4 80 }
Bobty 19:0367cb46d003 81 // Find the highest count
Bobty 19:0367cb46d003 82 int maxCount = count0;
Bobty 19:0367cb46d003 83 if (count1 > count0)
Bobty 19:0367cb46d003 84 maxCount = count1;
Bobty 19:0367cb46d003 85 _lastWrittenGasCount = maxCount;
Bobty 19:0367cb46d003 86 _pulseDetector->SetPulseCount(maxCount);
Bobty 20:7933076df5af 87 _logger.LogDebug("GasCount set to %d", maxCount);
Bobty 5:5bccf48799d4 88 }
Bobty 5:5bccf48799d4 89
Bobty 5:5bccf48799d4 90 void GasUseCounter::WriteGasCountToSD()
Bobty 5:5bccf48799d4 91 {
Bobty 19:0367cb46d003 92 for (int i = 0; i < 2; i++)
Bobty 5:5bccf48799d4 93 {
Bobty 19:0367cb46d003 94 const char* fname = _gasUseFilename1;
Bobty 19:0367cb46d003 95 if (i == 1)
Bobty 19:0367cb46d003 96 fname = _gasUseFilename2;
Bobty 20:7933076df5af 97
Bobty 20:7933076df5af 98 // Obtain lock to access sd card
Bobty 20:7933076df5af 99 _sdCardMutex.lock();
Bobty 20:7933076df5af 100
Bobty 19:0367cb46d003 101 FILE* fp = fopen(fname, "w");
Bobty 19:0367cb46d003 102 if (fp == NULL)
Bobty 19:0367cb46d003 103 {
Bobty 20:7933076df5af 104 _logger.LogDebug("GasCount write failed filename %s not found", fname);
Bobty 19:0367cb46d003 105 }
Bobty 19:0367cb46d003 106 else
Bobty 19:0367cb46d003 107 {
Bobty 19:0367cb46d003 108 fprintf(fp, "%d", GetCount());
Bobty 20:7933076df5af 109 _logger.LogDebug("GasCount written to %s gas count = %d", fname, GetCount());
Bobty 19:0367cb46d003 110 fclose(fp);
Bobty 19:0367cb46d003 111 }
Bobty 20:7933076df5af 112
Bobty 20:7933076df5af 113 // Release lock on sd card
Bobty 20:7933076df5af 114 _sdCardMutex.unlock();
Bobty 20:7933076df5af 115
Bobty 5:5bccf48799d4 116 }
Bobty 5:5bccf48799d4 117 }