2 #include <DallasTemperature.h>
4 // Data wire is plugged into port 2 on the Arduino
6 #define TEMPERATURE_PRECISION 9
8 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
9 OneWire oneWire(ONE_WIRE_BUS);
11 // Pass our oneWire reference to Dallas Temperature.
12 DallasTemperature sensors(&oneWire);
14 // arrays to hold device addresses
15 DeviceAddress insideThermometer, outsideThermometer;
17 // function that will be called when an alarm condition exists during DallasTemperatures::processAlarms();
18 void newAlarmHandler(uint8_t* deviceAddress)
20 Serial.println("Alarm Handler Start");
21 printAlarmInfo(deviceAddress);
22 printTemp(deviceAddress);
24 Serial.println("Alarm Handler Finish");
27 void printCurrentTemp(DeviceAddress deviceAddress)
29 printAddress(deviceAddress);
30 printTemp(deviceAddress);
34 void printAddress(DeviceAddress deviceAddress)
36 Serial.print("Address: ");
37 for (uint8_t i = 0; i < 8; i++)
39 if (deviceAddress[i] < 16) Serial.print("0");
40 Serial.print(deviceAddress[i], HEX);
45 void printTemp(DeviceAddress deviceAddress)
47 float tempC = sensors.getTempC(deviceAddress);
48 if (tempC != DEVICE_DISCONNECTED)
50 Serial.print("Current Temp C: ");
53 else Serial.print("DEVICE DISCONNECTED");
57 void printAlarmInfo(DeviceAddress deviceAddress)
60 printAddress(deviceAddress);
61 temp = sensors.getHighAlarmTemp(deviceAddress);
62 Serial.print("High Alarm: ");
63 Serial.print(temp, DEC);
65 Serial.print(" Low Alarm: ");
66 temp = sensors.getLowAlarmTemp(deviceAddress);
67 Serial.print(temp, DEC);
76 Serial.println("Dallas Temperature IC Control Library Demo");
78 // Start up the library
81 // locate devices on the bus
82 Serial.print("Found ");
83 Serial.print(sensors.getDeviceCount(), DEC);
84 Serial.println(" devices.");
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");
90 Serial.print("Device insideThermometer ");
91 printAlarmInfo(insideThermometer);
94 Serial.print("Device outsideThermometer ");
95 printAlarmInfo(outsideThermometer);
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);
105 Serial.print("New insideThermometer ");
106 printAlarmInfo(insideThermometer);
109 Serial.print("New outsideThermometer ");
110 printAlarmInfo(outsideThermometer);
113 // attach alarm handler
114 sensors.setAlarmHandler(&newAlarmHandler);
120 // ask the devices to measure the temperature
121 sensors.requestTemperatures();
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
127 if (sensors.hasAlarm())
129 Serial.println("Oh noes! There is at least one alarm on the bus.");
132 // call alarm handler function defined by sensors.setAlarmHandler
133 // for each device reporting an alarm
134 sensors.processAlarms();
136 if (!sensors.hasAlarm())
138 // just print out the current temperature
139 printCurrentTemp(insideThermometer);
140 printCurrentTemp(outsideThermometer);