to github
[svn42.git] / rf433ctl / find_onewire.pde
1 #include <OneWire.h>
2 #include <DallasTemperature.h>
3
4 /* DS18S20 Temperature chip i/o */
5
6 OneWire  ds(8); 
7 DallasTemperature sensors(&ds);
8
9 DeviceAddress insideThermometer = { 0x10, 0xE7, 0x77, 0xD3, 0x01, 0x08, 0x00, 0x3F };
10
11 void printTemperature(DeviceAddress deviceAddress)
12 {
13   float tempC = sensors.getTempC(deviceAddress);
14   Serial.print("Temp C: ");
15   Serial.print(tempC);
16   Serial.print(" Temp F: ");
17   Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
18 }
19
20 void setup(void) {
21   Serial.begin(9600);
22   sensors.begin();
23 }
24
25 void loop(void) {
26   if (millis() < 2000)
27   {
28     return;
29   }
30   
31   printTemperature(insideThermometer);
32   
33   byte i;
34   byte present = 0;
35   byte data[12];
36   byte addr[8];
37   
38   if ( !ds.search(addr)) {
39     Serial.print("\nNo more addresses.\n");
40     ds.reset_search();
41     delay(250);
42     return;
43   }
44   
45   Serial.print("\nR=");
46   for( i = 0; i < 8; i++) {
47     Serial.print(addr[i], HEX);
48     Serial.print(" ");
49   }
50
51   if ( OneWire::crc8( addr, 7) != addr[7]) {
52       Serial.print("  CRC is not valid!\n");
53       return;
54   }
55   
56   if ( addr[0] != 0x10) {
57       Serial.print("\nDevice is not a DS18S20 family device.\n");
58       return;
59   }
60
61   // The DallasTemperature library can do all this work for you!
62
63   ds.reset();
64   ds.select(addr);
65   ds.write(0x44,1);         // start conversion, with parasite power on at the end
66   
67   delay(1000);     // maybe 750ms is enough, maybe not
68   // we might do a ds.depower() here, but the reset will take care of it.
69   
70   present = ds.reset();
71   ds.select(addr);    
72   ds.write(0xBE);         // Read Scratchpad
73
74   Serial.print("\nP=");
75   Serial.print(present,HEX);
76   Serial.print(" ");
77   for ( i = 0; i < 9; i++) {           // we need 9 bytes
78     data[i] = ds.read();
79     Serial.print(data[i], HEX);
80     Serial.print(" ");
81   }
82   Serial.println();
83   Serial.print(" CRC=");
84   Serial.print( OneWire::crc8( data, 8), HEX);
85   Serial.println();
86 }