#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is conntec to the Arduino digital pin 4
#define ONE_WIRE_BUS 4
const int ledPin = 21;
const float thresholdTemp = 30.0;
// 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(void)
{
// Start serial communication for debugging purposes
Serial.begin(9600);
// Start up the library
sensors.begin();
pinMode(ledPin, OUTPUT);
}
void loop(void)
{
// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
float temperature= sensors.getTempCByIndex(0);
Serial.print("Celsius temperature: ");
// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.print(temperature);
Serial.print(" - Fahrenheit temperature: ");
Serial.println(sensors.getTempFByIndex(0));
if (temperature > thresholdTemp)
{
digitalWrite(21, !digitalRead(21));
Serial.print("Alert: Temperature exceeds threshold! Temperature: ");
// Convert the float temperature to a string
String tempStr = String(temperature, 1); // 1 decimal place
// Send the temperature as a string byte by byte
for (int i = 0; i < tempStr.length(); i++)
{
Serial.write(tempStr[i]);
}
}
else
{
digitalWrite(ledPin, LOW);
}
delay(1000);
}