#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define ntc_pin 32
#define led_aquecedor 18
#define led_refrigerador 19
char input = 0;
String entrada;
int temp_alvo;
int delta_temp_alvo;
float temp_ssd;
const float BETA = 3950; // Deve corresponder ao coeficiente beta do termistor
int leitura_ntc;
void setup() {
pinMode(ntc_pin, OUTPUT);
pinMode(led_aquecedor, OUTPUT);
pinMode(led_refrigerador, OUTPUT);
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(2000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 2); // set position to display (x,y)
}
void loop() {
if (Serial.available() > 0) {
input = Serial.read();
if (input != '\n') {
if (input == 'T' ) {
while (input != '\n') {
input = Serial.read();
entrada += input;
if (input == '\n') {
temp_alvo = entrada.toInt();
entrada = "0";
}
}
Serial.print("A temperatura alvo é: ");
Serial.println(temp_alvo);
}
if (input == 't' ) {
while (input != '\n') {
input = Serial.read();
entrada += input;
if (input == '\n') {
delta_temp_alvo = entrada.toInt();
entrada = "0";
}
}
Serial.print("A temperatura pode variar em até: ");
Serial.println(delta_temp_alvo);
}
}
}
if (temp_alvo && delta_temp_alvo != 0) {
leitura_ntc = analogRead(ntc_pin);
float temp_celsius = 1 / (log(1 / (4095. / leitura_ntc - 1)) / BETA + 1.0 / 298.15) - 273.15;
temp_ssd = temp_celsius;
oled.setCursor(0, 2);
oled.print("temperatura atual: "); // set text
oled.display(); // display on OLED
oled.println(temp_ssd); // set text
oled.display(); // display on OLED
delay(1000);
oled.clearDisplay(); // clear display
if (temp_celsius < (temp_alvo - delta_temp_alvo)) {//Muito frio
digitalWrite(led_refrigerador, LOW);
digitalWrite(led_aquecedor, HIGH);
Serial.println("Muito frio");
} else if (temp_celsius > (temp_alvo + delta_temp_alvo)) {//Muito quente
digitalWrite(led_aquecedor, LOW);
digitalWrite(led_refrigerador, HIGH);
Serial.println("Muito quente");
} else {
digitalWrite(led_refrigerador, LOW);
digitalWrite(led_aquecedor, LOW);
Serial.println("Temperatura OK");
}
}
}