to github
[svn42.git] / rf433ctl / DallasTemperature / examples / AlarmHandler / AlarmHandler.pde
1 #include <OneWire.h>
2 #include <DallasTemperature.h>
3
4 // Data wire is plugged into port 2 on the Arduino
5 #define ONE_WIRE_BUS 3
6 #define TEMPERATURE_PRECISION 9
7
8 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
9 OneWire oneWire(ONE_WIRE_BUS);
10
11 // Pass our oneWire reference to Dallas Temperature. 
12 DallasTemperature sensors(&oneWire);
13
14 // arrays to hold device addresses
15 DeviceAddress insideThermometer, outsideThermometer;
16
17 // function that will be called when an alarm condition exists during DallasTemperatures::processAlarms();
18 void newAlarmHandler(uint8_t* deviceAddress)
19 {
20   Serial.println("Alarm Handler Start"); 
21   printAlarmInfo(deviceAddress);
22   printTemp(deviceAddress);
23   Serial.println();
24   Serial.println("Alarm Handler Finish");
25 }
26
27 void printCurrentTemp(DeviceAddress deviceAddress)
28 {
29   printAddress(deviceAddress);
30   printTemp(deviceAddress);
31   Serial.println();
32 }
33
34 void printAddress(DeviceAddress deviceAddress)
35 {
36   Serial.print("Address: ");
37   for (uint8_t i = 0; i < 8; i++)
38   {
39     if (deviceAddress[i] < 16) Serial.print("0");
40     Serial.print(deviceAddress[i], HEX);
41   }
42   Serial.print(" ");
43 }
44
45 void printTemp(DeviceAddress deviceAddress)
46 {
47   float tempC = sensors.getTempC(deviceAddress);
48   if (tempC != DEVICE_DISCONNECTED)
49   {
50     Serial.print("Current Temp C: ");
51     Serial.print(tempC);
52   }
53   else Serial.print("DEVICE DISCONNECTED");
54   Serial.print(" ");
55 }
56
57 void printAlarmInfo(DeviceAddress deviceAddress)
58 {
59   char temp;
60   printAddress(deviceAddress);
61   temp = sensors.getHighAlarmTemp(deviceAddress);
62   Serial.print("High Alarm: ");
63   Serial.print(temp, DEC);
64   Serial.print("C");
65   Serial.print(" Low Alarm: ");
66   temp = sensors.getLowAlarmTemp(deviceAddress);
67   Serial.print(temp, DEC);
68   Serial.print("C");
69   Serial.print(" ");
70 }
71
72 void setup(void)
73 {
74   // start serial port
75   Serial.begin(9600);
76   Serial.println("Dallas Temperature IC Control Library Demo");
77
78   // Start up the library
79   sensors.begin();
80   
81   // locate devices on the bus
82   Serial.print("Found ");
83   Serial.print(sensors.getDeviceCount(), DEC);
84   Serial.println(" devices.");
85
86   // search for devices on the bus and assign based on an index
87   if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
88   if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1"); 
89
90   Serial.print("Device insideThermometer ");
91   printAlarmInfo(insideThermometer);
92   Serial.println();
93   
94   Serial.print("Device outsideThermometer ");
95   printAlarmInfo(outsideThermometer);
96   Serial.println();
97   
98   // set alarm ranges
99   Serial.println("Setting alarm temps...");
100   sensors.setHighAlarmTemp(insideThermometer, 26);
101   sensors.setLowAlarmTemp(insideThermometer, 22);
102   sensors.setHighAlarmTemp(outsideThermometer, 25);
103   sensors.setLowAlarmTemp(outsideThermometer, 21);
104   
105   Serial.print("New insideThermometer ");
106   printAlarmInfo(insideThermometer);
107   Serial.println();
108   
109   Serial.print("New outsideThermometer ");
110   printAlarmInfo(outsideThermometer);
111   Serial.println();
112
113   // attach alarm handler
114   sensors.setAlarmHandler(&newAlarmHandler);
115
116 }
117
118 void loop(void)
119
120   // ask the devices to measure the temperature
121   sensors.requestTemperatures();
122   
123   // if an alarm condition exists as a result of the most recent 
124   // requestTemperatures() request, it exists until the next time 
125   // requestTemperatures() is called AND there isn't an alarm condition
126   // on the device
127   if (sensors.hasAlarm())
128   {
129     Serial.println("Oh noes!  There is at least one alarm on the bus.");
130   }
131
132   // call alarm handler function defined by sensors.setAlarmHandler
133   // for each device reporting an alarm
134   sensors.processAlarms();
135
136   if (!sensors.hasAlarm())
137   {
138     // just print out the current temperature
139     printCurrentTemp(insideThermometer);
140     printCurrentTemp(outsideThermometer);
141   }
142   
143   delay(1000);
144 }
145 \r