#include <Wire.h>//I2C
#include <EEPROM.h>
#define relay 7
#define buzzer 8
//Display OLED
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // Largura do display
#define SCREEN_HEIGHT 64 // Altura do display
#define SCREEN_ADDRESS 0x3C // Endereço do display (tente trocar por 0x3D se não funcionar)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
static const unsigned char PROGMEM image_data_WIETECH[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xfd, 0xfe, 0x7f, 0x7b, 0xe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ff, 0xff, 0x87, 0xc3, 0xfb, 0xef, 0xff, 0x9f, 0xcf, 0xfc, 0xfd, 0xcf, 0xe7, 0xff, 0xff,
0xff, 0xff, 0xff, 0xc7, 0xe3, 0xfb, 0xe7, 0xff, 0x9f, 0xcf, 0xfc, 0x31, 0xcf, 0xe7, 0xff, 0xff,
0xff, 0xff, 0xff, 0xc7, 0xe3, 0xfb, 0xe0, 0x1f, 0x9f, 0xc0, 0x3f, 0x03, 0xcf, 0xe7, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
//Fim display OLED
//inicio do encoder
//Pinos de ligacao do encoder
#define ENCODER_CLK 2
#define ENCODER_DT 3
#define ENCODER_SW 4
int counter = 0;
int cont1=0, cont2=0;
//fim encoder
//Memorias de controle da EEPROM
int HUMI, TEMP;
//Endereços para EEPROM
int end1 = 0;
int end2 = 1;
//inicio SHT41
//#include "Adafruit_SHT4x.h"
//Adafruit_SHT4x sht4 = Adafruit_SHT4x();
//fim SHT41
//variaveis de tela
int tela = 0 ;
int tempo = 0;
int stand =0;
void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(9600);
//Sensor de temperatura
//sht4.setPrecision(SHT4X_HIGH_PRECISION);
Serial.println("High precision");
// sht4.setHeater(SHT4X_NO_HEATER);
Serial.println("No heater");
//saidas
pinMode(relay, OUTPUT);
pinMode(buzzer, OUTPUT);
//encoder
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING); //maneira facil de apenas ler uma vez
// Inicializa o display
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
{
Serial.println(F("Erro na inicialização!"));
while(1);
}
// Configura fonte do texto
display.setTextSize(1); // Tamanho
display.setTextColor(1); // Cor
display.clearDisplay();
display.setCursor(5, 10);
display.drawBitmap(0, 0, image_data_WIETECH, 128, 64, 1);
display.display();
tela=0;
}
void readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
counter++; // Clockwise
}
if (dtValue == LOW) {
counter--; // Counterclockwise
}
}
int getCounter() {
int result = 0;
noInterrupts();
result = counter;
interrupts();
return result;
}
void loop() {
// put your main code here, to run repeatedly:
// sensors_event_t humidity, temp;
// uint32_t timestamp = millis();
// sht4.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
// timestamp = millis() - timestamp;
Serial.println(tela);
if(!digitalRead(ENCODER_SW))
{
tempo++;
delay(100);
Serial.println(tempo);
}
if(tempo>10 && digitalRead(ENCODER_SW))
{
tela++;
tempo=0;
tone(8, 500, 250);
}
if(tempo<10 && digitalRead(ENCODER_SW))
{
// if(tela>0)
// {
// tela--;
// }
tempo=0;
}
if((tela) == 0)
{
display.clearDisplay();
display.drawBitmap(0, 0, image_data_WIETECH, 128, 64, 1);
display.display();
}
if ((tela) == 1 )
{
display.clearDisplay();
display.drawLine(64, 0, 64, 64, WHITE);
//metade da temperatura
cont2 = EEPROM.read(end1);
display.setCursor(0, 5);
display.print("Temp atual");
display.setCursor(0, 15);
display.print(TEMP);
display.print(" C");
display.setCursor(0, 25);
display.print("Temp seted");
display.setCursor(0, 35);
if((cont2)==255)
{
display.print("?");
}
else{
display.print(cont2);
}
display.print(" C");
//metade da humidade
display.setCursor(67, 5);
display.print("Humi atual:");
display.setCursor(67, 15);
display.print("A");
display.print("% rH");
display.display();
}
if ((tela)==2)
{
display.clearDisplay();
display.drawLine(64, 0, 64, 64, WHITE);
//metade da temperatura
cont1=getCounter();
display.setCursor(0, 5);
display.print("Temp SET");
display.setCursor(0, 15);
display.print(cont1);
display.print(" C");
//metade da humidade
display.setCursor(67, 5);
display.print("Humi SET:");
display.setCursor(67, 15);
display.print(cont2);
display.print("% rH");
display.display();
}
if ((tela)==3)
{
EEPROM.write(end1, cont1);
tela =1;
}
if(tela!=0)
{
delay(10);
stand++;
Serial.println(1000-stand + String("Time to sleep"));
if(tela==2)
{
stand=0;
}
if(stand>=1000)
{
Serial.println("sleeping");
tela=0;
}
}
if((tempo)!=cont2)
{
digitalWrite(relay, HIGH);
}
}