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

Files at this revision

API Documentation at this revision

Comitter:
Bobty
Date:
Tue Oct 13 18:35:20 2015 +0000
Parent:
19:0367cb46d003
Child:
21:ccf053bab795
Commit message:
Improved logging and added a mutex to avoid clashes on SD card access

Changed in this revision

GasUseCounter.cpp Show annotated file Show diff for this revision Revisions of this file
GasUseCounter.h Show annotated file Show diff for this revision Revisions of this file
Logger.cpp Show annotated file Show diff for this revision Revisions of this file
Logger.h Show annotated file Show diff for this revision Revisions of this file
RdDS18B20.cpp Show annotated file Show diff for this revision Revisions of this file
RdDS18B20.h Show annotated file Show diff for this revision Revisions of this file
RdWebServer.lib Show annotated file Show diff for this revision Revisions of this file
Thermometers.cpp Show annotated file Show diff for this revision Revisions of this file
Thermometers.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/GasUseCounter.cpp	Mon Oct 05 14:05:33 2015 +0000
+++ b/GasUseCounter.cpp	Tue Oct 13 18:35:20 2015 +0000
@@ -31,7 +31,7 @@
         }
             
         // Show count
-        _pc.printf("Count %d Rate(ms) %d\r\n", _pulseDetector->GetPulseCount(), _pulseDetector->GetPulseRateMs());
+        _logger.LogDebug("GasCount %d Rate(ms) %d", _pulseDetector->GetPulseCount(), _pulseDetector->GetPulseRateMs());
     }   
     return edgeDetected;
 }
@@ -46,20 +46,32 @@
         const char* fname = _gasUseFilename1;
         if (i == 1)
             fname = _gasUseFilename2;
+
+        // Obtain lock to access sd card
+        _sdCardMutex.lock();
+        
         FILE* fp = fopen(fname, "r");
         if (fp == NULL)
         {
-            _pc.printf ("Read Gas ... Filename %s not found\r\n", fname);
+            // Release lock on sd card
+            _sdCardMutex.unlock();
+
+            _logger.LogDebug("GasCount Read Filename %s not found", fname);            
         }
         else
         {
             int curCount = 0;
             int retVal = fscanf(fp, "%d", &curCount);
             fclose(fp);
+        
+            // Release lock on sd card
+            _sdCardMutex.unlock();
+
+            // Handle result
             if (retVal == 1)
-                _pc.printf ("Read from file %s last gas count = %d\r\n", fname, curCount);
+                _logger.LogDebug("GasCount read from file %s gas count = %d", fname, curCount);
             else
-                _pc.printf ("Failed to read gas count from file %s\r\n", fname);
+                _logger.LogDebug("GasCount Failed to read gas count from file %s", fname);
             if (i == 0)
                 count0 = curCount;
             else
@@ -72,7 +84,7 @@
         maxCount = count1;
     _lastWrittenGasCount = maxCount;
     _pulseDetector->SetPulseCount(maxCount);
-    _pc.printf ("Pulse count set to %d\r\n", maxCount);
+    _logger.LogDebug("GasCount set to %d", maxCount);
 }
 
 void GasUseCounter::WriteGasCountToSD()
@@ -82,16 +94,24 @@
         const char* fname = _gasUseFilename1;
         if (i == 1)
             fname = _gasUseFilename2;
+
+        // Obtain lock to access sd card
+        _sdCardMutex.lock();
+
         FILE* fp = fopen(fname, "w");
         if (fp == NULL)
         {
-            _pc.printf ("WriteGas ... Filename %s not found\r\n", fname);
+            _logger.LogDebug("GasCount write failed filename %s not found", fname);
         }
         else
         {
             fprintf(fp, "%d", GetCount());
-            _pc.printf ("Written to %s last gas count = %d\r\n", fname, GetCount());
+            _logger.LogDebug("GasCount written to %s gas count = %d", fname, GetCount());
             fclose(fp);
         }
