#include <OneWire.h>
#include <DallasTemperature.h>
#include <EEPROM.h>
// EEPROM addresses for configuration
#define EEPROM_SIZE 3 // 3 bytes for storing mode, low, and high thresholds
#define T1_MODE_ADDR 0 // Address for mode
#define T1_LOW_ADDR 1 // Address for lower threshold
#define T1_HIGH_ADDR 2 // Address for upper threshold
// DS18B20 sensor setup
#define SENSOR_PIN 12 // DS18B20 data pin
OneWire oneWire(SENSOR_PIN);
DallasTemperature sensors(&oneWire);
// Output pins
#define RELAY_PIN 14 // Relay control pin
#define BUZZER_PIN 27 // Buzzer control pin
// Stored configuration values
int T1_mode;
int T1_Lvalue;
int T1_Hvalue;
void setup() {
Serial.begin(115200);
// Initialize EEPROM
if (!EEPROM.begin(EEPROM_SIZE)) {
Serial.println("Failed to initialize EEPROM");
return;
}
// Initialize pins
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
// Read stored configuration from EEPROM
T1_mode = EEPROM.read(T1_MODE_ADDR);
T1_Lvalue = EEPROM.read(T1_LOW_ADDR);
T1_Hvalue = EEPROM.read(T1_HIGH_ADDR);
// Initialize DS18B20
sensors.begin();
// Show startup configuration
showStoredValues();
Serial.println("Enter 'T1:mode:low:high' to configure thresholds and mode.");
Serial.println("Enter 'READ' to display the stored configuration.");
}
void loop() {
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n'); // Read input from Serial Monitor
input.trim();
if (input.equalsIgnoreCase("READ")) {
showStoredValues();
} else if (input.startsWith("T1:")) {
parseAndStoreData(input);
} else {
Serial.println("Invalid input. Use 'T1:mode:low:high' or 'READ'.");
}
}
// Request temperature from the sensor
sensors.requestTemperatures();
float currentTemp = sensors.getTempCByIndex(0);
if (currentTemp != DEVICE_DISCONNECTED_C) {
Serial.print("Current Temperature: ");
Serial.print(currentTemp);
Serial.println(" °C");
// Relay and buzzer control based on mode
switch (T1_mode) {
case 1: // Relay ON below low threshold, OFF above high threshold
if (currentTemp <= T1_Lvalue) {
digitalWrite(RELAY_PIN, HIGH);
} else if (currentTemp >= T1_Hvalue) {
digitalWrite(RELAY_PIN, LOW);
}
break;
case 2: // Relay OFF below low threshold, ON above high threshold
if (currentTemp <= T1_Lvalue) {
digitalWrite(RELAY_PIN, LOW);
} else if (currentTemp >= T1_Hvalue) {
digitalWrite(RELAY_PIN, HIGH);
}
break;
case 3: // Buzzer ON above low threshold; Relay ON above high threshold
if (currentTemp >= T1_Lvalue) {
digitalWrite(BUZZER_PIN, HIGH);
} else {
digitalWrite(BUZZER_PIN, LOW);
}
if (currentTemp >= T1_Hvalue) {
digitalWrite(RELAY_PIN, HIGH);
} else {
digitalWrite(RELAY_PIN, LOW);
}
break;
case 4: // Relay and Buzzer ON below respective thresholds
if (currentTemp <= T1_Lvalue) {
digitalWrite(RELAY_PIN, HIGH);
} else {
digitalWrite(RELAY_PIN, LOW);
}
if (currentTemp <= T1_Hvalue) {
digitalWrite(BUZZER_PIN, HIGH);
} else {
digitalWrite(BUZZER_PIN, LOW);
}
break;
default:
Serial.println("Invalid mode!");
}
} else {
Serial.println("Sensor not found!");
}
delay(1000); // Wait before the next reading
}
// Function to parse input and store values in EEPROM
void parseAndStoreData(String input) {
int firstColon = input.indexOf(':');
int secondColon = input.indexOf(':', firstColon + 1);
int thirdColon = input.indexOf(':', secondColon + 1);
if (firstColon != -1 && secondColon != -1 && thirdColon != -1) {
String modeStr = input.substring(firstColon + 1, secondColon);
String lowStr = input.substring(secondColon + 1, thirdColon);
String highStr = input.substring(thirdColon + 1);
int mode = modeStr.toInt();
int low = lowStr.toInt();
int high = highStr.toInt();
if (mode >= 0 && mode <= 255 && low >= 0 && low <= 255 && high >= 0 && high <= 255) {
if (EEPROM.read(T1_MODE_ADDR) != mode) {
EEPROM.write(T1_MODE_ADDR, mode);
T1_mode = mode;
}
if (EEPROM.read(T1_LOW_ADDR) != low) {
EEPROM.write(T1_LOW_ADDR, low);
T1_Lvalue = low;
}
if (EEPROM.read(T1_HIGH_ADDR) != high) {
EEPROM.write(T1_HIGH_ADDR, high);
T1_Hvalue = high;
}
EEPROM.commit();
Serial.println("Configuration saved successfully.");
showStoredValues();
} else {
Serial.println("Values must be between 0 and 255.");
}
} else {
Serial.println("Invalid format! Use 'T1:mode:low:high'.");
}
}
// Function to display stored values
void showStoredValues() {
Serial.println("Stored Configuration:");
Serial.print("Mode: ");
Serial.println(T1_mode);
Serial.print("Lower Threshold: ");
Serial.println(T1_Lvalue);
Serial.print("Upper Threshold: ");
Serial.println(T1_Hvalue);
}