// C++ code
/*
bt electronics:
btn_esq:
"f": fahrenheit
"c": celsius
btn_mid:
"M": min max | temp & humid
"N": atual
btn_dir:
"L": liga
"l": desliga
*/
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include "BluetoothSerial.h"
#define DHTTYPE DHT11 // Sensor DHT11
#define DHTPIN 5 // Digital pin connected to the DHT sensor
#define LED_PIN 4
//Verifica se o bluetooth esta habilitado corretamente
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
LiquidCrystal_I2C lcd(0x27,16,2); // pinos 21 22
byte dcircle[] = {
B00110,
B01001,
B01001,
B00110,
B00000,
B00000,
B00000,
B00000
};
DHT dht(DHTPIN, DHTTYPE);
float h, t;
float max_t, max_h, min_t, min_h;
bool STATE = true;
bool CELSIUS = true;
bool button_state, last_button_state;
int delay_show_minmax_led = 5000;
long last_change = millis();
char dado;
float last_t;
bool first = true;
//Cria uma instância de BluetoothSerial chamado SerialBT
BluetoothSerial SerialBT;
void setup()
{
Serial.begin(19200);
SerialBT.begin("LUIZPGT");
lcd.init();
lcd.clear();
lcd.backlight();
lcd.createChar(0, dcircle);
dht.begin();
pinMode(LED_PIN, OUTPUT);
h = dht.readHumidity();
t = dht.readTemperature(); // Celsius (the default)
max_t = t;
min_t = t;
max_h = h;
min_h = h;
last_t = 0;
}
float ctof(float c) { return (c * 1.8) + 32; }
float ftoc(float f) { return (f - 32) * 0.555555; }
bool bt_led_on = false;
void loop() {
h = dht.readHumidity();
t = dht.readTemperature(); // Celsius (the default)
if (!CELSIUS) {
t = ctof(t);
}
if (t > max_t) { max_t = t; last_change = millis(); }
if (t < min_t) { min_t = t; last_change = millis(); }
if (h > max_h) { max_h = h; last_change = millis(); }
if (h < min_h) { min_h = h; last_change = millis(); }
if (STATE)
print_cur_status();
else
print_hist_status();
bt_graph_cur_status();
last_button_state = button_state;
if (millis() - last_change < 5000 || bt_led_on) {
digitalWrite(LED_PIN, HIGH);
SerialBT.print("*LR255G255B255*");
}
if (digitalRead(LED_PIN) && (millis() - last_change > 5000) && bt_led_on == false) {
digitalWrite(LED_PIN, LOW);
SerialBT.print("*LR0G0B0*");
}
leBluetooth();
delay(500);
}
void print_cur_status() {
lcd_print_cur_status();
bt_print_cur_status();
}
void print_hist_status() {
lcd_print_hist_status();
bt_print_hist_status();
}
void bt_print_cur_status() {
String sh = String(h);
String st = String(t);
SerialBT.print("*tHumi: " + sh + " %\n*");
if (CELSIUS)
SerialBT.print("*tTemp: " + st + " oC\n*");
else
SerialBT.print("*tTemp: " + st + " oF\n*");
SerialBT.print("*t\n*");
SerialBT.print("*t\n*");
SerialBT.print("*t\n*");
SerialBT.print("*t\n*");
SerialBT.print("*t\n*");
SerialBT.print("*t\n*");
SerialBT.print("*t\n*");
SerialBT.print("*t\n*");
}
void bt_graph_cur_status() {
String st = String(t);
if (!CELSIUS) { st = String(ftoc(t)); }
SerialBT.print("*T"+ st +"*");
SerialBT.print("*H"+ String(h) +"*");
}
void bt_print_hist_status() {
String s_min_h = String(min_h);
String s_max_h = String(max_h);
String s_min_t = String(min_t);
String s_max_t = String(max_t);
SerialBT.print("*tHumi: MIN("+ s_min_h +") | MAX("+ s_max_h +") %\n*");
if (CELSIUS)
SerialBT.print("*tTemp: MIN("+ s_min_t +") | MAX("+ s_max_t +") oC\n*");
else
SerialBT.print("*tTemp: MIN("+ s_min_t +") | MAX("+ s_max_t +") oF\n*");
SerialBT.print("*t\n*");
SerialBT.print("*t\n*");
SerialBT.print("*t\n*");
SerialBT.print("*t\n*");
SerialBT.print("*t\n*");
SerialBT.print("*t\n*");
SerialBT.print("*t\n*");
SerialBT.print("*t\n*");
}
void lcd_print_cur_status()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(t, 1);
lcd.print(" ");
lcd.write(0);
if (CELSIUS)
lcd.print("C");
else
lcd.print("F");
lcd.setCursor(0, 1);
lcd.print(h, 1);
lcd.print(" %");
}
void lcd_print_hist_status()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(min_t, 1);
lcd.setCursor(7, 0);
lcd.print(max_t, 1);
lcd.setCursor(13, 0);
lcd.write(0);
if (CELSIUS)
lcd.print("C");
else
lcd.print("F");
lcd.print("");
lcd.setCursor(0, 1);
lcd.print(min_h, 1);
lcd.setCursor(7, 1);
lcd.print(max_h, 1);
lcd.setCursor(14, 1);
lcd.print("%");
bt_print_hist_status();
}
void leBluetooth(){
if(SerialBT.available()){
dado=(SerialBT.read());
Serial.write(SerialBT.read());
escreveBluetooth();
}
}
void escreveBluetooth(){
if(dado=='M'){
STATE = false;
Serial.print("coisa:");
Serial.println(STATE);
}
if (dado=='N') {
STATE = true;
Serial.print("coisa:");
Serial.println(STATE);
}
if (dado == 'f') {
if (CELSIUS) {
t = ctof(t);
min_t = ctof(min_t);
max_t = ctof(max_t);
CELSIUS = false;
}
}
if (dado == 'c') {
if (!CELSIUS) {
t = ftoc(t);
min_t = ftoc(min_t);
max_t = ftoc(max_t);
CELSIUS = true;
}
}
if (dado == 'd') {
digitalWrite(LED_PIN, LOW);
bt_led_on = false;
SerialBT.print("*LR0G0B0*");
}
if (dado == 'D') {
digitalWrite(LED_PIN, HIGH);
bt_led_on = true;
SerialBT.print("*LR255G255B255*");
}
}