#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define UI unsigned int
#define DHT_TYPE DHT22
#define DHT_PIN 5
#define LED_PIN 4
#define FPS 5
UI now = 0, prev = 0, last_update = 0, last_press = 0;
bool celsius = true;
int records = 0;
char dado;
float temp, humd, recs[4];
byte grau[8] = {0b00110, 0b01001, 0b01001, 0b00110,
0b00000, 0b00000, 0b00000, 0b00000
};
byte setaBaixo[8] = {0b00000, 0b00100, 0b00100, 0b00100,
0b10101, 0b01110, 0b00100, 0b00000
};
byte setaCima[8] = {0b00000, 0b00100, 0b01110, 0b10101,
0b00100, 0b00100, 0b00100, 0b00000
};
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
Serial.begin(9600);
// SerialBT.begin("UFFS-CC_ESP32");
// Serial.println("THE BLUETOOTH DEVICE IS READY TO PAIR");
pinMode(LED_PIN, OUTPUT);
lcd.init();
lcd.backlight();
lcd.createChar(0, grau);
lcd.createChar(1, setaBaixo);
lcd.createChar(2, setaCima);
dht.begin();
initialize();
}
void initialize() {
temp = recs[0] = recs[1] = dht.readTemperature();
humd = recs[2] = recs[3] = dht.readHumidity();
}
void loop() {
now = millis();
leBluetooth();
updateTemp();
printLCD();
delay(20);
}
void leBluetooth() {
if (Serial.available()) {
dado = Serial.read();
// if (SerialBT.available()) {
// dado = (SerialBT.read());
Serial.write(dado);
escreveBluetooth();
}
}
void escreveBluetooth() {
if (dado == 'L') {
digitalWrite(LED_PIN, HIGH);
// SerialBT.print("Led ON via APP");
Serial.println("Led ON via APP");
// SerialBT.print("*LR255G255B255*");
} else if (dado == 'D') {
digitalWrite(LED_PIN, LOW);
// SerialBT.print("Led OFF via APP");
Serial.println("Led OFF via APP");
// SerialBT.print("*LR0G0B0*");
} else if (dado == 'C') {
celsius = !celsius;
String c = celsius ? "Celsius" : "Fahrenheit";
String comp = "Temperatura em " + c + " no LCD";
// SerialBT.print(comp);
Serial.println(comp);
} else if (dado == 'R') {
records = (records + 1 > 2) ? 0 : records + 1;
String r = "atuais";
if (records) r = (records == 1) ? "mínimos" : "máximos";
String comp = "Valores " + r + " no LCD";
// SerialBT.print(comp);
Serial.println(comp);
}
}
void updateTemp() {
if (now - last_update < 1000) return; // DHT update rate 1s?
temp = dht.readTemperature();
humd = dht.readHumidity();
// SerialBT.print("*G"+String(temp)+","+String(humd)+"*");
Serial.println("*G"+String(temp)+","+String(humd)+"*");
checkChanges();
last_update = now;
}
void checkChanges() {
if (temp < recs[0]) recs[0] = temp;
if (temp > recs[1]) recs[1] = temp;
if (humd < recs[2]) recs[2] = humd;
if (humd > recs[3]) recs[3] = humd;
}
void printLCD() {
if (now - prev < (1000 / FPS)) return; // FPS
float t = temp;
if (records) t = (records == 1) ? recs[0] : recs[1];
float h = humd;
if (records) h = (records == 1) ? recs[2] : recs[3];
lcd.clear();
lcd.setCursor(0, 0);
if (records == 1) lcd.print("\x01");
if (records == 2) lcd.print("\x02");
if (celsius) lcd.print("Temp.: " + String(t, 1) + "\x08" + "C");
else lcd.print("Temp.: " + String((int)(t * 9 / 5) + 32) + "F");
lcd.setCursor(0, 1);
if (records == 1) lcd.print("\x01");
if (records == 2) lcd.print("\x02");
lcd.print("Umid.: " + String(h, 1) + " %");
prev = now;
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4