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

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?

UserRevisionLine numberNew 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 16:89778849e9f7 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 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 13:9ec0e11cf3c1 59 printf("Temp CRC Fail\r\n");
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 13:9ec0e11cf3c1 67 printf("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 13:9ec0e11cf3c1 78 printf("Temp out of bounds\r\n");
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 8:5980547ae71c 114 void DS18B20::DebugPrintAddress(int addrIdx)
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 8:5980547ae71c 119 printf("Invalid addrIdx %d", addrIdx);
Bobty 8:5980547ae71c 120 return;
Bobty 8:5980547ae71c 121 }
Bobty 8:5980547ae71c 122 // Write out address
Bobty 8:5980547ae71c 123 printf(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
Bobty 19:0367cb46d003 137 const int MAX_ADDR_SEARCH_RETRIES = 5;
Bobty 8:5980547ae71c 138
Bobty 19:0367cb46d003 139 // Address Table
Bobty 19:0367cb46d003 140 uint8_t tmpAddrTable[MAX_BUS_DEVICES][ONEWIRE_ADDR_BYTES];
Bobty 19:0367cb46d003 141 int validAddresses = 0;
Bobty 19:0367cb46d003 142 bool okResultAchieved = false;
Bobty 19:0367cb46d003 143
Bobty 19:0367cb46d003 144 // Try a number of times
Bobty 19:0367cb46d003 145 for (int retryCount = 0; retryCount < MAX_ADDR_SEARCH_RETRIES; retryCount++)
Bobty 19:0367cb46d003 146 {
Bobty 19:0367cb46d003 147 // Check if the last search was ok (if there was one)
Bobty 19:0367cb46d003 148 if (okResultAchieved)
Bobty 8:5980547ae71c 149 {
Bobty 19:0367cb46d003 150 // Copy found addresses
Bobty 19:0367cb46d003 151 for (int addrIdx = 0; addrIdx < validAddresses; addrIdx++)
Bobty 19:0367cb46d003 152 {
Bobty 19:0367cb46d003 153 for( int i = 0; i < ONEWIRE_ADDR_BYTES; i++)
Bobty 19:0367cb46d003 154 _addrTable[addrIdx][i] = tmpAddrTable[addrIdx][i];
Bobty 19:0367cb46d003 155 }
Bobty 19:0367cb46d003 156 _numValidAddresses = validAddresses;
Bobty 8:5980547ae71c 157 break;
Bobty 8:5980547ae71c 158 }
Bobty 8:5980547ae71c 159
Bobty 19:0367cb46d003 160 // Start another search
Bobty 19:0367cb46d003 161 validAddresses = 0;
Bobty 19:0367cb46d003 162 _oneWire.reset_search();
Bobty 19:0367cb46d003 163 for (int addrIdx = 0; addrIdx < MAX_BUS_DEVICES; addrIdx++)
Bobty 8:5980547ae71c 164 {
Bobty 19:0367cb46d003 165 uint8_t addr[8];
Bobty 19:0367cb46d003 166 uint8_t rslt = _oneWire.search(addr);
Bobty 19:0367cb46d003 167 if (rslt == ONEWIRE_SEARCH_ALL_DONE)
Bobty 19:0367cb46d003 168 {
Bobty 19:0367cb46d003 169 if (validAddresses >= _numValidAddresses)
Bobty 19:0367cb46d003 170 okResultAchieved = true;
Bobty 19:0367cb46d003 171 break;
Bobty 19:0367cb46d003 172 }
Bobty 19:0367cb46d003 173 if (rslt != ONEWIRE_OK)
Bobty 19:0367cb46d003 174 {
Bobty 8:5980547ae71c 175 #ifdef SHOW_18B20_DEBUGGING
Bobty 19:0367cb46d003 176 printf("Search returned %s\r\n", (rslt == ONEWIRE_SEARCH_INIT_FAIL) ? "InitFail" : ((rslt == ONEWIRE_SEARCH_NOT_FOUND) ? "NotFound" : "UnknownError"));
Bobty 19:0367cb46d003 177 #endif
Bobty 19:0367cb46d003 178 break;
Bobty 19:0367cb46d003 179 }
Bobty 19:0367cb46d003 180
Bobty 19:0367cb46d003 181 #ifdef SHOW_18B20_DEBUGGING
Bobty 19:0367cb46d003 182 printf("Found device addr (ROM) =");
Bobty 8:5980547ae71c 183 #endif
Bobty 19:0367cb46d003 184 for( int i = 0; i < ONEWIRE_ADDR_BYTES; i++)
Bobty 19:0367cb46d003 185 {
Bobty 19:0367cb46d003 186 // Copy to table
Bobty 19:0367cb46d003 187 tmpAddrTable[validAddresses][i] = addr[i];
Bobty 19:0367cb46d003 188 #ifdef SHOW_18B20_DEBUGGING
Bobty 19:0367cb46d003 189 printf(" %02x", addr[i]);
Bobty 19:0367cb46d003 190 #endif
Bobty 19:0367cb46d003 191 }
Bobty 8:5980547ae71c 192
Bobty 19:0367cb46d003 193 // Check CRC - only include if CRC is valid
Bobty 19:0367cb46d003 194 if (_oneWire.CRC(addr, ONEWIRE_ADDR_BYTES-1) == addr[ONEWIRE_ADDR_BYTES-1])
Bobty 19:0367cb46d003 195 validAddresses++;
Bobty 8:5980547ae71c 196 #ifdef SHOW_18B20_DEBUGGING
Bobty 19:0367cb46d003 197 else
Bobty 19:0367cb46d003 198 printf(" (CRC INVALID!)");
Bobty 8:5980547ae71c 199 #endif
Bobty 8:5980547ae71c 200
Bobty 8:5980547ae71c 201 #ifdef SHOW_18B20_DEBUGGING
Bobty 19:0367cb46d003 202 // the first ROM byte indicates which chip
Bobty 19:0367cb46d003 203 switch (addr[0])
Bobty 19:0367cb46d003 204 {
Bobty 19:0367cb46d003 205 case 0x10:
Bobty 19:0367cb46d003 206 printf(" Chip = DS18S20\r\n"); // or old DS1820
Bobty 19:0367cb46d003 207 break;
Bobty 19:0367cb46d003 208 case 0x28:
Bobty 19:0367cb46d003 209 printf(" Chip = DS18B20\r\n");
Bobty 19:0367cb46d003 210 break;
Bobty 19:0367cb46d003 211 case 0x22:
Bobty 19:0367cb46d003 212 printf(" Chip = DS1822\r\n");
Bobty 19:0367cb46d003 213 break;
Bobty 19:0367cb46d003 214 default:
Bobty 19:0367cb46d003 215 printf(" NOT DS18x20 FAMILY\r\n");
Bobty 19:0367cb46d003 216 break;
Bobty 19:0367cb46d003 217 }
Bobty 8:5980547ae71c 218 #endif
Bobty 19:0367cb46d003 219 }
Bobty 16:89778849e9f7 220 }
Bobty 19:0367cb46d003 221 return validAddresses;
Bobty 8:5980547ae71c 222 }