#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is connected to ESP15
#define ONE_WIRE_BUS 15
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void setup() {
// Start serial communication for debugging
Serial.begin(9600);
// Start up the library
sensors.begin();
}
void loop() {
// Request temperature measurement
sensors.requestTemperatures();
// Fetch temperature in Celsius
float temperatureC = sensors.getTempCByIndex(0);
// Check if the reading is valid
if (temperatureC != DEVICE_DISCONNECTED_C) {
// Print the temperature to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
} else {
Serial.println("Error: Could not read temperature data");
}
// Wait 1 second before repeating
delay(1000);
}