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
Thermometers.cpp@23:fd5a5a9f30bc, 2015-10-16 (annotated)
- Committer:
- Bobty
- Date:
- Fri Oct 16 09:07:04 2015 +0000
- Revision:
- 23:fd5a5a9f30bc
- Parent:
- 21:ccf053bab795
Added index.html file to project for completeness
Who changed what in which revision?
User | Revision | Line number | New 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 | 19:0367cb46d003 | 7 | const int DEBUG_EXPECTED_THERMOMETER_COUNT = 3; |
Bobty | 9:0e103c2f869a | 8 | |
Bobty | 20:7933076df5af | 9 | Thermometers::Thermometers(int numTempSensorPins, const PinName tempSensorPins[], int serviceIntervalInMs, Logger &logger) : |
Bobty | 20:7933076df5af | 10 | _logger(logger) |
Bobty | 9:0e103c2f869a | 11 | { |
Bobty | 9:0e103c2f869a | 12 | _numTempSensorPins = numTempSensorPins; |
Bobty | 9:0e103c2f869a | 13 | _tempSensorPins = tempSensorPins; |
Bobty | 9:0e103c2f869a | 14 | _serviceIntervalInMs = serviceIntervalInMs; |
Bobty | 9:0e103c2f869a | 15 | _numThermometerBuses = 0; |
Bobty | 16:89778849e9f7 | 16 | _failAddrCount = 0; |
Bobty | 16:89778849e9f7 | 17 | _failReadCount = 0; |
Bobty | 9:0e103c2f869a | 18 | } |
Bobty | 9:0e103c2f869a | 19 | |
Bobty | 9:0e103c2f869a | 20 | void Thermometers::Init() |
Bobty | 9:0e103c2f869a | 21 | { |
Bobty | 9:0e103c2f869a | 22 | // Setup the thermometers |
Bobty | 9:0e103c2f869a | 23 | for (int busIdx = 0; busIdx < _numTempSensorPins; busIdx++) |
Bobty | 9:0e103c2f869a | 24 | { |
Bobty | 9:0e103c2f869a | 25 | if (busIdx >= MAX_ONEWIRE_BUSES) |
Bobty | 9:0e103c2f869a | 26 | break; |
Bobty | 20:7933076df5af | 27 | _thermometerBuses[busIdx] = new DS18B20(_tempSensorPins[busIdx], _logger); |
Bobty | 9:0e103c2f869a | 28 | DS18B20* pThermBus = _thermometerBuses[busIdx]; |
Bobty | 9:0e103c2f869a | 29 | pThermBus->SearchToGetAddresses(); |
Bobty | 9:0e103c2f869a | 30 | pThermBus->ReqConvert(); |
Bobty | 9:0e103c2f869a | 31 | _numThermometerBuses++; |
Bobty | 9:0e103c2f869a | 32 | } |
Bobty | 9:0e103c2f869a | 33 | } |
Bobty | 9:0e103c2f869a | 34 | |
Bobty | 9:0e103c2f869a | 35 | void Thermometers::Service() |
Bobty | 9:0e103c2f869a | 36 | { |
Bobty | 9:0e103c2f869a | 37 | int numLoopsPerThermReading = numSecondsBetweenThermReadings*1000/_serviceIntervalInMs; |
Bobty | 9:0e103c2f869a | 38 | int loopCountForRequestingThermReading = numLoopsPerThermReading - (timeForThermReadingInSecs*1000/_serviceIntervalInMs); |
Bobty | 9:0e103c2f869a | 39 | |
Bobty | 9:0e103c2f869a | 40 | // Check if thermometer addresses need to be got |
Bobty | 9:0e103c2f869a | 41 | if (_countForThermReadings++ == 0) |
Bobty | 9:0e103c2f869a | 42 | { |
Bobty | 9:0e103c2f869a | 43 | if (_countForGetThermometerAddresses++ == 0) |
Bobty | 9:0e103c2f869a | 44 | { |
Bobty | 9:0e103c2f869a | 45 | #ifdef SHOW_THERMOMETER_DEBUGGING |
Bobty | 20:7933076df5af | 46 | _logger.LogDebug("ThermReqAddr"); |
Bobty | 9:0e103c2f869a | 47 | #endif |
Bobty | 9:0e103c2f869a | 48 | for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++) |
Bobty | 9:0e103c2f869a | 49 | { |
Bobty | 9:0e103c2f869a | 50 | DS18B20* pThermBus = _thermometerBuses[busIdx]; |
Bobty | 16:89778849e9f7 | 51 | int numTherms = pThermBus->SearchToGetAddresses(); |
Bobty | 19:0367cb46d003 | 52 | if (numTherms != DEBUG_EXPECTED_THERMOMETER_COUNT) |
Bobty | 16:89778849e9f7 | 53 | _failAddrCount++; |
Bobty | 9:0e103c2f869a | 54 | } |
Bobty | 20:7933076df5af | 55 | |
Bobty | 20:7933076df5af | 56 | #ifdef SHOW_THERMOMETER_DEBUGGING |
Bobty | 20:7933076df5af | 57 | for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++) |
Bobty | 20:7933076df5af | 58 | { |
Bobty | 20:7933076df5af | 59 | DS18B20* pThermBus = _thermometerBuses[busIdx]; |
Bobty | 20:7933076df5af | 60 | if(_failAddrCount == 0 && _failReadCount == 0) |
Bobty | 20:7933076df5af | 61 | { |
Bobty | 20:7933076df5af | 62 | _logger.LogDebug("Therm B%d N%d OK", busIdx, pThermBus->GetNumAddresses()); |
Bobty | 20:7933076df5af | 63 | } |
Bobty | 20:7933076df5af | 64 | else |
Bobty | 20:7933076df5af | 65 | { |
Bobty | 20:7933076df5af | 66 | _logger.LogDebug("Therm B%d N%d FailAddr %d FailRead %d", busIdx, pThermBus->GetNumAddresses(), |
Bobty | 20:7933076df5af | 67 | _failAddrCount, _failReadCount); |
Bobty | 20:7933076df5af | 68 | } |
Bobty | 20:7933076df5af | 69 | for (int addrIdx = 0; addrIdx < pThermBus->GetNumAddresses(); addrIdx++) |
Bobty | 20:7933076df5af | 70 | { |
Bobty | 20:7933076df5af | 71 | char buf [40]; |
Bobty | 20:7933076df5af | 72 | pThermBus->DebugGetAddress(addrIdx, buf); |
Bobty | 20:7933076df5af | 73 | _logger.LogDebug("Therm B%d N%d %s", busIdx, addrIdx, buf); |
Bobty | 20:7933076df5af | 74 | } |
Bobty | 20:7933076df5af | 75 | } |
Bobty | 20:7933076df5af | 76 | #endif |
Bobty | 9:0e103c2f869a | 77 | } |
Bobty | 9:0e103c2f869a | 78 | else if (_countForGetThermometerAddresses > reGetThermometerAddressesAfterNumReadings) |
Bobty | 9:0e103c2f869a | 79 | { |
Bobty | 9:0e103c2f869a | 80 | _countForGetThermometerAddresses = 0; |
Bobty | 9:0e103c2f869a | 81 | } |
Bobty | 9:0e103c2f869a | 82 | } |
Bobty | 9:0e103c2f869a | 83 | else |
Bobty | 9:0e103c2f869a | 84 | { |
Bobty | 19:0367cb46d003 | 85 | // Check if time to request thermometers to take readings |
Bobty | 9:0e103c2f869a | 86 | if (_countForThermReadings == loopCountForRequestingThermReading) |
Bobty | 9:0e103c2f869a | 87 | { |
Bobty | 9:0e103c2f869a | 88 | #ifdef SHOW_THERMOMETER_DEBUGGING |
Bobty | 20:7933076df5af | 89 | // _logger.LogDebug("ThermReqConv"); |
Bobty | 9:0e103c2f869a | 90 | #endif |
Bobty | 9:0e103c2f869a | 91 | for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++) |
Bobty | 9:0e103c2f869a | 92 | { |
Bobty | 9:0e103c2f869a | 93 | DS18B20* pThermBus = _thermometerBuses[busIdx]; |
Bobty | 9:0e103c2f869a | 94 | pThermBus->ReqConvert(); |
Bobty | 9:0e103c2f869a | 95 | } |
Bobty | 9:0e103c2f869a | 96 | } |
Bobty | 9:0e103c2f869a | 97 | |
Bobty | 19:0367cb46d003 | 98 | // Check if it is time to get the values from the thermometers |
Bobty | 9:0e103c2f869a | 99 | if (_countForThermReadings > numLoopsPerThermReading) |
Bobty | 9:0e103c2f869a | 100 | { |
Bobty | 9:0e103c2f869a | 101 | _countForThermReadings = 0; |
Bobty | 9:0e103c2f869a | 102 | for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++) |
Bobty | 9:0e103c2f869a | 103 | { |
Bobty | 9:0e103c2f869a | 104 | DS18B20* pThermBus = _thermometerBuses[busIdx]; |
Bobty | 9:0e103c2f869a | 105 | for (int addrIdx = 0; addrIdx < pThermBus->GetNumAddresses(); addrIdx++) |
Bobty | 9:0e103c2f869a | 106 | { |
Bobty | 9:0e103c2f869a | 107 | double tempValue = pThermBus->ReadTemperature(addrIdx); |
Bobty | 16:89778849e9f7 | 108 | if (tempValue == DS18B20::INVALID_TEMPERATURE) |
Bobty | 16:89778849e9f7 | 109 | _failReadCount++; |
Bobty | 9:0e103c2f869a | 110 | } |
Bobty | 9:0e103c2f869a | 111 | } |
Bobty | 20:7933076df5af | 112 | |
Bobty | 20:7933076df5af | 113 | #ifdef SHOW_THERMOMETER_DEBUGGING |
Bobty | 20:7933076df5af | 114 | char tempStrBuf[140]; |
Bobty | 20:7933076df5af | 115 | tempStrBuf[0] = 0; |
Bobty | 20:7933076df5af | 116 | for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++) |
Bobty | 20:7933076df5af | 117 | { |
Bobty | 20:7933076df5af | 118 | DS18B20* pThermBus = _thermometerBuses[busIdx]; |
Bobty | 20:7933076df5af | 119 | for (int addrIdx = 0; addrIdx < pThermBus->GetNumAddresses(); addrIdx++) |
Bobty | 20:7933076df5af | 120 | { |
Bobty | 20:7933076df5af | 121 | time_t timeOfReading = 0; |
Bobty | 20:7933076df5af | 122 | double tempValue = pThermBus->GetLatestTemperature(addrIdx, timeOfReading); |
Bobty | 20:7933076df5af | 123 | int ageInSecs = time(NULL) - timeOfReading; |
Bobty | 20:7933076df5af | 124 | if (tempValue == DS18B20::INVALID_TEMPERATURE) |
Bobty | 21:ccf053bab795 | 125 | sprintf(tempStrBuf+strlen(tempStrBuf), "%.1fC (INVALID) ", tempValue); |
Bobty | 21:ccf053bab795 | 126 | else if (ageInSecs <= 2) |
Bobty | 21:ccf053bab795 | 127 | sprintf(tempStrBuf+strlen(tempStrBuf), "%.1fC ", tempValue); |
Bobty | 20:7933076df5af | 128 | else |
Bobty | 21:ccf053bab795 | 129 | sprintf(tempStrBuf+strlen(tempStrBuf), "%.1fC (%dS ago) ", tempValue, ageInSecs); |
Bobty | 20:7933076df5af | 130 | } |
Bobty | 20:7933076df5af | 131 | } |
Bobty | 20:7933076df5af | 132 | _logger.LogDebug("Therm %s", tempStrBuf); |
Bobty | 20:7933076df5af | 133 | #endif |
Bobty | 20:7933076df5af | 134 | |
Bobty | 20:7933076df5af | 135 | } |
Bobty | 9:0e103c2f869a | 136 | } |
Bobty | 9:0e103c2f869a | 137 | } |
Bobty | 9:0e103c2f869a | 138 | |
Bobty | 9:0e103c2f869a | 139 | int Thermometers::GetTemperatureValues(int maxTempValues, TemperatureValue* tempValues, int maxAgeInSecs) |
Bobty | 9:0e103c2f869a | 140 | { |
Bobty | 9:0e103c2f869a | 141 | // Go through available values |
Bobty | 9:0e103c2f869a | 142 | int curTempValueIdx = 0; |
Bobty | 9:0e103c2f869a | 143 | for (int busIdx = 0; busIdx < _numThermometerBuses; busIdx++) |
Bobty | 9:0e103c2f869a | 144 | { |
Bobty | 9:0e103c2f869a | 145 | DS18B20* pThermBus = _thermometerBuses[busIdx]; |
Bobty | 9:0e103c2f869a | 146 | for (int addrIdx = 0; addrIdx < pThermBus->GetNumAddresses(); addrIdx++) |
Bobty | 9:0e103c2f869a | 147 | { |
Bobty | 9:0e103c2f869a | 148 | time_t timeOfReading = 0; |
Bobty | 9:0e103c2f869a | 149 | double tempValue = pThermBus->GetLatestTemperature(addrIdx, timeOfReading); |
Bobty | 9:0e103c2f869a | 150 | if (tempValue != DS18B20::INVALID_TEMPERATURE) |
Bobty | 9:0e103c2f869a | 151 | { |
Bobty | 9:0e103c2f869a | 152 | if ((time(NULL) - timeOfReading) < maxAgeInSecs) |
Bobty | 9:0e103c2f869a | 153 | { |
Bobty | 9:0e103c2f869a | 154 | tempValues[curTempValueIdx].timeStamp = timeOfReading; |
Bobty | 9:0e103c2f869a | 155 | strncpy(tempValues[curTempValueIdx].address, pThermBus->GetAddressStr(addrIdx), DS18B20::ONEWIRE_ADDR_STRLEN-1); |
Bobty | 9:0e103c2f869a | 156 | tempValues[curTempValueIdx].tempInCentigrade = tempValue; |
Bobty | 9:0e103c2f869a | 157 | curTempValueIdx++; |
Bobty | 9:0e103c2f869a | 158 | if (curTempValueIdx >= maxTempValues) |
Bobty | 9:0e103c2f869a | 159 | break; |
Bobty | 9:0e103c2f869a | 160 | } |
Bobty | 9:0e103c2f869a | 161 | } |
Bobty | 9:0e103c2f869a | 162 | } |
Bobty | 9:0e103c2f869a | 163 | if (curTempValueIdx >= maxTempValues) |
Bobty | 9:0e103c2f869a | 164 | break; |
Bobty | 9:0e103c2f869a | 165 | } |
Bobty | 9:0e103c2f869a | 166 | return curTempValueIdx; |
Bobty | 9:0e103c2f869a | 167 | } |
Bobty | 9:0e103c2f869a | 168 | |
Bobty | 9:0e103c2f869a | 169 |