#include <Adafruit_SSD1306.h>
#include <Wire.h>
/////////////// OLED DISPLAY ///////////////////
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//////////// Temperatura sensor //////////////
const int temperaturePin = A1;
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
float celsius = 0; // temp convertida en celsius
float minTemp = 50.0; // Min Temp para ON
float maxTemp = 70.0; // Max Temp para OFF
////////////// RELAYS ///////////////////
const int relayPin = 0; // Pin for relay
//////////// SETUP & LOOP /////////////////
void setup() {
Serial.begin(115200);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure the relay is initially off
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Your existing setup code here...
// Display thermostat icon and set temperature
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Smart Zapa");
display.setCursor(0, 24);
display.println("Temp Ctrl");
display.setCursor(0, 48);
display.println("Iniciado!");
display.display();
display.setTextSize(1);
delay(2000); // Display for 2 seconds
}
void loop() {
// read temperature from the sensor
int sensorValue = analogRead(temperaturePin);
Serial.print("Valor medido: ");
Serial.println(sensorValue);
float celsius = 1 / (log(1 / (4095. / sensorValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" ℃");
// Control térmico con relé en modo bang-bang
if (celsius > maxTemp) {
digitalWrite(relayPin, LOW); //off
} else if (celsius < minTemp) {
digitalWrite(relayPin, HIGH); //on
}
// Mostrar temperatura medida, configuración y estado del relé en pantalla
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Temperatura seteada: ");
display.setCursor(0, 12);
display.println("Min: " + String(minTemp) + " C");
display.setCursor(0, 24);
display.println("Max: " + String(maxTemp) + " C" );
display.setCursor(0, 36);
display.print("Actual: " + String(celsius) + " C");
// Mostrar el estado del relé
display.setCursor(0, 48);
display.setTextSize(2);
display.print("out: ");
display.print(digitalRead(relayPin) == HIGH ? "ON" : "OFF");
display.display();
delay(100);
// Check for serial commands
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n');
if (command.startsWith("MIN_TEMP")) {
minTemp = command.substring(8).toFloat();
Serial.println("Temperatura Minima establecida en: " + String(minTemp) + " C");
} else if (command.startsWith("MAX_TEMP")) {
maxTemp = command.substring(8).toFloat();
Serial.println("Temperatura maxima establecida en: " + String(maxTemp) + " C");
} else {
Serial.println("Comando no reconocido");
Serial.println("Use MIN_TEMP o MAX_TEMP para asignar nuevos valores de temperatura.");
Serial.println("Ejemplo: MIN_TEMP 20.0");
Serial.println("Establecerá la temperatura minima en la cual activa el realy en 20.0 grados Celsius.");
Serial.println("Ejemplo: MAX_TEMP 25.0");
Serial.println("Establecerá la temperatura maxima para apagado en 25.0 grados Celsius.");
}
}
}