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