// After running the simulator, click on the DS18B20 chip to change the temperature
// Chip by bonnyr, source code: https://github.com/bonnyr/wokwi-ds1820-custom-chip/
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(10);
DallasTemperature sensor(&oneWire);
const int RELAY_PIN = 3;
float tempCelsius; // temperature in Celsius
float tempFahrenheit; // temperature in Fahrenheit
void setup(void) {
Serial.begin(115200);
delay(2);
sensor.begin();
delay(20);
pinMode(RELAY_PIN, OUTPUT);
}
void loop(void) {
// sensor.requestTemperatures();
// Serial.print("Temperature is: ");
// delay(10);
// Serial.println(sensor.getTempCByIndex(0));
// delay(1000);
sensor.requestTemperatures(); // send the command to get temperatures
tempCelsius = sensor.getTempCByIndex(0); // read temperature in Celsius
tempFahrenheit = tempCelsius * 9 / 5 + 32; // convert Celsius to Fahrenheit
Serial.print("Temperature: ");
Serial.print(tempCelsius); // print the temperature in Celsius
Serial.print("°C");
Serial.print(" ~ "); // separator between Celsius and Fahrenheit
Serial.print(tempFahrenheit); // print the temperature in Fahrenheit
Serial.println("°F");
if (tempFahrenheit >= 100) {
Serial.println("The button is being pressed");
digitalWrite(RELAY_PIN, HIGH); // turn on
}
else
if (tempFahrenheit <100) {
Serial.println("The button is unpressed");
digitalWrite(RELAY_PIN, LOW); // turn off
}
}