+        
+        // Release lock on sd card
+        _sdCardMutex.unlock();
+        
     }
 }
--- a/GasUseCounter.h	Mon Oct 05 14:05:33 2015 +0000
+++ b/GasUseCounter.h	Tue Oct 13 18:35:20 2015 +0000
@@ -3,7 +3,7 @@
 #include "mbed.h"
 #include "PulsePin.h"
 #include "SDFileSystem.h"
-#include <RawSerial.h>
+#include "Logger.h"
 
 const int MAX_GAS_COUNT_STR_LEN = 50;
 const int MAX_PULSES_BEFORE_STORE_NV = 1; // Store at each pulse
@@ -12,8 +12,8 @@
 {
     public:
         // Constructor
-        GasUseCounter(const char* gasUseFilename1, const char* gasUseFilename2, DigitalIn& gasPulsePin, RawSerial &pc) :
-                _gasPulsePin(gasPulsePin), _pc(pc)
+        GasUseCounter(const char* gasUseFilename1, const char* gasUseFilename2, DigitalIn& gasPulsePin, Logger &logger, Mutex &sdCardMutex) :
+                _gasPulsePin(gasPulsePin), _logger(logger), _sdCardMutex(sdCardMutex)
         {
             _gasUseFilename1 = gasUseFilename1;
             _gasUseFilename2 = gasUseFilename2;
@@ -62,7 +62,8 @@
         char _gasCountStr [MAX_GAS_COUNT_STR_LEN];
         DigitalIn& _gasPulsePin;
         PulsePin* _pulseDetector;
-        RawSerial& _pc;
+        Logger &_logger;
+        Mutex &_sdCardMutex;
 };
 
 
--- a/Logger.cpp	Mon Oct 05 14:05:33 2015 +0000
+++ b/Logger.cpp	Tue Oct 13 18:35:20 2015 +0000
@@ -3,10 +3,13 @@
 
 #include "Logger.h"
 
-Logger::Logger(const char* eventLogFileName, const char* dataLogFileBase)
+Logger::Logger(const char* eventLogFileName, const char* dataLogFileBase, Mutex &sdCardMutex) :
+            _sdCardMutex(sdCardMutex)
 {
     _eventLogFileName = eventLogFileName;
     _dataLogFileBase = dataLogFileBase;
+    _logDebugToFile = false;
+    _logDebugToConsole = false;
 }
 
 void Logger::LogEvent(const char* format, ...)
@@ -15,6 +18,11 @@
     time_t seconds = time(NULL);
     strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d\t%H:%M:%S\t", localtime(&seconds));
 
+    // Obtain lock to access sd card - if unable then abort the logging process
+    if (!_sdCardMutex.trylock())
+        return;
+    
+    // Open file
     FILE* fp = fopen(_eventLogFileName, "a");
     if (fp == NULL)
     {
@@ -30,6 +38,10 @@
         fprintf(fp, "\r\n");
         fclose(fp);
     }
+    
+    // Release lock on sd card
+    _sdCardMutex.unlock();
+
 }
 
 // Utility function to log data
@@ -40,6 +52,10 @@
     time_t seconds = time(NULL);
     strftime(fileNameBuf+strlen(fileNameBuf), sizeof(fileNameBuf)-strlen(fileNameBuf), "Data_%Y%m%d.txt", localtime(&seconds));
     
+    // Obtain lock to access sd card - if unable then abort the logging process
+    if (!_sdCardMutex.trylock())
+        return;
+    
     FILE* fp = fopen(fileNameBuf, "a");
     if (fp == NULL)
     {
@@ -54,4 +70,56 @@
         fprintf(fp, "\r\n");
         fclose(fp);
     }
+    
+    // Release lock on sd card
+    _sdCardMutex.unlock();    
 }
