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 Sep 28 10:33:14 2015 +0000
Revision:
16:89778849e9f7
Parent:
13:9ec0e11cf3c1
Child:
19:0367cb46d003
Turned on debugging messages on thermometers; Updated web callbacks method signatures

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 8:5980547ae71c 136 _numValidAddresses = 0;
Bobty 8:5980547ae71c 137 for (int addrIdx = 0; addrIdx < MAX_BUS_DEVICES; addrIdx++)
Bobty 8:5980547ae71c 138 {
Bobty 8:5980547ae71c 139 uint8_t addr[8];
Bobty 8:5980547ae71c 140
Bobty 8:5980547ae71c 141 if ( !_oneWire.search(addr))
Bobty 8:5980547ae71c 142 {
Bobty 8:5980547ae71c 143 #ifdef SHOW_18B20_DEBUGGING
Bobty 8:5980547ae71c 144 printf("No more addresses.\r\n");
Bobty 8:5980547ae71c 145 #endif
Bobty 8:5980547ae71c 146 _oneWire.reset_search();
Bobty 8:5980547ae71c 147 break;
Bobty 8:5980547ae71c 148 }
Bobty 8:5980547ae71c 149
Bobty 8:5980547ae71c 150 #ifdef SHOW_18B20_DEBUGGING
Bobty 8:5980547ae71c 151 printf("Found device addr (ROM) =");
Bobty 8:5980547ae71c 152 #endif
Bobty 9:0e103c2f869a 153 for( int i = 0; i < ONEWIRE_ADDR_BYTES; i++)
Bobty 8:5980547ae71c 154 {
Bobty 8:5980547ae71c 155 // Copy to table
Bobty 8:5980547ae71c 156 _addrTable[_numValidAddresses][i] = addr[i];
Bobty 8:5980547ae71c 157 #ifdef SHOW_18B20_DEBUGGING
Bobty 8:5980547ae71c 158 printf(" %02x", addr[i]);
Bobty 8:5980547ae71c 159 #endif
Bobty 8:5980547ae71c 160 }
Bobty 8:5980547ae71c 161
Bobty 8:5980547ae71c 162 // Check CRC - only include if CRC is valid
Bobty 8:5980547ae71c 163 if (_oneWire.CRC(addr, ONEWIRE_ADDR_BYTES-1) == addr[ONEWIRE_ADDR_BYTES-1])
Bobty 8:5980547ae71c 164 _numValidAddresses++;
Bobty 8:5980547ae71c 165 #ifdef SHOW_18B20_DEBUGGING
Bobty 8:5980547ae71c 166 else
Bobty 8:5980547ae71c 167 printf(" (CRC INVALID!)");
Bobty 8:5980547ae71c 168 #endif
Bobty 8:5980547ae71c 169
Bobty 8:5980547ae71c 170 #ifdef SHOW_18B20_DEBUGGING
Bobty 8:5980547ae71c 171 // the first ROM byte indicates which chip
Bobty 8:5980547ae71c 172 switch (addr[0])
Bobty 8:5980547ae71c 173 {
Bobty 8:5980547ae71c 174 case 0x10:
Bobty 8:5980547ae71c 175 printf(" Chip = DS18S20\r\n"); // or old DS1820
Bobty 8:5980547ae71c 176 break;
Bobty 8:5980547ae71c 177 case 0x28:
Bobty 8:5980547ae71c 178 printf(" Chip = DS18B20\r\n");
Bobty 8:5980547ae71c 179 break;
Bobty 8:5980547ae71c 180 case 0x22:
Bobty 8:5980547ae71c 181 printf(" Chip = DS1822\r\n");
Bobty 8:5980547ae71c 182 break;
Bobty 8:5980547ae71c 183 default:
Bobty 8:5980547ae71c 184 printf(" NOT DS18x20 FAMILY\r\n");
Bobty 8:5980547ae71c 185 break;
Bobty 8:5980547ae71c 186 }
Bobty 8:5980547ae71c 187 #endif
Bobty 16:89778849e9f7 188 }
Bobty 16:89778849e9f7 189 return _numValidAddresses;
Bobty 8:5980547ae71c 190 }