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/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");
+}