+
+// Utility function to log data
+void Logger::LogDebug(const char* format, ...)
+{
+    char fileNameBuf[60];
+    strcpy(fileNameBuf, _dataLogFileBase);
+    time_t seconds = time(NULL);
+    strftime(fileNameBuf+strlen(fileNameBuf), sizeof(fileNameBuf)-strlen(fileNameBuf), "Dbg_%Y%m%d.txt", localtime(&seconds));
+    
+    // Log to file if enabled
+    if (_logDebugToFile)
+    {
+        // Obtain lock to access sd card - if unable then abort the logging process
+        if (_sdCardMutex.trylock())
+        {
+            FILE* fp = fopen(fileNameBuf, "a");
+            if (fp == NULL)
+            {
+                printf ("Data Log ... Filename %s not found\r\n", _eventLogFileName);
+            }
+            else
+            {
+                char timeBuf[40];
+                time_t seconds = time(NULL);
+                strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d\t%H:%M:%S\t", localtime(&seconds));
+                fprintf(fp, timeBuf);        
+                va_list argptr;
+                va_start(argptr, format);
+                vfprintf(fp, format, argptr);
+                va_end(argptr);
+                fprintf(fp, "\r\n");
+            }
+            fclose(fp);
+    
+            // Release lock on sd card
+            _sdCardMutex.unlock();    
+        }
+    }
+
+    // Print to terminal if enabled
+    if (_logDebugToConsole)
+    {
+        va_list argptr;
+        va_start(argptr, format);
+        vprintf(format, argptr);
+        va_end(argptr);
+        printf("\r\n");
+    }
+}
--- a/Logger.h	Mon Oct 05 14:05:33 2015 +0000
+++ b/Logger.h	Tue Oct 13 18:35:20 2015 +0000
@@ -5,17 +5,27 @@
 #define __LOGGER__H
 #include "mbed.h"
 #include <stdarg.h>
+#include "rtos.h"
 
 class Logger
 {
     public:
-        Logger(const char* eventLogFileName, const char* dataLogFileBase);
+        Logger(const char* eventLogFileName, const char* dataLogFileBase, Mutex &sdCardMutex);
         void LogEvent(const char* format, ...);
         void LogData(const char* format, ...);
+        void LogDebug(const char* format, ...);
+        void SetDebugDest(bool logToFile, bool logToConsole)
+        {
+            _logDebugToFile = logToFile;
+            _logDebugToConsole = logToConsole;
+        };
 
     private:
         const char* _eventLogFileName;
         const char* _dataLogFileBase;
+        bool _logDebugToFile;
+        bool _logDebugToConsole;
+        Mutex &_sdCardMutex;
 };
 
 
--- a/RdDS18B20.cpp	Mon Oct 05 14:05:33 2015 +0000
+++ b/RdDS18B20.cpp	Tue Oct 13 18:35:20 2015 +0000
@@ -4,10 +4,10 @@
 
 #include "RdDS18B20.h"
 
-#define SHOW_18B20_DEBUGGING 1
+// #define SHOW_18B20_DEBUGGING 1
 
 // Construct onewire bus with desired pin
-DS18B20::DS18B20(PinName mbedPin) : _oneWire(mbedPin)
+DS18B20::DS18B20(PinName mbedPin, Logger &logger) : _oneWire(mbedPin), _logger(logger)
 {
     _numValidAddresses = 0;
     for (int i = 0; i < MAX_BUS_DEVICES; i++)
@@ -56,7 +56,7 @@
     if (_oneWire.CRC(temperatureVals, sizeof(temperatureVals)/sizeof(int)-1) == temperatureVals[sizeof(temperatureVals)/sizeof(int)-1])
     {
 #ifdef SHOW_18B20_DEBUGGING
-        printf("Temp CRC Fail\r\n");
+        _logger.LogDebug("Temp CRC Fail addr %d", addrIdx);
 #endif
         return INVALID_TEMPERATURE;
     }
@@ -64,7 +64,7 @@
     {
 #ifdef SHOW_18B20_DEBUGGING
         double temperature = ((((int)(temperatureVals[1])) * 256) + temperatureVals[0])*0.0625;
-        printf("Temp = %0.1f", temperature);
+        _logger.LogDebug("Temp = %0.1f", temperature);
 #endif
     }
     
@@ -75,7 +75,7 @@
     if ((temperature < -10) || (temperature > 100))
     {
 #ifdef SHOW_18B20_DEBUGGING
-        printf("Temp out of bounds\r\n");
+        _logger.LogDebug("Temp out of bounds");
 #endif
         return INVALID_TEMPERATURE;
     }
@@ -111,16 +111,16 @@
 }
     
 // Debug print address
