Attachment 'GetTemp.c'

Download

   1 // file: GetTemp.c  2015-12-05 
   2 // get temperature from DS16B20 sensor
   3 
   4 /* needs the following definitions in main program:
   5 #include <OneWire.h>
   6 OneWire  ds(2);  // on pin 2 (a 4.7K resistor is necessary)
   7 
   8 byte b_idx;
   9 byte b_present = 0;
  10 byte b_type_s;
  11 byte b_data[12];
  12 byte b_addr[8];
  13 float celsius;
  14 String strCelsius;
  15 */
  16 
  17 void initSensors() {
  18   if ( !ds.search(b_addr)) {
  19     Serial.println("No more addresses.");
  20     Serial.println();
  21     ds.reset_search();
  22     while (0 == 1) {  // signal NO sensor connected
  23       digitalWrite(BUILTIN_LED2, LOW);
  24       delay(300); // ms
  25       digitalWrite(BUILTIN_LED2, HIGH);
  26       delay(300); // ms       
  27     }
  28   } else {  // init temperature sensors
  29     Serial.print("ROM =");
  30     for( b_idx = 0; b_idx < 8; b_idx++) {
  31       Serial.write(' ');
  32       Serial.print(b_addr[b_idx], HEX);
  33     }
  34     if (OneWire::crc8(b_addr, 7) != b_addr[7]) {
  35       Serial.println("CRC is not valid!");
  36       while (0 == 1) {  // signal bad wiring
  37         digitalWrite(BUILTIN_LED2, LOW);
  38         delay(300); // ms
  39         digitalWrite(BUILTIN_LED2, HIGH);
  40         delay(300); // ms       
  41       }          
  42     } 
  43     Serial.println();
  44   // the first ROM byte indicates which chip
  45   switch (b_addr[0]) {
  46     case 0x10:
  47       Serial.println("  Chip = DS18S20");  // or old DS1820
  48       b_type_s = 1;
  49       break;
  50     case 0x28:
  51       Serial.println("  Chip = DS18B20");
  52       b_type_s = 0;
  53       break;
  54     case 0x22:
  55       Serial.println("  Chip = DS1822");
  56       b_type_s = 0;
  57       break;
  58     default:
  59       Serial.println("Device is not a DS18x20 family device.");
  60       while (0 == 1) {  // signal bad wiring
  61         digitalWrite(BUILTIN_LED2, LOW);
  62         delay(300); // ms
  63         digitalWrite(BUILTIN_LED2, HIGH);
  64         delay(300); // ms       
  65       }
  66     }  
  67   } 
  68 } // initSensors()
  69 
  70 void readTemp() {
  71   b_present = ds.reset();
  72   ds.select(b_addr);    
  73   ds.write(0xBE);         // Read Scratchpad
  74   if (debug) {
  75     Serial.print("  Data = ");
  76     Serial.print(b_present, HEX);
  77     Serial.print(" ");
  78   }
  79   for ( b_idx = 0; b_idx < 9; b_idx++) {           // we need 9 bytes
  80     b_data[b_idx] = ds.read();
  81     if (debug) {
  82       Serial.print(b_data[b_idx], HEX);
  83       Serial.print(" ");
  84     }
  85   }
  86   if (debug) {
  87     Serial.print(" CRC=");
  88     Serial.print(OneWire::crc8(b_data, 8), HEX);
  89     Serial.println();
  90   }
  91 
  92   // Convert the data to actual temperature
  93   // because the result is a 16 bit signed integer, it should
  94   // be stored to an "int16_t" type, which is always 16 bits
  95   // even when compiled on a 32 bit processor.
  96   int16_t raw = (b_data[1] << 8) | b_data[0];
  97   if (b_type_s) {
  98     raw = raw << 3; // 9 bit resolution default
  99     if (b_data[7] == 0x10) {
 100       // "count remain" gives full 12 bit resolution
 101       raw = (raw & 0xFFF0) + 12 - b_data[6];
 102     }
 103   } else {
 104     byte cfg = (b_data[4] & 0x60);
 105     // at lower res, the low bits are undefined, so let's zero them
 106     if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
 107     else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
 108     else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
 109     //// default is 12 bit resolution, 750 ms conversion time
 110   }
 111   celsius = (float)raw / 16.0;
 112   if (debug) {
 113     Serial.print("  Temperature = ");
 114     Serial.print(celsius);
 115     Serial.print(" Celsius, ");
 116     Serial.println(); 
 117   } 
 118 }  // readTemp()
 119 

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2015-12-20 15:34:00, 3.3 KB) [[attachment:GetTemp.c]]
  • [get | view] (2015-12-18 19:00:00, 79.9 KB) [[attachment:Thingspeak_WarmWasser.png]]
  • [get | view] (2015-12-21 15:53:00, 11.9 KB) [[attachment:WiFiLoggerOneWire.c]]
  • [get | view] (2015-12-20 15:35:00, 1.0 KB) [[attachment:WiFiLoggerOneWire.h]]
  • [get | view] (2015-12-15 21:02:00, 77.5 KB) [[attachment:WiFiLoggerOneWireVZ.png]]
  • [get | view] (2015-12-20 15:05:00, 103.1 KB) [[attachment:WiFiLoggerOneWire_config.png]]
  • [get | view] (2015-12-18 19:51:00, 89.0 KB) [[attachment:WiFiLogger_DSC06322.jpg]]
  • [get | view] (2016-05-08 10:40:00, 36.5 KB) [[attachment:WiFiLogger_screenshot.png]]
  • [get | view] (2016-06-20 05:37:00, 35.1 KB) [[attachment:WiFiLogger_screenshot2.png]]
  • [get | view] (2015-12-15 14:38:00, 100.5 KB) [[attachment:WiFi_Logger_DSC06319.jpg]]
  • [get | view] (2015-12-20 15:33:00, 0.7 KB) [[attachment:credentials.c]]
  • [get | view] (2015-12-20 15:33:00, 3.9 KB) [[attachment:getNTPtime.c]]
  • [get | view] (2015-12-20 15:34:00, 5.4 KB) [[attachment:handleHTTP.c]]
  • [get | view] (2015-12-20 15:35:00, 0.4 KB) [[attachment:tools.c]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.