// Definição de Bibliotecas
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Definição da resolução do OLED display
#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);
// Definição dos pinos do ESP32 utilizados
const int botao_func = 14; // Pino de ligação do botão de posição
int b_aux = 0;// Variavel aux para guardar o estado do botão
const int rele = 26; // Pino de ligação do Relé
const int RGB_Red = 4; // Pino de controlo da componente RED do LED RGB
const int RGB_Green = 5; // Pino de controlo da componente Green do LED RGB
const int RGB_Blue = 18; // Pino de controlo da componente Blue do LED RGB
const int Sensor_Temp = 2; // Pino de controlo do sensor de temp
int S_T_aux = 0; //Variável aux para guardar o valor do sensor de temp
const float BETA = 5000; // Deve corresponder ao coeficiente beta do termistor
float voltage = 0;
float celsius = 0;
void setup() {;
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// Definição do funcionamneto para os pinos INPUT/OUTP
pinMode(botao_func, INPUT);
pinMode(rele, OUTPUT);
pinMode(RGB_Red, OUTPUT);
pinMode(RGB_Green, OUTPUT);
pinMode(RGB_Blue, OUTPUT);
pinMode(Sensor_Temp, INPUT);
// OLED display initialization ////////////////////////////////////
// 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);
////////////////////////////////////////////////////////////
oled.clearDisplay(); // clear display
oled.setTextSize(2); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(20, 2); // set position to display (x,y)
oled.println("Mec2225"); // set text
oled.display(); // display on OLED
delay(2000); // wait two seconds for initializing
}
void loop()
{
// put your main code here, to run repeatedly
b_aux = digitalRead(botao_func);
//Se liguei o botao_func
Serial.println(b_aux);
if(b_aux == HIGH)
{
// Ler temperatura
S_T_aux = analogRead(Sensor_Temp);
//////////////////////////////////////////////
voltage = S_T_aux/65535*3.300; //get reading in voltage
celsius = (1 / (log(1 / (65535.00 / S_T_aux - 1)) / BETA + 1.0 / 298.15) - 273.15);
//////////////////////////////////////////////
oled.clearDisplay(); // clear display
oled.setTextSize(2); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(20, 2); // set position to display (x,y)
oled.print("Sist ON ");
oled.setCursor(20, 30); // set position to display (x,y)
oled.print("S: ");
oled.println(S_T_aux); // set text
oled.display();
// Ativar LED RGB com cor verde
Serial.println("Verde");
digitalWrite(RGB_Red, LOW);
digitalWrite(RGB_Green, HIGH);
digitalWrite(RGB_Blue, LOW);
//analogWrite(RGB_Red, 255);
// analogWrite(RGB_Green, 100);
//analogWrite(RGB_Blue, 0);
if(S_T_aux > 2273){
digitalWrite(rele, LOW);
Serial.println("Rele ON");
}
else{
digitalWrite(rele, HIGH);
Serial.println("Rele OFF");
}
}
else
{
//////////////////////////////////////////////
oled.clearDisplay(); // clear display
oled.setTextSize(2); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(20, 2); // set position to display (x,y)
oled.print("Sist OFF ");
oled.setCursor(20, 30); // set position to display (x,y)
oled.print("S: ");
oled.println(S_T_aux); // set text
oled.display();
// Ativar LED RGB com cor verde
Serial.println("Vermelho");
digitalWrite(RGB_Red, HIGH);
digitalWrite(RGB_Green, LOW);
digitalWrite(RGB_Blue, LOW);
}
delay(10); // this speeds up the simulation
}