-void DS18B20::DebugPrintAddress(int addrIdx)
+void DS18B20::DebugGetAddress(int addrIdx, char* buf)
 {
     // Check valid address
     if ((addrIdx >= _numValidAddresses) || (addrIdx < 0))
     {
-        printf("Invalid addrIdx %d", addrIdx);
+        sprintf(buf, "Invalid addrIdx %d", addrIdx);
         return;
     }
     // Write out address
-    printf(GetAddressStr(addrIdx));
+    strcpy(buf, GetAddressStr(addrIdx));
 }
 
 double DS18B20::GetLatestTemperature(int addrIdx, time_t& timeOfReading)
@@ -133,7 +133,6 @@
 
 int DS18B20::SearchToGetAddresses()
 {
-    
     const int MAX_ADDR_SEARCH_RETRIES = 5;
 
     // Address Table
@@ -173,50 +172,45 @@
             if (rslt != ONEWIRE_OK)
             {
 #ifdef SHOW_18B20_DEBUGGING
-                printf("Search returned %s\r\n", (rslt == ONEWIRE_SEARCH_INIT_FAIL) ? "InitFail" : ((rslt == ONEWIRE_SEARCH_NOT_FOUND) ? "NotFound" : "UnknownError"));
+                _logger.LogDebug("Search returned %s", (rslt == ONEWIRE_SEARCH_INIT_FAIL) ? "InitFail" : ((rslt == ONEWIRE_SEARCH_NOT_FOUND) ? "NotFound" : "UnknownError"));
 #endif
                 break;
             }
             
-#ifdef SHOW_18B20_DEBUGGING
-            printf("Found device addr (ROM) =");
-#endif
             for( int i = 0; i < ONEWIRE_ADDR_BYTES; i++) 
             {
                 // Copy to table
                 tmpAddrTable[validAddresses][i] = addr[i];
-#ifdef SHOW_18B20_DEBUGGING
-                printf(" %02x", addr[i]);
-#endif
             }
         
             // Check CRC - only include if CRC is valid
-            if (_oneWire.CRC(addr, ONEWIRE_ADDR_BYTES-1) == addr[ONEWIRE_ADDR_BYTES-1])
+            bool addrValid = (_oneWire.CRC(addr, ONEWIRE_ADDR_BYTES-1) == addr[ONEWIRE_ADDR_BYTES-1]);
+            if (addrValid)
                 validAddresses++;
-#ifdef SHOW_18B20_DEBUGGING
-            else
-                printf(" (CRC INVALID!)");
-#endif
 
-#ifdef SHOW_18B20_DEBUGGING        
-            // the first ROM byte indicates which chip
-            switch (addr[0])
-            {
-                case 0x10:
-                    printf("  Chip = DS18S20\r\n");  // or old DS1820
-                    break;
-                case 0x28:
-                    printf("  Chip = DS18B20\r\n");
-                    break;
-                case 0x22:
-                    printf("  Chip = DS1822\r\n");
-                    break;
-                default:
-                    printf("  NOT DS18x20 FAMILY\r\n");
-                    break;
-            } 
+#ifdef SHOW_18B20_DEBUGGING
+            _logger.LogDebug("Found addr (ROM) = %02x%02x%02x%02x%02x%02x%02x%02x %s %s", 
+                addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
+                (addrValid ? "CRC OK" : "CRC INVALID"),
+                GetChipId(addr[0]));
 #endif
         }
