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

Revision:
20:7933076df5af
Parent:
19:0367cb46d003
Child:
21:ccf053bab795
--- 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
+
+        }        
     }
 }