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:
8:5980547ae71c
Parent:
7:113c68639d10
Child:
9:0e103c2f869a
--- a/main.cpp	Fri Feb 20 22:53:28 2015 +0000
+++ b/main.cpp	Sat Feb 21 19:00:08 2015 +0000
@@ -3,10 +3,9 @@
 #include "NTPClient.h"
 #include "RdWebServer.h"
 #include "GasUseCounter.h"
+#include "RdDS18B20.h"
 #include <stdarg.h>
 
-//#define TEST_REMOVE_UDP_AND_NTP 1
-
 // Web and UDB ports 
 const int WEBPORT = 80; // Port for web server
 const int BROADCAST_PORT = 42853; // Arbitrarily chosen port number
@@ -37,6 +36,11 @@
 const char* logFilename = "/sd/log.txt";
 GasUseCounter gasUseCounter(gasPulseFileName, gasPulsePin, pc);
 
+// Thermometers - DS18B20 OneWire Thermometer connections
+const PinName tempSensorPins[] = { p22 };
+const int NUM_THERM_BUSES = sizeof(tempSensorPins)/sizeof(int);
+DS18B20* thermometerBuses[NUM_THERM_BUSES];
+
 void LogData(const char* format, ...)
 {
     FILE* fp = fopen(logFilename, "a");
@@ -156,9 +160,7 @@
             }
             pc.printf("Waited %d mins\r\n", i);
         }
-#ifndef TEST_REMOVE_UDP_AND_NTP
         break;
-#endif                
     }
 }
     
@@ -169,6 +171,18 @@
 
 //    ticker.attach(&TickFunction,TICK_MS / 1000.0);
 
+    // Setup the thermometers
+    for (int thermIdx = 0; thermIdx < NUM_THERM_BUSES; thermIdx++)
+        thermometerBuses[thermIdx] = new DS18B20(tempSensorPins[thermIdx]);
+        
+    // Initialise thermometers   
+    for (int thermIdx = 0; thermIdx < NUM_THERM_BUSES; thermIdx++)
+    {
+        DS18B20* pThermBus = thermometerBuses[thermIdx];
+        pThermBus->SearchToGetAddresses();
+        pThermBus->ReqConvert();
+    }
+
     // Get the current count from the SD Card
     gasUseCounter.Init();
     
@@ -177,22 +191,77 @@
     eth.connect();
     
     pc.printf("IP Address is %s\n\r", eth.getIPAddress());
-
-#ifndef TEST_REMOVE_UDP_AND_NTP
+    
+    // NTP Time setter
     Thread ntpTimeSetter(&ntp_thread);
-#endif
     
+    // Web Server
     Thread httpServer(&http_thread, NULL, osPriorityNormal, (DEFAULT_STACK_SIZE * 3));
     
+    const int loopDelayInMs = 250;
+    const int numSecondsBetweenThermReadings = 10;
+    const int numLoopsPerThermReading = numSecondsBetweenThermReadings*1000/loopDelayInMs;
+    const int timeForThermReadingInSecs = 2;
+    const int loopCountForRequestingThermReading = numLoopsPerThermReading - (timeForThermReadingInSecs*1000/loopDelayInMs);
+    int countForThermReadings = 0;
+    const int reGetThermometerAddressesAfterNumReadings = 100;
+    int countForGetThermometerAddresses = 0;
     while(true)
     {
-        osDelay(250);
+        osDelay(loopDelayInMs);
         led1 = !led1;
 
         // Service gas count
-#ifndef TEST_REMOVE_UDP_AND_NTP
         if (gasUseCounter.Service())
             SendInfoBroadcast();
-#endif
+        
+        // Check if thermometer addresses need to be got
+        if (countForThermReadings++ == 0)
+        {
+            if (countForGetThermometerAddresses++ == 0)
+            {
+                printf("Requested Addresses\n\r");
+                for (int thermIdx = 0; thermIdx < NUM_THERM_BUSES; thermIdx++)
+                {
+                    DS18B20* pThermBus = thermometerBuses[thermIdx];
+                    pThermBus->SearchToGetAddresses();
+                }
+            }
+            else if (countForGetThermometerAddresses > reGetThermometerAddressesAfterNumReadings)
+            {
+                countForGetThermometerAddresses = 0;
+            }
+        }
+        else
+        {
+            // Check if time to request thermometer readings
+            if (countForThermReadings == loopCountForRequestingThermReading)
+            {
+                printf("Requested Convert\n\r");
+                for (int thermIdx = 0; thermIdx < NUM_THERM_BUSES; thermIdx++)
+                {
+                    DS18B20* pThermBus = thermometerBuses[thermIdx];
+                    printf("Bus %d Num therms %d\n\r", thermIdx, pThermBus->GetNumAddresses());
+                    pThermBus->ReqConvert();
+                }                
+            }
+        
+            // Read thermometers
+            if (countForThermReadings > numLoopsPerThermReading)
+            {
+                countForThermReadings = 0;
+                printf("Reading Temp\n\r");
+                for (int thermIdx = 0; thermIdx < NUM_THERM_BUSES; thermIdx++)
+                {
+                    DS18B20* pThermBus = thermometerBuses[thermIdx];
+                    for (int addrIdx = 0; addrIdx < pThermBus->GetNumAddresses(); addrIdx++)
+                    {
+                        printf("Bus %d Therm %d === %.2fC ... Addr = ", thermIdx, addrIdx, pThermBus->GetTemperature(addrIdx));
+                        pThermBus->DebugPrintAddress(addrIdx);
+                        printf("\r\n");
+                    }
+                }                
+            }
+        }
     }
 }