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@9:0e103c2f869a, 2015-02-22 (annotated)
- Committer:
- Bobty
- Date:
- Sun Feb 22 11:57:12 2015 +0000
- Revision:
- 9:0e103c2f869a
- Parent:
- 8:5980547ae71c
- Child:
- 10:72eb217def1f
Moved thermometer code to separate class
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 | 8:5980547ae71c | 7 | // #define SHOW_18B20_DEBUGGING 1 |
Bobty | 8:5980547ae71c | 8 | |
Bobty | 8:5980547ae71c | 9 | // Construct onewire bus with desired pin |
Bobty | 8:5980547ae71c | 10 | DS18B20::DS18B20(PinName mbedPin) : _oneWire(mbedPin) |
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 | 8:5980547ae71c | 46 | int temperatureVals[] = {0,0}; |
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 | 8:5980547ae71c | 55 | // Convert temperature |
Bobty | 8:5980547ae71c | 56 | double temperature = ((temperatureVals[1] * 256) + temperatureVals[0])*0.0625; |
Bobty | 9:0e103c2f869a | 57 | _temperatureTable[addrIdx] = temperature; |
Bobty | 9:0e103c2f869a | 58 | _timeOfReadingTable[addrIdx] = time(NULL); |
Bobty | 8:5980547ae71c | 59 | return temperature; |
Bobty | 8:5980547ae71c | 60 | } |
Bobty | 8:5980547ae71c | 61 | |
Bobty | 8:5980547ae71c | 62 | // Get address for a device |
Bobty | 9:0e103c2f869a | 63 | uint8_t* DS18B20::GetAddress(int addrIdx, uint8_t* addrBufPtr) |
Bobty | 8:5980547ae71c | 64 | { |
Bobty | 8:5980547ae71c | 65 | if ((addrIdx >= _numValidAddresses) || (addrIdx < 0)) |
Bobty | 8:5980547ae71c | 66 | return _addrTable[0]; |
Bobty | 9:0e103c2f869a | 67 | // Make a copy if non-null pointer passed in |
Bobty | 9:0e103c2f869a | 68 | if (addrBufPtr != NULL) |
Bobty | 9:0e103c2f869a | 69 | { |
Bobty | 9:0e103c2f869a | 70 | for( int i = 0; i < ONEWIRE_ADDR_BYTES; i++) |
Bobty | 9:0e103c2f869a | 71 | addrBufPtr[i] = _addrTable[addrIdx][i]; |
Bobty | 9:0e103c2f869a | 72 | } |
Bobty | 8:5980547ae71c | 73 | return _addrTable[addrIdx]; |
Bobty | 8:5980547ae71c | 74 | } |
Bobty | 8:5980547ae71c | 75 | |
Bobty | 8:5980547ae71c | 76 | // Get address as a string |
Bobty | 8:5980547ae71c | 77 | char* DS18B20::GetAddressStr(int addrIdx) |
Bobty | 8:5980547ae71c | 78 | { |
Bobty | 8:5980547ae71c | 79 | if ((addrIdx >= _numValidAddresses) || (addrIdx < 0)) |
Bobty | 8:5980547ae71c | 80 | return ""; |
Bobty | 8:5980547ae71c | 81 | sprintf(_addrStr, "%02x %02x %02x %02x %02x %02x %02x %02x", |
Bobty | 8:5980547ae71c | 82 | _addrTable[addrIdx][0], _addrTable[addrIdx][1], _addrTable[addrIdx][2], |
Bobty | 8:5980547ae71c | 83 | _addrTable[addrIdx][3], _addrTable[addrIdx][4], _addrTable[addrIdx][5], |
Bobty | 8:5980547ae71c | 84 | _addrTable[addrIdx][6], _addrTable[addrIdx][7]); |
Bobty | 8:5980547ae71c | 85 | return _addrStr; |
Bobty | 8:5980547ae71c | 86 | } |
Bobty | 8:5980547ae71c | 87 | |
Bobty | 8:5980547ae71c | 88 | // Debug print address |
Bobty | 8:5980547ae71c | 89 | void DS18B20::DebugPrintAddress(int addrIdx) |
Bobty | 8:5980547ae71c | 90 | { |
Bobty | 8:5980547ae71c | 91 | // Check valid address |
Bobty | 8:5980547ae71c | 92 | if ((addrIdx >= _numValidAddresses) || (addrIdx < 0)) |
Bobty | 8:5980547ae71c | 93 | { |
Bobty | 8:5980547ae71c | 94 | printf("Invalid addrIdx %d", addrIdx); |
Bobty | 8:5980547ae71c | 95 | return; |
Bobty | 8:5980547ae71c | 96 | } |
Bobty | 8:5980547ae71c | 97 | // Write out address |
Bobty | 8:5980547ae71c | 98 | printf(GetAddressStr(addrIdx)); |
Bobty | 8:5980547ae71c | 99 | } |
Bobty | 8:5980547ae71c | 100 | |
Bobty | 9:0e103c2f869a | 101 | double DS18B20::GetLatestTemperature(int addrIdx, time_t& timeOfReading) |
Bobty | 9:0e103c2f869a | 102 | { |
Bobty | 9:0e103c2f869a | 103 | if ((addrIdx >= _numValidAddresses) || (addrIdx < 0)) |
Bobty | 9:0e103c2f869a | 104 | return INVALID_TEMPERATURE; |
Bobty | 9:0e103c2f869a | 105 | timeOfReading = _timeOfReadingTable[addrIdx]; |
Bobty | 9:0e103c2f869a | 106 | return _temperatureTable[addrIdx]; |
Bobty | 9:0e103c2f869a | 107 | } |
Bobty | 9:0e103c2f869a | 108 | |
Bobty | 8:5980547ae71c | 109 | void DS18B20::SearchToGetAddresses() |
Bobty | 8:5980547ae71c | 110 | { |
Bobty | 8:5980547ae71c | 111 | _numValidAddresses = 0; |
Bobty | 8:5980547ae71c | 112 | for (int addrIdx = 0; addrIdx < MAX_BUS_DEVICES; addrIdx++) |
Bobty | 8:5980547ae71c | 113 | { |
Bobty | 8:5980547ae71c | 114 | uint8_t addr[8]; |
Bobty | 8:5980547ae71c | 115 | |
Bobty | 8:5980547ae71c | 116 | if ( !_oneWire.search(addr)) |
Bobty | 8:5980547ae71c | 117 | { |
Bobty | 8:5980547ae71c | 118 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 8:5980547ae71c | 119 | printf("No more addresses.\r\n"); |
Bobty | 8:5980547ae71c | 120 | #endif |
Bobty | 8:5980547ae71c | 121 | _oneWire.reset_search(); |
Bobty | 8:5980547ae71c | 122 | break; |
Bobty | 8:5980547ae71c | 123 | } |
Bobty | 8:5980547ae71c | 124 | |
Bobty | 8:5980547ae71c | 125 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 8:5980547ae71c | 126 | printf("Found device addr (ROM) ="); |
Bobty | 8:5980547ae71c | 127 | #endif |
Bobty | 9:0e103c2f869a | 128 | for( int i = 0; i < ONEWIRE_ADDR_BYTES; i++) |
Bobty | 8:5980547ae71c | 129 | { |
Bobty | 8:5980547ae71c | 130 | // Copy to table |
Bobty | 8:5980547ae71c | 131 | _addrTable[_numValidAddresses][i] = addr[i]; |
Bobty | 8:5980547ae71c | 132 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 8:5980547ae71c | 133 | printf(" %02x", addr[i]); |
Bobty | 8:5980547ae71c | 134 | #endif |
Bobty | 8:5980547ae71c | 135 | } |
Bobty | 8:5980547ae71c | 136 | |
Bobty | 8:5980547ae71c | 137 | // Check CRC - only include if CRC is valid |
Bobty | 8:5980547ae71c | 138 | if (_oneWire.CRC(addr, ONEWIRE_ADDR_BYTES-1) == addr[ONEWIRE_ADDR_BYTES-1]) |
Bobty | 8:5980547ae71c | 139 | _numValidAddresses++; |
Bobty | 8:5980547ae71c | 140 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 8:5980547ae71c | 141 | else |
Bobty | 8:5980547ae71c | 142 | printf(" (CRC INVALID!)"); |
Bobty | 8:5980547ae71c | 143 | #endif |
Bobty | 8:5980547ae71c | 144 | |
Bobty | 8:5980547ae71c | 145 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 8:5980547ae71c | 146 | // the first ROM byte indicates which chip |
Bobty | 8:5980547ae71c | 147 | switch (addr[0]) |
Bobty | 8:5980547ae71c | 148 | { |
Bobty | 8:5980547ae71c | 149 | case 0x10: |
Bobty | 8:5980547ae71c | 150 | printf(" Chip = DS18S20\r\n"); // or old DS1820 |
Bobty | 8:5980547ae71c | 151 | break; |
Bobty | 8:5980547ae71c | 152 | case 0x28: |
Bobty | 8:5980547ae71c | 153 | printf(" Chip = DS18B20\r\n"); |
Bobty | 8:5980547ae71c | 154 | break; |
Bobty | 8:5980547ae71c | 155 | case 0x22: |
Bobty | 8:5980547ae71c | 156 | printf(" Chip = DS1822\r\n"); |
Bobty | 8:5980547ae71c | 157 | break; |
Bobty | 8:5980547ae71c | 158 | default: |
Bobty | 8:5980547ae71c | 159 | printf(" NOT DS18x20 FAMILY\r\n"); |
Bobty | 8:5980547ae71c | 160 | break; |
Bobty | 8:5980547ae71c | 161 | } |
Bobty | 8:5980547ae71c | 162 | #endif |
Bobty | 8:5980547ae71c | 163 | } |
Bobty | 8:5980547ae71c | 164 | } |