123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define ONE_WIRE_BUS 2
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- DeviceAddress insideThermometer;
- void setup(void)
- {
-
- Serial.begin(9600);
- Serial.println("Dallas Temperature IC Control Library Demo");
-
- Serial.print("Locating devices...");
- sensors.begin();
- Serial.print("Found ");
- Serial.print(sensors.getDeviceCount(), DEC);
- Serial.println(" devices.");
-
- Serial.print("Parasite power is: ");
- if (sensors.isParasitePowerMode()) Serial.println("ON");
- else Serial.println("OFF");
-
-
-
-
-
-
-
-
-
-
-
-
- if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
-
-
-
-
-
-
-
-
-
-
-
-
-
- Serial.print("Device 0 Address: ");
- printAddress(insideThermometer);
- Serial.println();
-
- sensors.setResolution(insideThermometer, 9);
-
- Serial.print("Device 0 Resolution: ");
- Serial.print(sensors.getResolution(insideThermometer), DEC);
- Serial.println();
- }
- void printTemperature(DeviceAddress deviceAddress)
- {
-
-
-
-
-
-
- float tempC = sensors.getTempC(deviceAddress);
- if(tempC == DEVICE_DISCONNECTED_C)
- {
- Serial.println("Error: Could not read temperature data");
- return;
- }
- Serial.print("Temp C: ");
- Serial.print(tempC);
- Serial.print(" Temp F: ");
- Serial.println(DallasTemperature::toFahrenheit(tempC));
- }
- void loop(void)
- {
-
-
- Serial.print("Requesting temperatures...");
- sensors.requestTemperatures();
- Serial.println("DONE");
-
-
- printTemperature(insideThermometer);
- }
- void printAddress(DeviceAddress deviceAddress)
- {
- for (uint8_t i = 0; i < 8; i++)
- {
- if (deviceAddress[i] < 16) Serial.print("0");
- Serial.print(deviceAddress[i], HEX);
- }
- }
|