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:
19:0367cb46d003
Parent:
16:89778849e9f7
Child:
20:7933076df5af
--- a/RdDS18B20.cpp	Mon Sep 28 11:16:46 2015 +0000
+++ b/RdDS18B20.cpp	Mon Oct 05 14:05:33 2015 +0000
@@ -133,58 +133,90 @@
 
 int DS18B20::SearchToGetAddresses()
 {
-    _numValidAddresses = 0;
-    for (int addrIdx = 0; addrIdx < MAX_BUS_DEVICES; addrIdx++)
-    {
-        uint8_t addr[8];
+    
+    const int MAX_ADDR_SEARCH_RETRIES = 5;
 
-        if ( !_oneWire.search(addr))
+    // Address Table
+    uint8_t tmpAddrTable[MAX_BUS_DEVICES][ONEWIRE_ADDR_BYTES];
+    int validAddresses = 0;
+    bool okResultAchieved = false;
+        
+    // Try a number of times
+    for (int retryCount = 0; retryCount < MAX_ADDR_SEARCH_RETRIES; retryCount++)
+    {
+        // Check if the last search was ok (if there was one)
+        if (okResultAchieved)
         {
-#ifdef SHOW_18B20_DEBUGGING
-            printf("No more addresses.\r\n");
-#endif
-            _oneWire.reset_search();
+            // Copy found addresses
+            for (int addrIdx = 0; addrIdx < validAddresses; addrIdx++)
+            {
+                for( int i = 0; i < ONEWIRE_ADDR_BYTES; i++) 
+                    _addrTable[addrIdx][i] = tmpAddrTable[addrIdx][i];
+            }
+            _numValidAddresses = validAddresses;
             break;
         }
         
-#ifdef SHOW_18B20_DEBUGGING
-        printf("Found device addr (ROM) =");
-#endif
-        for( int i = 0; i < ONEWIRE_ADDR_BYTES; i++) 
+        // Start another search
+        validAddresses = 0;
+        _oneWire.reset_search();
+        for (int addrIdx = 0; addrIdx < MAX_BUS_DEVICES; addrIdx++)
         {
-            // Copy to table
-            _addrTable[_numValidAddresses][i] = addr[i];
+            uint8_t addr[8];
+            uint8_t rslt = _oneWire.search(addr);
+            if (rslt == ONEWIRE_SEARCH_ALL_DONE)
+            {
+                if (validAddresses >= _numValidAddresses)
+                    okResultAchieved = true;
+                break;
+            }
+            if (rslt != ONEWIRE_OK)
+            {
 #ifdef SHOW_18B20_DEBUGGING
-            printf(" %02x", addr[i]);
+                printf("Search returned %s\r\n", (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])
-            _numValidAddresses++;
+            // Check CRC - only include if CRC is valid
+            if (_oneWire.CRC(addr, ONEWIRE_ADDR_BYTES-1) == addr[ONEWIRE_ADDR_BYTES-1])
+                validAddresses++;
 #ifdef SHOW_18B20_DEBUGGING
-        else
-            printf(" (CRC INVALID!)");
+            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;
-        } 
+            // 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;
+            } 
 #endif
+        }
     }
-    return _numValidAddresses;
+    return validAddresses;
 }