+        
     }
     return validAddresses;
 }
+
+char* DS18B20::GetChipId(int val)
+{
+    // the first ROM byte indicates which chip
+    switch (val)
+    {
+        case 0x10:
+            return("Chip = DS18S20\r\n");  // or old DS1820
+        case 0x28:
+            return("Chip = DS18B20\r\n");
+        case 0x22:
+            return("Chip = DS1822\r\n");
+    } 
+    return("Chip NOT DS18x20 FAMILY\r\n");
+}
--- a/RdDS18B20.h	Mon Oct 05 14:05:33 2015 +0000
+++ b/RdDS18B20.h	Tue Oct 13 18:35:20 2015 +0000
@@ -7,14 +7,15 @@
 
 #include "mbed.h"
 #include "Onewire.h"
+#include "Logger.h"
 
 class DS18B20
 {
 public:
-    DS18B20(PinName mbedPin);
+    DS18B20(PinName mbedPin, Logger &logger);
     void ReqConvert();
     double ReadTemperature(int addrIdx);
-    void DebugPrintAddress(int addrIdx);
+    void DebugGetAddress(int addrIdx, char* buf);
     int SearchToGetAddresses();
     int GetNumAddresses()
     {
@@ -36,6 +37,9 @@
     time_t _timeOfReadingTable[MAX_BUS_DEVICES];
     Onewire _oneWire;
     char _addrStr[ONEWIRE_ADDR_STRLEN];
+    Logger &_logger;
+    char* GetChipId(int val);
+
 };
 
 #endif
\ No newline at end of file
--- a/RdWebServer.lib	Mon Oct 05 14:05:33 2015 +0000
+++ b/RdWebServer.lib	Tue Oct 13 18:35:20 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/Bobty/code/RdWebServer/#ffa1dddd3da4
+http://mbed.org/users/Bobty/code/RdWebServer/#3c4c10e989b1
--- a/Thermometers.cpp	Mon Oct 05 14:05:33 2015 +0000
+++ b/Thermometers.cpp	Tue Oct 13 18:35:20 2015 +0000
@@ -6,7 +6,8 @@
 #define SHOW_THERMOMETER_DEBUGGING 1
 const int DEBUG_EXPECTED_THERMOMETER_COUNT = 3;
 
-Thermometers::Thermometers(int numTempSensorPins, const PinName tempSensorPins[], int serviceIntervalInMs)
+Thermometers::Thermometers(int numTempSensorPins, const PinName tempSensorPins[], int serviceIntervalInMs, Logger &logger) :
+    _logger(logger)
 {
     _numTempSensorPins = numTempSensorPins;
     _tempSensorPins = tempSensorPins;
@@ -23,7 +24,7 @@
     {
         if (busIdx >= MAX_ONEWIRE_BUSES)
             break;
-        _thermometerBuses[busIdx] = new DS18B20(_tempSensorPins[busIdx]);
+        _thermometerBuses[busIdx] = new DS18B20(_tempSensorPins[busIdx], _logger);
         DS18B20* pThermBus = _thermometerBuses[busIdx];
         pThermBus->SearchToGetAddresses();
         pThermBus->ReqConvert();
@@ -42,7 +43,7 @@
         if (_countForGetThermometerAddresses++ == 0)
         {
 #ifdef SHOW_THERMOMETER_DEBUGGING
-            printf("Requested Addresses\r\n");
+            _logger.LogDebug("ThermReqAddr");
 #endif
             for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++)
             {
@@ -51,6 +52,28 @@
                 if (numTherms != DEBUG_EXPECTED_THERMOMETER_COUNT)
                     _failAddrCount++;
             }
+            
+#ifdef SHOW_THERMOMETER_DEBUGGING
+            for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++)
+            {
+                DS18B20* pThermBus = _thermometerBuses[busIdx];
+                if(_failAddrCount == 0 && _failReadCount == 0)
+                {
+                    _logger.LogDebug("Therm B%d N%d OK", busIdx, pThermBus->GetNumAddresses());
+                }
+                else
+                {
+                    _logger.LogDebug("Therm B%d N%d FailAddr %d FailRead %d", busIdx, pThermBus->GetNumAddresses(),
+                            _failAddrCount, _failReadCount);
+                }
+                for (int addrIdx = 0; addrIdx < pThermBus->GetNumAddresses(); addrIdx++)
+                {
+                    char buf [40];
+                    pThermBus->DebugGetAddress(addrIdx, buf);
+                    _logger.LogDebug("Therm B%d N%d %s", busIdx, addrIdx, buf);
+                }
+            }
+#endif
         }
         else if (_countForGetThermometerAddresses > reGetThermometerAddressesAfterNumReadings)
         {
@@ -63,15 +86,11 @@
         if (_countForThermReadings == loopCountForRequestingThermReading)
         {
 #ifdef SHOW_THERMOMETER_DEBUGGING
-            printf("Requested Conversion\r\n");
+//            _logger.LogDebug("ThermReqConv");
 #endif
             for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++)
             {
                 DS18B20* pThermBus = _thermometerBuses[busIdx];
-#ifdef SHOW_THERMOMETER_DEBUGGING
-                printf("Bus %d Num therms %d Failed Addr %d Failed Read %d\r\n", busIdx, pThermBus->GetNumAddresses(),
-                            _failAddrCount, _failReadCount);
-#endif
                 pThermBus->ReqConvert();
             }                
         }
@@ -80,25 +99,40 @@
         if (_countForThermReadings > numLoopsPerThermReading)
         {
             _countForThermReadings = 0;
-#ifdef SHOW_THERMOMETER_DEBUGGING
-            printf("Reading Temp\r\n");
-#endif
             for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++)
             {
                 DS18B20* pThermBus = _thermometerBuses[busIdx];
                 for (int addrIdx = 0; addrIdx < pThermBus->GetNumAddresses(); addrIdx++)
                 {
                     double tempValue = pThermBus->ReadTemperature(addrIdx);
-#ifdef SHOW_THERMOMETER_DEBUGGING                    
-                    printf("Bus %d Therm %d === %.2fC ... Addr = ", busIdx, addrIdx, tempValue);
-                    pThermBus->DebugPrintAddress(addrIdx);
-                    printf("\r\n");
-#endif
                     if (tempValue == DS18B20::INVALID_TEMPERATURE)
                         _failReadCount++;
                 }
             }                
-        }
+
+#ifdef SHOW_THERMOMETER_DEBUGGING
+            char tempStrBuf[140];
+            tempStrBuf[0] = 0;
+            for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++)
+            {
+                DS18B20* pThermBus = _thermometerBuses[busIdx];
+                for (int addrIdx = 0; addrIdx < pThermBus->GetNumAddresses(); addrIdx++)
+                {
+                    time_t timeOfReading = 0;
+                    double tempValue = pThermBus->GetLatestTemperature(addrIdx, timeOfReading);
+                    int ageInSecs = time(NULL) - timeOfReading;
+                    if (tempValue == DS18B20::INVALID_TEMPERATURE)
+                        sprintf(tempStrBuf+strlen(tempStrBuf), "%.2fC (INVALID) ", tempValue);
+                    else if (ageInSecs == 0)
+                        sprintf(tempStrBuf+strlen(tempStrBuf), "%.2fC ", tempValue);
+                    else
+                        sprintf(tempStrBuf+strlen(tempStrBuf), "%.2fC (%dS ago) ", tempValue, ageInSecs);
+                }
+            }
+            _logger.LogDebug("Therm %s", tempStrBuf);
+#endif
+
+        }        
     }
 }
 
