2 #include <DallasTemperature.h>
4 // Data wire is plugged into port 2 on the Arduino
7 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
8 OneWire oneWire(ONE_WIRE_BUS);
10 // Pass our oneWire reference to Dallas Temperature.
11 DallasTemperature sensors(&oneWire);
13 // arrays to hold device addresses
14 DeviceAddress insideThermometer, outsideThermometer;
20 Serial.println("Dallas Temperature IC Control Library Demo");
22 // Start up the library
25 // locate devices on the bus
26 Serial.print("Found ");
27 Serial.print(sensors.getDeviceCount(), DEC);
28 Serial.println(" devices.");
30 // search for devices on the bus and assign based on an index.
31 if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
32 if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
34 // show the addresses we found on the bus
35 Serial.print("Device 0 Address: ");
36 printAddress(insideThermometer);
39 Serial.print("Device 0 Alarms: ");
40 printAlarms(insideThermometer);
43 Serial.print("Device 1 Address: ");
44 printAddress(outsideThermometer);
47 Serial.print("Device 1 Alarms: ");
48 printAlarms(outsideThermometer);
51 Serial.println("Setting alarm temps...");
53 // alarm when temp is higher than 30C
54 sensors.setHighAlarmTemp(insideThermometer, 30);
56 // alarm when temp is lower than -10C
57 sensors.setLowAlarmTemp(insideThermometer, -10);
59 // alarm when temp is higher than 31C
60 sensors.setHighAlarmTemp(outsideThermometer, 31);
62 // alarn when temp is lower than 27C
63 sensors.setLowAlarmTemp(outsideThermometer, 27);
65 Serial.print("New Device 0 Alarms: ");
66 printAlarms(insideThermometer);
69 Serial.print("New Device 1 Alarms: ");
70 printAlarms(outsideThermometer);
74 // function to print a device address
75 void printAddress(DeviceAddress deviceAddress)
77 for (uint8_t i = 0; i < 8; i++)
79 if (deviceAddress[i] < 16) Serial.print("0");
80 Serial.print(deviceAddress[i], HEX);
84 // function to print the temperature for a device
85 void printTemperature(DeviceAddress deviceAddress)
87 float tempC = sensors.getTempC(deviceAddress);
88 Serial.print("Temp C: ");
90 Serial.print(" Temp F: ");
91 Serial.print(DallasTemperature::toFahrenheit(tempC));
94 void printAlarms(uint8_t deviceAddress[])
97 temp = sensors.getHighAlarmTemp(deviceAddress);
98 Serial.print("High Alarm: ");
99 Serial.print(temp, DEC);
101 Serial.print(DallasTemperature::toFahrenheit(temp));
102 Serial.print("F | Low Alarm: ");
103 temp = sensors.getLowAlarmTemp(deviceAddress);
104 Serial.print(temp, DEC);
106 Serial.print(DallasTemperature::toFahrenheit(temp));
110 // main function to print information about a device
111 void printData(DeviceAddress deviceAddress)
113 Serial.print("Device Address: ");
114 printAddress(deviceAddress);
116 printTemperature(deviceAddress);
120 void checkAlarm(DeviceAddress deviceAddress)
122 if (sensors.hasAlarm(deviceAddress))
124 Serial.print("ALARM: ");
125 printData(deviceAddress);
131 // call sensors.requestTemperatures() to issue a global temperature
132 // request to all devices on the bus
133 Serial.print("Requesting temperatures...");
134 sensors.requestTemperatures();
135 Serial.println("DONE");
138 // check each address individually for an alarm condition
139 checkAlarm(insideThermometer);
140 checkAlarm(outsideThermometer);
143 // Search the bus and iterate through addresses of devices with alarms
145 // space for the alarm device's address
146 DeviceAddress alarmAddr;
148 Serial.println("Searching for alarms...");
150 // resetAlarmSearch() must be called before calling alarmSearch()
151 sensors.resetAlarmSearch();
153 // alarmSearch() returns 0 when there are no devices with alarms
154 while (sensors.alarmSearch(alarmAddr))
156 Serial.print("ALARM: ");
157 printData(alarmAddr);