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

Committer:
Bobty
Date:
Mon Sep 28 10:33:14 2015 +0000
Revision:
16:89778849e9f7
Parent:
9:0e103c2f869a
Child:
19:0367cb46d003
Turned on debugging messages on thermometers; Updated web callbacks method signatures

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bobty 9:0e103c2f869a 1 // Handles temperature collection from a number of DS18B20 devices
Bobty 9:0e103c2f869a 2 // Rob Dobson, 2015
Bobty 9:0e103c2f869a 3
Bobty 9:0e103c2f869a 4 #include "Thermometers.h"
Bobty 9:0e103c2f869a 5
Bobty 16:89778849e9f7 6 #define SHOW_THERMOMETER_DEBUGGING 1
Bobty 9:0e103c2f869a 7
Bobty 9:0e103c2f869a 8 Thermometers::Thermometers(int numTempSensorPins, const PinName tempSensorPins[], int serviceIntervalInMs)
Bobty 9:0e103c2f869a 9 {
Bobty 9:0e103c2f869a 10 _numTempSensorPins = numTempSensorPins;
Bobty 9:0e103c2f869a 11 _tempSensorPins = tempSensorPins;
Bobty 9:0e103c2f869a 12 _serviceIntervalInMs = serviceIntervalInMs;
Bobty 9:0e103c2f869a 13 _numThermometerBuses = 0;
Bobty 16:89778849e9f7 14 _failAddrCount = 0;
Bobty 16:89778849e9f7 15 _failReadCount = 0;
Bobty 9:0e103c2f869a 16 }
Bobty 9:0e103c2f869a 17
Bobty 9:0e103c2f869a 18 void Thermometers::Init()
Bobty 9:0e103c2f869a 19 {
Bobty 9:0e103c2f869a 20 // Setup the thermometers
Bobty 9:0e103c2f869a 21 for (int busIdx = 0; busIdx < _numTempSensorPins; busIdx++)
Bobty 9:0e103c2f869a 22 {
Bobty 9:0e103c2f869a 23 if (busIdx >= MAX_ONEWIRE_BUSES)
Bobty 9:0e103c2f869a 24 break;
Bobty 9:0e103c2f869a 25 _thermometerBuses[busIdx] = new DS18B20(_tempSensorPins[busIdx]);
Bobty 9:0e103c2f869a 26 DS18B20* pThermBus = _thermometerBuses[busIdx];
Bobty 9:0e103c2f869a 27 pThermBus->SearchToGetAddresses();
Bobty 9:0e103c2f869a 28 pThermBus->ReqConvert();
Bobty 9:0e103c2f869a 29 _numThermometerBuses++;
Bobty 9:0e103c2f869a 30 }
Bobty 9:0e103c2f869a 31 }
Bobty 9:0e103c2f869a 32
Bobty 9:0e103c2f869a 33 void Thermometers::Service()
Bobty 9:0e103c2f869a 34 {
Bobty 9:0e103c2f869a 35 int numLoopsPerThermReading = numSecondsBetweenThermReadings*1000/_serviceIntervalInMs;
Bobty 9:0e103c2f869a 36 int loopCountForRequestingThermReading = numLoopsPerThermReading - (timeForThermReadingInSecs*1000/_serviceIntervalInMs);
Bobty 9:0e103c2f869a 37
Bobty 9:0e103c2f869a 38 // Check if thermometer addresses need to be got
Bobty 9:0e103c2f869a 39 if (_countForThermReadings++ == 0)
Bobty 9:0e103c2f869a 40 {
Bobty 9:0e103c2f869a 41 if (_countForGetThermometerAddresses++ == 0)
Bobty 9:0e103c2f869a 42 {
Bobty 9:0e103c2f869a 43 #ifdef SHOW_THERMOMETER_DEBUGGING
Bobty 9:0e103c2f869a 44 printf("Requested Addresses\r\n");
Bobty 9:0e103c2f869a 45 #endif
Bobty 9:0e103c2f869a 46 for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++)
Bobty 9:0e103c2f869a 47 {
Bobty 9:0e103c2f869a 48 DS18B20* pThermBus = _thermometerBuses[busIdx];
Bobty 16:89778849e9f7 49 int numTherms = pThermBus->SearchToGetAddresses();
Bobty 16:89778849e9f7 50 if (numTherms != 3)
Bobty 16:89778849e9f7 51 _failAddrCount++;
Bobty 9:0e103c2f869a 52 }
Bobty 9:0e103c2f869a 53 }
Bobty 9:0e103c2f869a 54 else if (_countForGetThermometerAddresses > reGetThermometerAddressesAfterNumReadings)
Bobty 9:0e103c2f869a 55 {
Bobty 9:0e103c2f869a 56 _countForGetThermometerAddresses = 0;
Bobty 9:0e103c2f869a 57 }
Bobty 9:0e103c2f869a 58 }
Bobty 9:0e103c2f869a 59 else
Bobty 9:0e103c2f869a 60 {
Bobty 9:0e103c2f869a 61 // Check if time to request thermometer readings
Bobty 9:0e103c2f869a 62 if (_countForThermReadings == loopCountForRequestingThermReading)
Bobty 9:0e103c2f869a 63 {
Bobty 9:0e103c2f869a 64 #ifdef SHOW_THERMOMETER_DEBUGGING
Bobty 9:0e103c2f869a 65 printf("Requested Conversion\r\n");
Bobty 9:0e103c2f869a 66 #endif
Bobty 9:0e103c2f869a 67 for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++)
Bobty 9:0e103c2f869a 68 {
Bobty 9:0e103c2f869a 69 DS18B20* pThermBus = _thermometerBuses[busIdx];
Bobty 9:0e103c2f869a 70 #ifdef SHOW_THERMOMETER_DEBUGGING
Bobty 16:89778849e9f7 71 printf("Bus %d Num therms %d Failed Addr %d Failed Read %d\r\n", busIdx, pThermBus->GetNumAddresses(),
Bobty 16:89778849e9f7 72 _failAddrCount, _failReadCount);
Bobty 9:0e103c2f869a 73 #endif
Bobty 9:0e103c2f869a 74 pThermBus->ReqConvert();
Bobty 9:0e103c2f869a 75 }
Bobty 9:0e103c2f869a 76 }
Bobty 9:0e103c2f869a 77
Bobty 9:0e103c2f869a 78 // Read thermometers
Bobty 9:0e103c2f869a 79 if (_countForThermReadings > numLoopsPerThermReading)
Bobty 9:0e103c2f869a 80 {
Bobty 9:0e103c2f869a 81 _countForThermReadings = 0;
Bobty 9:0e103c2f869a 82 #ifdef SHOW_THERMOMETER_DEBUGGING
Bobty 9:0e103c2f869a 83 printf("Reading Temp\r\n");
Bobty 9:0e103c2f869a 84 #endif
Bobty 9:0e103c2f869a 85 for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++)
Bobty 9:0e103c2f869a 86 {
Bobty 9:0e103c2f869a 87 DS18B20* pThermBus = _thermometerBuses[busIdx];
Bobty 9:0e103c2f869a 88 for (int addrIdx = 0; addrIdx < pThermBus->GetNumAddresses(); addrIdx++)
Bobty 9:0e103c2f869a 89 {
Bobty 9:0e103c2f869a 90 double tempValue = pThermBus->ReadTemperature(addrIdx);
Bobty 9:0e103c2f869a 91 #ifdef SHOW_THERMOMETER_DEBUGGING
Bobty 9:0e103c2f869a 92 printf("Bus %d Therm %d === %.2fC ... Addr = ", busIdx, addrIdx, tempValue);
Bobty 9:0e103c2f869a 93 pThermBus->DebugPrintAddress(addrIdx);
Bobty 9:0e103c2f869a 94 printf("\r\n");
Bobty 9:0e103c2f869a 95 #endif
Bobty 16:89778849e9f7 96 if (tempValue == DS18B20::INVALID_TEMPERATURE)
Bobty 16:89778849e9f7 97 _failReadCount++;
Bobty 9:0e103c2f869a 98 }
Bobty 9:0e103c2f869a 99 }
Bobty 9:0e103c2f869a 100 }
Bobty 9:0e103c2f869a 101 }
Bobty 9:0e103c2f869a 102 }
Bobty 9:0e103c2f869a 103
Bobty 9:0e103c2f869a 104 int Thermometers::GetTemperatureValues(int maxTempValues, TemperatureValue* tempValues, int maxAgeInSecs)
Bobty 9:0e103c2f869a 105 {
Bobty 9:0e103c2f869a 106 // Go through available values
Bobty 9:0e103c2f869a 107 int curTempValueIdx = 0;
Bobty 9:0e103c2f869a 108 for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++)
Bobty 9:0e103c2f869a 109 {
Bobty 9:0e103c2f869a 110 DS18B20* pThermBus = _thermometerBuses[busIdx];
Bobty 9:0e103c2f869a 111 for (int addrIdx = 0; addrIdx < pThermBus->GetNumAddresses(); addrIdx++)
Bobty 9:0e103c2f869a 112 {
Bobty 9:0e103c2f869a 113 time_t timeOfReading = 0;
Bobty 9:0e103c2f869a 114 double tempValue = pThermBus->GetLatestTemperature(addrIdx, timeOfReading);
Bobty 9:0e103c2f869a 115 if (tempValue != DS18B20::INVALID_TEMPERATURE)
Bobty 9:0e103c2f869a 116 {
Bobty 9:0e103c2f869a 117 if ((time(NULL) - timeOfReading) < maxAgeInSecs)
Bobty 9:0e103c2f869a 118 {
Bobty 9:0e103c2f869a 119 tempValues[curTempValueIdx].timeStamp = timeOfReading;
Bobty 9:0e103c2f869a 120 strncpy(tempValues[curTempValueIdx].address, pThermBus->GetAddressStr(addrIdx), DS18B20::ONEWIRE_ADDR_STRLEN-1);
Bobty 9:0e103c2f869a 121 tempValues[curTempValueIdx].tempInCentigrade = tempValue;
Bobty 9:0e103c2f869a 122 curTempValueIdx++;
Bobty 9:0e103c2f869a 123 if (curTempValueIdx >= maxTempValues)
Bobty 9:0e103c2f869a 124 break;
Bobty 9:0e103c2f869a 125 }
Bobty 9:0e103c2f869a 126 }
Bobty 9:0e103c2f869a 127 }
Bobty 9:0e103c2f869a 128 if (curTempValueIdx >= maxTempValues)
Bobty 9:0e103c2f869a 129 break;
Bobty 9:0e103c2f869a 130 }
Bobty 9:0e103c2f869a 131 return curTempValueIdx;
Bobty 9:0e103c2f869a 132 }
Bobty 9:0e103c2f869a 133
Bobty 9:0e103c2f869a 134