--- a/Thermometers.h	Mon Oct 05 14:05:33 2015 +0000
+++ b/Thermometers.h	Tue Oct 13 18:35:20 2015 +0000
@@ -5,6 +5,7 @@
 #define Thermometers__H
 
 #include "RdDS18B20.h"
+#include "Logger.h"
 
 struct TemperatureValue
 {
@@ -16,7 +17,7 @@
 class Thermometers
 {
 public:
-    Thermometers(int numTempSensorPins, const PinName tempSensorPins[], int serviceIntervalInMs);
+    Thermometers(int numTempSensorPins, const PinName tempSensorPins[], int serviceIntervalInMs, Logger &logger);
     void Init();
     void Service();
     int GetTemperatureValues(int maxTempValues, TemperatureValue* tempValues, int maxAgeInSecs);
@@ -41,6 +42,7 @@
     // DEBUG
     int _failReadCount;
     int _failAddrCount;
+    Logger &_logger;
 };
 
 #endif
--- a/main.cpp	Mon Oct 05 14:05:33 2015 +0000
+++ b/main.cpp	Tue Oct 13 18:35:20 2015 +0000
@@ -40,6 +40,9 @@
 NTPClient ntp;
 const int NTP_REFRESH_INTERVAL_HOURS = 1;
 
+// Mutex for SD card access
+Mutex sdCardMutex;
+
 // File system for SD card
 SDFileSystem sd(p5, p6, p7, p8, "sd");
 
@@ -50,15 +53,15 @@
 const char* dataLogFileBase = "/sd/";
 
 // Logger
-Logger logger(eventLogFileName, dataLogFileBase);
+Logger logger(eventLogFileName, dataLogFileBase, sdCardMutex);
 
 // Gas use counter
 DigitalIn gasPulsePin(p21);
-GasUseCounter gasUseCounter(gasPulseFileName1, gasPulseFileName2, gasPulsePin, pc);
+GasUseCounter gasUseCounter(gasPulseFileName1, gasPulseFileName2, gasPulsePin, logger, sdCardMutex);
 
 // Thermometers - DS18B20 OneWire Thermometer connections
 const PinName tempSensorPins[] = { p22 };
-Thermometers thermometers(sizeof(tempSensorPins)/sizeof(PinName), tempSensorPins, LOOP_DELAY_IN_MS);
+Thermometers thermometers(sizeof(tempSensorPins)/sizeof(PinName), tempSensorPins, LOOP_DELAY_IN_MS, logger);
 
 // Voltage Sensors / Alerters
 const int NUM_VOLT_ALERTERS = 3;
@@ -144,15 +147,15 @@
     int rslt = sendUDPSocket.sendTo(broadcastEndpoint, broadcastMsgBuffer, bytesToSend);
     if (rslt == bytesToSend)
     {
-        pc.printf("Broadcast (len %d) Sent ok %s\r\n", bytesToSend, broadcastMsgBuffer);
+        logger.LogDebug("Broadcast (len %d) Sent ok %s", bytesToSend, broadcastMsgBuffer);
     }
     else if (rslt == -1)
     {
-        pc.printf("Broadcast Failed to send %s\r\n", broadcastMsgBuffer);
+        logger.LogDebug("Broadcast Failed to send %s", broadcastMsgBuffer);
     }
     else
     {
-        pc.printf("Broadcast Didn't send all of %s\r\n", broadcastMsgBuffer);
+        logger.LogDebug("Broadcast Didn't send all of %s", broadcastMsgBuffer);
     }
     
     // Log the data
@@ -172,7 +175,7 @@
 char* setGasUseCallback(int method, char* cmdStr, char* argStr, char* msgBuffer, int msgLen, 
                 int contentLen, unsigned char* pPayload, int payloadLen, int splitPayloadPos)
 {
-    pc.printf("Setting gas use count %s\r\n", argStr);
+    logger.LogDebug("Setting gas use count %s", argStr);
     int newGasUse = 0;
     char* eqStr = strchr(argStr, '=');
     if (eqStr == NULL)
@@ -186,7 +189,7 @@
 void http_thread(void const* arg)
 {
     char* baseWebFolder = "/sd/";
-    RdWebServer webServer;
+    RdWebServer webServer(&sdCardMutex);
     webServer.addCommand("", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, "index.htm", false);
     webServer.addCommand("gear-gr.png", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, NULL, true);
     webServer.addCommand("listfiles", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, "/", false);
@@ -201,18 +204,18 @@
 {
     while (1)
     {
-        pc.printf("Trying to update time...\r\n");
+        logger.LogDebug("Trying to update time...");
         if (ntp.setTime("0.pool.ntp.org") == NTP_OK)
         {
             osDelay(1000);  // This delay is simply to try to improve printf output
-            printf("Set time successfully\r\n");
+            logger.LogDebug("Set time successfully");
             time_t ctTime;
             ctTime = time(NULL);
-            pc.printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
+            logger.LogDebug("Time is set to (UTC): %s", ctime(&ctTime));
         }
         else
         {
-            pc.printf("Cannot set from NTP\r\n");
+            logger.LogDebug("Cannot set from NTP");
         }
         
         // Refresh time every K hours
@@ -225,7 +228,7 @@
                 {
                     osDelay(1000);
                 }
-                pc.printf("%d mins to next NTP time refresh\r\n", (NTP_REFRESH_INTERVAL_HOURS-k-1)*60 + (59-i));
+                logger.LogDebug("%d mins to next NTP time refresh", (NTP_REFRESH_INTERVAL_HOURS-k-1)*60 + (59-i));
             }
         }
     }
@@ -240,7 +243,8 @@
 int main()
 {
     pc.baud(115200);
-    pc.printf("\r\n\r\nGas Monitor V2 - Rob Dobson 2014\r\n");
+    logger.SetDebugDest(true, true);
+    logger.LogDebug("\r\n\r\nGas Monitor V2 - Rob Dobson 2015");
 
     // Initialise thermometers
     thermometers.Init();
@@ -251,8 +255,8 @@
     // Setup ethernet interface
     char macAddr[6];
     mbed_mac_address(macAddr);
-    pc.printf("Ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\r\n", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]); 
-    pc.printf("Connecting to ethernet ...\r\n");
+    logger.LogDebug("Ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]); 
+    logger.LogDebug("Connecting to ethernet ...");
 
     // Init ethernet
     EthernetInterface::init();
@@ -263,7 +267,7 @@
 
     // Connect ethernet
     EthernetInterface::connect();
-    pc.printf("IP Address: %s HostName %s\r\n", EthernetInterface::getIPAddress(), EthernetInterface::getName());
+    logger.LogDebug("IP Address: %s HostName %s", EthernetInterface::getIPAddress(), EthernetInterface::getName());
     
     // NTP Time setter
     Thread ntpTimeSetter(&ntp_thread);
@@ -275,8 +279,8 @@
     bool watchdogCausedRestart = watchdog.WatchdogCausedRestart();
     bool restartCauseRecorded = false;
     
-    // Setup the watchdog for 10s reset
-    watchdog.SetTimeoutSecs(10);
+    // Setup the watchdog for reset
+    watchdog.SetTimeoutSecs(60);
     
     // Time of last broadcast
     time_t timeOfLastBroadcast = time(NULL);
@@ -293,12 +297,12 @@
                 if (watchdogCausedRestart)
                 {
                     logger.LogEvent("Watchdog Restart");
-                    pc.printf("Watchdog Restart\r\n");
+                    logger.LogDebug("Watchdog Restart");
                 }
                 else
                 {
                     logger.LogEvent("Normal Restart");
-                    pc.printf("Normal Restart\r\n");
+                    logger.LogDebug("Normal Restart");
                 }
                 restartCauseRecorded = true;
             }