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