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
RdDS18B20.cpp@21:ccf053bab795, 2015-10-16 (annotated)
- Committer:
- Bobty
- Date:
- Fri Oct 16 08:37:30 2015 +0000
- Revision:
- 21:ccf053bab795
- Parent:
- 20:7933076df5af
Added names to thermometers and pumps; Added file upload and delete
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bobty | 8:5980547ae71c | 1 | // Handles OneWire temperature sensors DB18S20 |
Bobty | 8:5980547ae71c | 2 | // Can handle multiple devices per pin |
Bobty | 8:5980547ae71c | 3 | // Rob Dobson, 2015 |
Bobty | 8:5980547ae71c | 4 | |
Bobty | 8:5980547ae71c | 5 | #include "RdDS18B20.h" |
Bobty | 8:5980547ae71c | 6 | |
Bobty | 20:7933076df5af | 7 | // #define SHOW_18B20_DEBUGGING 1 |
Bobty | 8:5980547ae71c | 8 | |
Bobty | 8:5980547ae71c | 9 | // Construct onewire bus with desired pin |
Bobty | 20:7933076df5af | 10 | DS18B20::DS18B20(PinName mbedPin, Logger &logger) : _oneWire(mbedPin), _logger(logger) |
Bobty | 8:5980547ae71c | 11 | { |
Bobty | 8:5980547ae71c | 12 | _numValidAddresses = 0; |
Bobty | 9:0e103c2f869a | 13 | for (int i = 0; i < MAX_BUS_DEVICES; i++) |
Bobty | 9:0e103c2f869a | 14 | { |
Bobty | 9:0e103c2f869a | 15 | _temperatureTable[i] = INVALID_TEMPERATURE; |
Bobty | 9:0e103c2f869a | 16 | _timeOfReadingTable[i] = 0; |
Bobty | 9:0e103c2f869a | 17 | } |
Bobty | 8:5980547ae71c | 18 | } |
Bobty | 8:5980547ae71c | 19 | |
Bobty | 8:5980547ae71c | 20 | // Request conversion |
Bobty | 8:5980547ae71c | 21 | void DS18B20::ReqConvert() |
Bobty | 8:5980547ae71c | 22 | { |
Bobty | 8:5980547ae71c | 23 | // Request conversion begins |
Bobty | 8:5980547ae71c | 24 | _oneWire.init(); |
Bobty | 8:5980547ae71c | 25 | _oneWire.writeByte(0xCC); |
Bobty | 8:5980547ae71c | 26 | _oneWire.writeByte(0x44); |
Bobty | 8:5980547ae71c | 27 | } |
Bobty | 8:5980547ae71c | 28 | |
Bobty | 8:5980547ae71c | 29 | // Get temperature |
Bobty | 9:0e103c2f869a | 30 | double DS18B20::ReadTemperature(int addrIdx) |
Bobty | 8:5980547ae71c | 31 | { |
Bobty | 8:5980547ae71c | 32 | // Check valid address |
Bobty | 8:5980547ae71c | 33 | if ((addrIdx >= _numValidAddresses) || (addrIdx < 0)) |
Bobty | 9:0e103c2f869a | 34 | return INVALID_TEMPERATURE; |
Bobty | 8:5980547ae71c | 35 | |
Bobty | 8:5980547ae71c | 36 | // Init the bus and req reading |
Bobty | 8:5980547ae71c | 37 | _oneWire.init(); |
Bobty | 8:5980547ae71c | 38 | _oneWire.writeByte(0x55); |
Bobty | 8:5980547ae71c | 39 | |
Bobty | 8:5980547ae71c | 40 | // Send the address |
Bobty | 8:5980547ae71c | 41 | for (int i = 0; i < 8; i++) |
Bobty | 8:5980547ae71c | 42 | _oneWire.writeByte(_addrTable[addrIdx][i]); |
Bobty | 8:5980547ae71c | 43 | _oneWire.writeByte(0xBE); |
Bobty | 8:5980547ae71c | 44 | |
Bobty | 8:5980547ae71c | 45 | // Temperature val |
Bobty | 13:9ec0e11cf3c1 | 46 | unsigned char temperatureVals[9]; |
Bobty | 8:5980547ae71c | 47 | |
Bobty | 8:5980547ae71c | 48 | // Read values back |
Bobty | 8:5980547ae71c | 49 | for (int i = 0; i < sizeof(temperatureVals)/sizeof(int); i++) |
Bobty | 8:5980547ae71c | 50 | { |
Bobty | 8:5980547ae71c | 51 | temperatureVals[i] = _oneWire.readByte(); |
Bobty | 8:5980547ae71c | 52 | } |
Bobty | 8:5980547ae71c | 53 | _oneWire.init(); |
Bobty | 8:5980547ae71c | 54 | |
Bobty | 13:9ec0e11cf3c1 | 55 | // Check the CRC |
Bobty | 13:9ec0e11cf3c1 | 56 | if (_oneWire.CRC(temperatureVals, sizeof(temperatureVals)/sizeof(int)-1) == temperatureVals[sizeof(temperatureVals)/sizeof(int)-1]) |
Bobty | 13:9ec0e11cf3c1 | 57 | { |
Bobty | 13:9ec0e11cf3c1 | 58 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 20:7933076df5af | 59 | _logger.LogDebug("Temp CRC Fail addr %d", addrIdx); |
Bobty | 13:9ec0e11cf3c1 | 60 | #endif |
Bobty | 13:9ec0e11cf3c1 | 61 | return INVALID_TEMPERATURE; |
Bobty | 13:9ec0e11cf3c1 | 62 | } |
Bobty | 13:9ec0e11cf3c1 | 63 | else |
Bobty | 13:9ec0e11cf3c1 | 64 | { |
Bobty | 13:9ec0e11cf3c1 | 65 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 13:9ec0e11cf3c1 | 66 | double temperature = ((((int)(temperatureVals[1])) * 256) + temperatureVals[0])*0.0625; |
Bobty | 20:7933076df5af | 67 | _logger.LogDebug("Temp = %0.1f", temperature); |
Bobty | 13:9ec0e11cf3c1 | 68 | #endif |
Bobty | 13:9ec0e11cf3c1 | 69 | } |
Bobty | 13:9ec0e11cf3c1 | 70 | |
Bobty | 8:5980547ae71c | 71 | // Convert temperature |
Bobty | 13:9ec0e11cf3c1 | 72 | double temperature = ((((int)(temperatureVals[1])) * 256) + temperatureVals[0])*0.0625; |
Bobty | 13:9ec0e11cf3c1 | 73 | |
Bobty | 13:9ec0e11cf3c1 | 74 | // Do a bounds check |
Bobty | 13:9ec0e11cf3c1 | 75 | if ((temperature < -10) || (temperature > 100)) |
Bobty | 13:9ec0e11cf3c1 | 76 | { |
Bobty | 13:9ec0e11cf3c1 | 77 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 20:7933076df5af | 78 | _logger.LogDebug("Temp out of bounds"); |
Bobty | 13:9ec0e11cf3c1 | 79 | #endif |
Bobty | 13:9ec0e11cf3c1 | 80 | return INVALID_TEMPERATURE; |
Bobty | 13:9ec0e11cf3c1 | 81 | } |
Bobty | 9:0e103c2f869a | 82 | _temperatureTable[addrIdx] = temperature; |
Bobty | 9:0e103c2f869a | 83 | _timeOfReadingTable[addrIdx] = time(NULL); |
Bobty | 8:5980547ae71c | 84 | return temperature; |
Bobty | 8:5980547ae71c | 85 | } |
Bobty | 8:5980547ae71c | 86 | |
Bobty | 8:5980547ae71c | 87 | // Get address for a device |
Bobty | 9:0e103c2f869a | 88 | uint8_t* DS18B20::GetAddress(int addrIdx, uint8_t* addrBufPtr) |
Bobty | 8:5980547ae71c | 89 | { |
Bobty | 8:5980547ae71c | 90 | if ((addrIdx >= _numValidAddresses) || (addrIdx < 0)) |
Bobty | 8:5980547ae71c | 91 | return _addrTable[0]; |
Bobty | 9:0e103c2f869a | 92 | // Make a copy if non-null pointer passed in |
Bobty | 9:0e103c2f869a | 93 | if (addrBufPtr != NULL) |
Bobty | 9:0e103c2f869a | 94 | { |
Bobty | 9:0e103c2f869a | 95 | for( int i = 0; i < ONEWIRE_ADDR_BYTES; i++) |
Bobty | 9:0e103c2f869a | 96 | addrBufPtr[i] = _addrTable[addrIdx][i]; |
Bobty | 9:0e103c2f869a | 97 | } |
Bobty | 8:5980547ae71c | 98 | return _addrTable[addrIdx]; |
Bobty | 8:5980547ae71c | 99 | } |
Bobty | 8:5980547ae71c | 100 | |
Bobty | 8:5980547ae71c | 101 | // Get address as a string |
Bobty | 8:5980547ae71c | 102 | char* DS18B20::GetAddressStr(int addrIdx) |
Bobty | 8:5980547ae71c | 103 | { |
Bobty | 8:5980547ae71c | 104 | if ((addrIdx >= _numValidAddresses) || (addrIdx < 0)) |
Bobty | 8:5980547ae71c | 105 | return ""; |
Bobty | 10:72eb217def1f | 106 | sprintf(_addrStr, "%02x%02x%02x%02x%02x%02x%02x%02x", |
Bobty | 8:5980547ae71c | 107 | _addrTable[addrIdx][0], _addrTable[addrIdx][1], _addrTable[addrIdx][2], |
Bobty | 8:5980547ae71c | 108 | _addrTable[addrIdx][3], _addrTable[addrIdx][4], _addrTable[addrIdx][5], |
Bobty | 8:5980547ae71c | 109 | _addrTable[addrIdx][6], _addrTable[addrIdx][7]); |
Bobty | 8:5980547ae71c | 110 | return _addrStr; |
Bobty | 8:5980547ae71c | 111 | } |
Bobty | 8:5980547ae71c | 112 | |
Bobty | 8:5980547ae71c | 113 | // Debug print address |
Bobty | 20:7933076df5af | 114 | void DS18B20::DebugGetAddress(int addrIdx, char* buf) |
Bobty | 8:5980547ae71c | 115 | { |
Bobty | 8:5980547ae71c | 116 | // Check valid address |
Bobty | 8:5980547ae71c | 117 | if ((addrIdx >= _numValidAddresses) || (addrIdx < 0)) |
Bobty | 8:5980547ae71c | 118 | { |
Bobty | 20:7933076df5af | 119 | sprintf(buf, "Invalid addrIdx %d", addrIdx); |
Bobty | 8:5980547ae71c | 120 | return; |
Bobty | 8:5980547ae71c | 121 | } |
Bobty | 8:5980547ae71c | 122 | // Write out address |
Bobty | 20:7933076df5af | 123 | strcpy(buf, GetAddressStr(addrIdx)); |
Bobty | 8:5980547ae71c | 124 | } |
Bobty | 8:5980547ae71c | 125 | |
Bobty | 9:0e103c2f869a | 126 | double DS18B20::GetLatestTemperature(int addrIdx, time_t& timeOfReading) |
Bobty | 9:0e103c2f869a | 127 | { |
Bobty | 9:0e103c2f869a | 128 | if ((addrIdx >= _numValidAddresses) || (addrIdx < 0)) |
Bobty | 9:0e103c2f869a | 129 | return INVALID_TEMPERATURE; |
Bobty | 9:0e103c2f869a | 130 | timeOfReading = _timeOfReadingTable[addrIdx]; |
Bobty | 9:0e103c2f869a | 131 | return _temperatureTable[addrIdx]; |
Bobty | 9:0e103c2f869a | 132 | } |
Bobty | 9:0e103c2f869a | 133 | |
Bobty | 16:89778849e9f7 | 134 | int DS18B20::SearchToGetAddresses() |
Bobty | 8:5980547ae71c | 135 | { |
Bobty | 19:0367cb46d003 | 136 | const int MAX_ADDR_SEARCH_RETRIES = 5; |
Bobty | 8:5980547ae71c | 137 | |
Bobty | 19:0367cb46d003 | 138 | // Address Table |
Bobty | 19:0367cb46d003 | 139 | uint8_t tmpAddrTable[MAX_BUS_DEVICES][ONEWIRE_ADDR_BYTES]; |
Bobty | 19:0367cb46d003 | 140 | int validAddresses = 0; |
Bobty | 19:0367cb46d003 | 141 | bool okResultAchieved = false; |
Bobty | 19:0367cb46d003 | 142 | |
Bobty | 19:0367cb46d003 | 143 | // Try a number of times |
Bobty | 19:0367cb46d003 | 144 | for (int retryCount = 0; retryCount < MAX_ADDR_SEARCH_RETRIES; retryCount++) |
Bobty | 19:0367cb46d003 | 145 | { |
Bobty | 19:0367cb46d003 | 146 | // Check if the last search was ok (if there was one) |
Bobty | 19:0367cb46d003 | 147 | if (okResultAchieved) |
Bobty | 8:5980547ae71c | 148 | { |
Bobty | 19:0367cb46d003 | 149 | // Copy found addresses |
Bobty | 19:0367cb46d003 | 150 | for (int addrIdx = 0; addrIdx < validAddresses; addrIdx++) |
Bobty | 19:0367cb46d003 | 151 | { |
Bobty | 19:0367cb46d003 | 152 | for( int i = 0; i < ONEWIRE_ADDR_BYTES; i++) |
Bobty | 19:0367cb46d003 | 153 | _addrTable[addrIdx][i] = tmpAddrTable[addrIdx][i]; |
Bobty | 19:0367cb46d003 | 154 | } |
Bobty | 19:0367cb46d003 | 155 | _numValidAddresses = validAddresses; |
Bobty | 8:5980547ae71c | 156 | break; |
Bobty | 8:5980547ae71c | 157 | } |
Bobty | 8:5980547ae71c | 158 | |
Bobty | 19:0367cb46d003 | 159 | // Start another search |
Bobty | 19:0367cb46d003 | 160 | validAddresses = 0; |
Bobty | 19:0367cb46d003 | 161 | _oneWire.reset_search(); |
Bobty | 19:0367cb46d003 | 162 | for (int addrIdx = 0; addrIdx < MAX_BUS_DEVICES; addrIdx++) |
Bobty | 8:5980547ae71c | 163 | { |
Bobty | 19:0367cb46d003 | 164 | uint8_t addr[8]; |
Bobty | 19:0367cb46d003 | 165 | uint8_t rslt = _oneWire.search(addr); |
Bobty | 19:0367cb46d003 | 166 | if (rslt == ONEWIRE_SEARCH_ALL_DONE) |
Bobty | 19:0367cb46d003 | 167 | { |
Bobty | 19:0367cb46d003 | 168 | if (validAddresses >= _numValidAddresses) |
Bobty | 19:0367cb46d003 | 169 | okResultAchieved = true; |
Bobty | 19:0367cb46d003 | 170 | break; |
Bobty | 19:0367cb46d003 | 171 | } |
Bobty | 19:0367cb46d003 | 172 | if (rslt != ONEWIRE_OK) |
Bobty | 19:0367cb46d003 | 173 | { |
Bobty | 8:5980547ae71c | 174 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 21:ccf053bab795 | 175 | // _logger.LogDebug("Search returned %s", _oneWire.GetErrorStr(rslt)); |
Bobty | 21:ccf053bab795 | 176 | _logger.LogDebug("Search returned %d", rslt); |
Bobty | 19:0367cb46d003 | 177 | #endif |
Bobty | 19:0367cb46d003 | 178 | break; |
Bobty | 19:0367cb46d003 | 179 | } |
Bobty | 19:0367cb46d003 | 180 | |
Bobty | 19:0367cb46d003 | 181 | for( int i = 0; i < ONEWIRE_ADDR_BYTES; i++) |
Bobty | 19:0367cb46d003 | 182 | { |
Bobty | 19:0367cb46d003 | 183 | // Copy to table |
Bobty | 19:0367cb46d003 | 184 | tmpAddrTable[validAddresses][i] = addr[i]; |
Bobty | 19:0367cb46d003 | 185 | } |
Bobty | 8:5980547ae71c | 186 | |
Bobty | 19:0367cb46d003 | 187 | // Check CRC - only include if CRC is valid |
Bobty | 20:7933076df5af | 188 | bool addrValid = (_oneWire.CRC(addr, ONEWIRE_ADDR_BYTES-1) == addr[ONEWIRE_ADDR_BYTES-1]); |
Bobty | 20:7933076df5af | 189 | if (addrValid) |
Bobty | 19:0367cb46d003 | 190 | validAddresses++; |
Bobty | 8:5980547ae71c | 191 | |
Bobty | 20:7933076df5af | 192 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 20:7933076df5af | 193 | _logger.LogDebug("Found addr (ROM) = %02x%02x%02x%02x%02x%02x%02x%02x %s %s", |
Bobty | 20:7933076df5af | 194 | addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7], |
Bobty | 20:7933076df5af | 195 | (addrValid ? "CRC OK" : "CRC INVALID"), |
Bobty | 20:7933076df5af | 196 | GetChipId(addr[0])); |
Bobty | 8:5980547ae71c | 197 | #endif |
Bobty | 19:0367cb46d003 | 198 | } |
Bobty | 20:7933076df5af | 199 | |
Bobty | 16:89778849e9f7 | 200 | } |
Bobty | 19:0367cb46d003 | 201 | return validAddresses; |
Bobty | 8:5980547ae71c | 202 | } |
Bobty | 20:7933076df5af | 203 | |
Bobty | 20:7933076df5af | 204 | char* DS18B20::GetChipId(int val) |
Bobty | 20:7933076df5af | 205 | { |
Bobty | 20:7933076df5af | 206 | // the first ROM byte indicates which chip |
Bobty | 20:7933076df5af | 207 | switch (val) |
Bobty | 20:7933076df5af | 208 | { |
Bobty | 20:7933076df5af | 209 | case 0x10: |
Bobty | 21:ccf053bab795 | 210 | return("Chip = DS18S20"); // or old DS1820 |
Bobty | 20:7933076df5af | 211 | case 0x28: |
Bobty | 21:ccf053bab795 | 212 | return("Chip = DS18B20"); |
Bobty | 20:7933076df5af | 213 | case 0x22: |
Bobty | 21:ccf053bab795 | 214 | return("Chip = DS1822"); |
Bobty | 20:7933076df5af | 215 | } |
Bobty | 21:ccf053bab795 | 216 | return("Chip NOT DS18x20 FAMILY"); |
Bobty | 20:7933076df5af | 217 | } |