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@8:5980547ae71c, 2015-02-21 (annotated)
- Committer:
- Bobty
- Date:
- Sat Feb 21 19:00:08 2015 +0000
- Revision:
- 8:5980547ae71c
- Child:
- 9:0e103c2f869a
Working with thermometer test code
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 | 8:5980547ae71c | 13 | } |
Bobty | 8:5980547ae71c | 14 | |
Bobty | 8:5980547ae71c | 15 | // Request conversion |
Bobty | 8:5980547ae71c | 16 | void DS18B20::ReqConvert() |
Bobty | 8:5980547ae71c | 17 | { |
Bobty | 8:5980547ae71c | 18 | // Request conversion begins |
Bobty | 8:5980547ae71c | 19 | _oneWire.init(); |
Bobty | 8:5980547ae71c | 20 | _oneWire.writeByte(0xCC); |
Bobty | 8:5980547ae71c | 21 | _oneWire.writeByte(0x44); |
Bobty | 8:5980547ae71c | 22 | } |
Bobty | 8:5980547ae71c | 23 | |
Bobty | 8:5980547ae71c | 24 | // Get temperature |
Bobty | 8:5980547ae71c | 25 | double DS18B20::GetTemperature(int addrIdx) |
Bobty | 8:5980547ae71c | 26 | { |
Bobty | 8:5980547ae71c | 27 | // Check valid address |
Bobty | 8:5980547ae71c | 28 | if ((addrIdx >= _numValidAddresses) || (addrIdx < 0)) |
Bobty | 8:5980547ae71c | 29 | return -1000.0; |
Bobty | 8:5980547ae71c | 30 | |
Bobty | 8:5980547ae71c | 31 | // Init the bus and req reading |
Bobty | 8:5980547ae71c | 32 | _oneWire.init(); |
Bobty | 8:5980547ae71c | 33 | _oneWire.writeByte(0x55); |
Bobty | 8:5980547ae71c | 34 | |
Bobty | 8:5980547ae71c | 35 | // Send the address |
Bobty | 8:5980547ae71c | 36 | for (int i = 0; i < 8; i++) |
Bobty | 8:5980547ae71c | 37 | _oneWire.writeByte(_addrTable[addrIdx][i]); |
Bobty | 8:5980547ae71c | 38 | _oneWire.writeByte(0xBE); |
Bobty | 8:5980547ae71c | 39 | |
Bobty | 8:5980547ae71c | 40 | // Temperature val |
Bobty | 8:5980547ae71c | 41 | int temperatureVals[] = {0,0}; |
Bobty | 8:5980547ae71c | 42 | |
Bobty | 8:5980547ae71c | 43 | // Read values back |
Bobty | 8:5980547ae71c | 44 | for (int i = 0; i < sizeof(temperatureVals)/sizeof(int); i++) |
Bobty | 8:5980547ae71c | 45 | { |
Bobty | 8:5980547ae71c | 46 | temperatureVals[i] = _oneWire.readByte(); |
Bobty | 8:5980547ae71c | 47 | } |
Bobty | 8:5980547ae71c | 48 | _oneWire.init(); |
Bobty | 8:5980547ae71c | 49 | |
Bobty | 8:5980547ae71c | 50 | // Convert temperature |
Bobty | 8:5980547ae71c | 51 | double temperature = ((temperatureVals[1] * 256) + temperatureVals[0])*0.0625; |
Bobty | 8:5980547ae71c | 52 | return temperature; |
Bobty | 8:5980547ae71c | 53 | } |
Bobty | 8:5980547ae71c | 54 | |
Bobty | 8:5980547ae71c | 55 | // Get address for a device |
Bobty | 8:5980547ae71c | 56 | uint8_t* DS18B20::GetAddress(int addrIdx) |
Bobty | 8:5980547ae71c | 57 | { |
Bobty | 8:5980547ae71c | 58 | if ((addrIdx >= _numValidAddresses) || (addrIdx < 0)) |
Bobty | 8:5980547ae71c | 59 | return _addrTable[0]; |
Bobty | 8:5980547ae71c | 60 | return _addrTable[addrIdx]; |
Bobty | 8:5980547ae71c | 61 | } |
Bobty | 8:5980547ae71c | 62 | |
Bobty | 8:5980547ae71c | 63 | // Get address as a string |
Bobty | 8:5980547ae71c | 64 | char* DS18B20::GetAddressStr(int addrIdx) |
Bobty | 8:5980547ae71c | 65 | { |
Bobty | 8:5980547ae71c | 66 | if ((addrIdx >= _numValidAddresses) || (addrIdx < 0)) |
Bobty | 8:5980547ae71c | 67 | return ""; |
Bobty | 8:5980547ae71c | 68 | sprintf(_addrStr, "%02x %02x %02x %02x %02x %02x %02x %02x", |
Bobty | 8:5980547ae71c | 69 | _addrTable[addrIdx][0], _addrTable[addrIdx][1], _addrTable[addrIdx][2], |
Bobty | 8:5980547ae71c | 70 | _addrTable[addrIdx][3], _addrTable[addrIdx][4], _addrTable[addrIdx][5], |
Bobty | 8:5980547ae71c | 71 | _addrTable[addrIdx][6], _addrTable[addrIdx][7]); |
Bobty | 8:5980547ae71c | 72 | return _addrStr; |
Bobty | 8:5980547ae71c | 73 | } |
Bobty | 8:5980547ae71c | 74 | |
Bobty | 8:5980547ae71c | 75 | // Debug print address |
Bobty | 8:5980547ae71c | 76 | void DS18B20::DebugPrintAddress(int addrIdx) |
Bobty | 8:5980547ae71c | 77 | { |
Bobty | 8:5980547ae71c | 78 | // Check valid address |
Bobty | 8:5980547ae71c | 79 | if ((addrIdx >= _numValidAddresses) || (addrIdx < 0)) |
Bobty | 8:5980547ae71c | 80 | { |
Bobty | 8:5980547ae71c | 81 | printf("Invalid addrIdx %d", addrIdx); |
Bobty | 8:5980547ae71c | 82 | return; |
Bobty | 8:5980547ae71c | 83 | } |
Bobty | 8:5980547ae71c | 84 | // Write out address |
Bobty | 8:5980547ae71c | 85 | printf(GetAddressStr(addrIdx)); |
Bobty | 8:5980547ae71c | 86 | } |
Bobty | 8:5980547ae71c | 87 | |
Bobty | 8:5980547ae71c | 88 | void DS18B20::SearchToGetAddresses() |
Bobty | 8:5980547ae71c | 89 | { |
Bobty | 8:5980547ae71c | 90 | _numValidAddresses = 0; |
Bobty | 8:5980547ae71c | 91 | for (int addrIdx = 0; addrIdx < MAX_BUS_DEVICES; addrIdx++) |
Bobty | 8:5980547ae71c | 92 | { |
Bobty | 8:5980547ae71c | 93 | uint8_t addr[8]; |
Bobty | 8:5980547ae71c | 94 | |
Bobty | 8:5980547ae71c | 95 | if ( !_oneWire.search(addr)) |
Bobty | 8:5980547ae71c | 96 | { |
Bobty | 8:5980547ae71c | 97 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 8:5980547ae71c | 98 | printf("No more addresses.\r\n"); |
Bobty | 8:5980547ae71c | 99 | #endif |
Bobty | 8:5980547ae71c | 100 | _oneWire.reset_search(); |
Bobty | 8:5980547ae71c | 101 | break; |
Bobty | 8:5980547ae71c | 102 | } |
Bobty | 8:5980547ae71c | 103 | |
Bobty | 8:5980547ae71c | 104 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 8:5980547ae71c | 105 | printf("Found device addr (ROM) ="); |
Bobty | 8:5980547ae71c | 106 | #endif |
Bobty | 8:5980547ae71c | 107 | for( int i = 0; i < 8; i++) |
Bobty | 8:5980547ae71c | 108 | { |
Bobty | 8:5980547ae71c | 109 | // Copy to table |
Bobty | 8:5980547ae71c | 110 | _addrTable[_numValidAddresses][i] = addr[i]; |
Bobty | 8:5980547ae71c | 111 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 8:5980547ae71c | 112 | printf(" %02x", addr[i]); |
Bobty | 8:5980547ae71c | 113 | #endif |
Bobty | 8:5980547ae71c | 114 | } |
Bobty | 8:5980547ae71c | 115 | |
Bobty | 8:5980547ae71c | 116 | // Check CRC - only include if CRC is valid |
Bobty | 8:5980547ae71c | 117 | if (_oneWire.CRC(addr, ONEWIRE_ADDR_BYTES-1) == addr[ONEWIRE_ADDR_BYTES-1]) |
Bobty | 8:5980547ae71c | 118 | _numValidAddresses++; |
Bobty | 8:5980547ae71c | 119 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 8:5980547ae71c | 120 | else |
Bobty | 8:5980547ae71c | 121 | printf(" (CRC INVALID!)"); |
Bobty | 8:5980547ae71c | 122 | #endif |
Bobty | 8:5980547ae71c | 123 | |
Bobty | 8:5980547ae71c | 124 | #ifdef SHOW_18B20_DEBUGGING |
Bobty | 8:5980547ae71c | 125 | // the first ROM byte indicates which chip |
Bobty | 8:5980547ae71c | 126 | switch (addr[0]) |
Bobty | 8:5980547ae71c | 127 | { |
Bobty | 8:5980547ae71c | 128 | case 0x10: |
Bobty | 8:5980547ae71c | 129 | printf(" Chip = DS18S20\r\n"); // or old DS1820 |
Bobty | 8:5980547ae71c | 130 | break; |
Bobty | 8:5980547ae71c | 131 | case 0x28: |
Bobty | 8:5980547ae71c | 132 | printf(" Chip = DS18B20\r\n"); |
Bobty | 8:5980547ae71c | 133 | break; |
Bobty | 8:5980547ae71c | 134 | case 0x22: |
Bobty | 8:5980547ae71c | 135 | printf(" Chip = DS1822\r\n"); |
Bobty | 8:5980547ae71c | 136 | break; |
Bobty | 8:5980547ae71c | 137 | default: |
Bobty | 8:5980547ae71c | 138 | printf(" NOT DS18x20 FAMILY\r\n"); |
Bobty | 8:5980547ae71c | 139 | break; |
Bobty | 8:5980547ae71c | 140 | } |
Bobty | 8:5980547ae71c | 141 | #endif |
Bobty | 8:5980547ae71c | 142 | } |
Bobty | 8:5980547ae71c | 143 | } |