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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GasUseCounter.cpp Source File

GasUseCounter.cpp

00001 // Gas usage based on pulses from a gas meter
00002 // Rob Dobson 2015
00003 
00004 #include "GasUseCounter.h"
00005 
00006 // Callback from web server to handle getting current gas count
00007 char* GasUseCounter::getGasUseCallback(char* cmdStr, char* argStr)
00008 {
00009     sprintf(_gasCountStr, "{\"gasUseCount\":\"%d\"}", GetCount());
00010     return _gasCountStr;
00011 }
00012 
00013 // Init
00014 void GasUseCounter::Init()
00015 {
00016     GetGasCountFromSD();
00017 }
00018     
00019 // Service function
00020 bool GasUseCounter::Service()
00021 {
00022     // Check for an edge
00023     bool edgeDetected = _pulseDetector->Service();
00024     if (edgeDetected)
00025     {
00026         // Check if we need to store in non-volatile storage
00027         if (GetCount() >= _lastWrittenGasCount + MAX_PULSES_BEFORE_STORE_NV)
00028         {
00029             WriteGasCountToSD();
00030             _lastWrittenGasCount = GetCount();
00031         }
00032             
00033         // Show count
00034         _logger.LogDebug("GasCount %d Rate(ms) %d", _pulseDetector->GetPulseCount(), _pulseDetector->GetPulseRateMs());
00035     }   
00036     return edgeDetected;
00037 }
00038     
00039 // Get the current usage count from SD card
00040 void GasUseCounter::GetGasCountFromSD()
00041 {
00042     int count0 = 0;
00043     int count1 = 0;
00044     for (int i = 0; i < 2; i++)
00045     {
00046         const char* fname = _gasUseFilename1;
00047         if (i == 1)
00048             fname = _gasUseFilename2;
00049 
00050         // Obtain lock to access sd card
00051         _sdCardMutex.lock();
00052         
00053         FILE* fp = fopen(fname, "r");
00054         if (fp == NULL)
00055         {
00056             // Release lock on sd card
00057             _sdCardMutex.unlock();
00058 
00059             _logger.LogDebug("GasCount Read Filename %s not found", fname);            
00060         }
00061         else
00062         {
00063             int curCount = 0;
00064             int retVal = fscanf(fp, "%d", &curCount);
00065             fclose(fp);
00066         
00067             // Release lock on sd card
00068             _sdCardMutex.unlock();
00069 
00070             // Handle result
00071             if (retVal == 1)
00072                 _logger.LogDebug("GasCount read from file %s gas count = %d", fname, curCount);
00073             else
00074                 _logger.LogDebug("GasCount Failed to read gas count from file %s", fname);
00075             if (i == 0)
00076                 count0 = curCount;
00077             else
00078                 count1 = curCount;
00079         }
00080     }
00081     // Find the highest count
00082     int maxCount = count0;
00083     if (count1 > count0)
00084         maxCount = count1;
00085     _lastWrittenGasCount = maxCount;
00086     _pulseDetector->SetPulseCount(maxCount);
00087     _logger.LogDebug("GasCount set to %d", maxCount);
00088 }
00089 
00090 void GasUseCounter::WriteGasCountToSD()
00091 {
00092     for (int i = 0; i < 2; i++)
00093     {
00094         const char* fname = _gasUseFilename1;
00095         if (i == 1)
00096             fname = _gasUseFilename2;
00097 
00098         // Obtain lock to access sd card
00099         _sdCardMutex.lock();
00100 
00101         FILE* fp = fopen(fname, "w");
00102         if (fp == NULL)
00103         {
00104             _logger.LogDebug("GasCount write failed filename %s not found", fname);
00105         }
00106         else
00107         {
00108             fprintf(fp, "%d", GetCount());
00109             _logger.LogDebug("GasCount written to %s gas count = %d", fname, GetCount());
00110             fclose(fp);
00111         }
00112         
00113         // Release lock on sd card
00114         _sdCardMutex.unlock();
00115         
00116     }
00117 }