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

Committer:
Bobty
Date:
2015-02-17
Revision:
5:5bccf48799d4
Child:
19:0367cb46d003

File content as of revision 5:5bccf48799d4:

#include "GasUseCounter.h"

// Callback from web server to handle getting current gas count
char* GasUseCounter::getGasUseCallback(char* cmdStr, char* argStr)
{
    sprintf(_gasCountStr, "{\"gasUseCount\":\"%d\"}", GetCount());
    return _gasCountStr;
}

// Init
void GasUseCounter::Init()
{
    GetGasCountFromSD();
}
    
// Service function
bool GasUseCounter::Service()
{
    // Check for an edge
    bool edgeDetected = _pulseDetector->Service();
    if (edgeDetected)
    {
        // Check if we need to store in non-volatile storage
        if (GetCount() >= _lastWrittenGasCount + MAX_PULSES_BEFORE_STORE_NV)
        {
            WriteGasCountToSD();
            _lastWrittenGasCount = GetCount();
        }
            
        // Show count
        _pc.printf("Count %d Rate(ms) %d\r\n", _pulseDetector->GetPulseCount(), _pulseDetector->GetPulseRateMs());
    }   
    return edgeDetected;
}
    
// 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 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);
        else
            _pc.printf ("Failed to read gas count from SD card\r\n");
        _lastWrittenGasCount = curCount;
        _pulseDetector->SetPulseCount(curCount);
    }
}

void GasUseCounter::WriteGasCountToSD()
{
    FILE* fp = fopen(_gasUseFilename, "w");
    if (fp == NULL)
    {
        _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);
    }
}