Last active
September 16, 2020 21:03
-
-
Save tiagocordeiro/83223df285b7caf1c36c3d54b7385448 to your computer and use it in GitHub Desktop.
GreenHouse ESP32 with Blynk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************* | |
GreenHouse é um pequeno script para acompanhar um ESP32 com | |
sensores de umidade, temperatura (DHT22 ou DHT11) e luminosidade (LDR). | |
Além dos sensores, um display LCD 16x2 (interface I2C) pode ser usado. | |
Estou usando o Blynk para criar algumas interfaces para celular, mas é | |
completamente dispensável caso queira acompanhar os dados apenas via o | |
display LCD. | |
Download latest Blynk library here: | |
https://github.com/blynkkk/blynk-library/releases/latest | |
Blynk is a platform with iOS and Android apps to control | |
Arduino, Raspberry Pi and the likes over the Internet. | |
You can easily build graphic interfaces for all your | |
projects by simply dragging and dropping widgets. | |
Downloads, docs, tutorials: http://www.blynk.cc | |
Sketch generator: http://examples.blynk.cc | |
Blynk community: http://community.blynk.cc | |
Follow us: http://www.fb.com/blynkapp | |
http://twitter.com/blynk_app | |
Blynk library is licensed under MIT license | |
This example code is in public domain. | |
************************************************************* | |
This example runs directly on ESP32 chip. | |
Note: This requires ESP32 support package: | |
https://github.com/espressif/arduino-esp32 | |
Please be sure to select the right ESP32 module | |
in the Tools -> Board menu! | |
Change WiFi ssid, pass, and Blynk auth token to run :) | |
Feel free to apply it to any other example. It's simple! | |
*************************************************************/ | |
/* Comment this out to disable prints and save space */ | |
#define BLYNK_PRINT Serial | |
#include <WiFi.h> | |
#include <WiFiClient.h> | |
#include <BlynkSimpleEsp32.h> | |
#include <LiquidCrystal_I2C.h> | |
#include <DHT.h> | |
// Inicializa o display no endereco 0x27 | |
LiquidCrystal_I2C lcd(0x27, 16, 2); | |
byte termChar[] = { | |
B00100, | |
B01010, | |
B01010, | |
B01010, | |
B01110, | |
B01110, | |
B01110, | |
B00100}; | |
byte humidChar[] = { | |
B00000, | |
B00100, | |
B01110, | |
B11111, | |
B11111, | |
B11111, | |
B01110, | |
B00000}; | |
byte lumiChar[] = { | |
B00100, | |
B01110, | |
B01110, | |
B01010, | |
B10001, | |
B10001, | |
B10001, | |
B01110}; | |
// You should get Auth Token in the Blynk App. | |
// Go to the Project Settings (nut icon). | |
char auth[] = "YoushouldgetAuthTokenintheBlynkApp"; | |
// Your WiFi credentials. | |
// Set password to "" for open networks. | |
char ssid[] = "Your-WiFi_SSID"; | |
char pass[] = "Your-WiFi_PASSWD"; | |
float temperatura; | |
#define DHTPIN 15 // Define em que pino está conectado o módulo DHT | |
#define DHTTYPE DHT22 // Define se o módulo conectado é o DHT11 ou DHT22 | |
DHT dht(DHTPIN, DHTTYPE); | |
BlynkTimer timerDHT22; // Criação do Timer do DHT22 | |
BlynkTimer timerLDR; // Criação do Timer do LDR | |
void lerSensorDHT22() // Função para ler os parâmetros do DHT22 | |
{ | |
float temperatura = dht.readTemperature(); // Comando da biblioteca que lê a temperatura adquirida pelo DHT22 | |
float umidade = dht.readHumidity(); // Comando da biblioteca que lê a umidade adquirida pelo DHT22 | |
if (isnan(temperatura) || isnan(umidade)) | |
{ // isnan = IS Not A Number <= verifica se o valor lido não é um número | |
return; | |
} | |
Blynk.virtualWrite(V0, temperatura); // Escreve no pino virtual V0 do Blynk o valor da temperatura lido pelo DHT22 | |
Blynk.virtualWrite(V1, umidade); // Escreve no pino virtual V1 do Blynk o valor de umidade lido pelo DHT22 | |
lcd.setCursor(0, 0); | |
lcd.write(0); | |
lcd.print(temperatura, 1); | |
lcd.print("\xDF" | |
"C "); | |
lcd.setCursor(11, 0); | |
lcd.write(1); | |
lcd.print(umidade, 0); | |
lcd.print("% "); | |
} | |
void lerLDR() | |
{ // Função para ler o parâmetro do LDR | |
int valorLDR = analogRead(34); | |
valorLDR = map(valorLDR, 0, 4095, 0, 100); // Função que mapeia os valores lidos de 0 à 1023 em 0 à 100 | |
Blynk.virtualWrite(V2, valorLDR); // Escreve no pino virtual V2 do Blynk o valor de luminosidade lido pelo LDR | |
lcd.setCursor(0, 1); | |
lcd.print("GreenHouse "); | |
lcd.write(2); | |
lcd.print(String(valorLDR) + "% "); | |
} | |
void setup() | |
{ | |
// Debug console | |
Serial.begin(9600); | |
dht.begin(); // Inicializa o DHT22 | |
// LCD begin | |
lcd.begin(); | |
lcd.createChar(0, termChar); | |
lcd.createChar(1, humidChar); | |
lcd.createChar(2, lumiChar); | |
lcd.backlight(); | |
lcd.clear(); | |
lcd.setCursor(0, 0); | |
lcd.print(": Green House :"); | |
lcd.setCursor(0, 1); | |
lcd.print("_\\_ _\\_ _\\_ _\\_"); | |
// Setup timers | |
timerDHT22.setInterval(5000L, lerSensorDHT22); // Define o intervalo de tempo da leitura do DHT22 para 5000 milisegundos | |
timerLDR.setInterval(3000L, lerLDR); // Define o intervalo de tempo da leitura do LDR para 3000 milisegundos | |
Blynk.begin(auth, ssid, pass); | |
lcd.clear(); | |
} | |
void loop() | |
{ | |
Blynk.run(); | |
timerDHT22.run(); // Inicializa o Timer do DHT22 | |
timerLDR.run(); // Inicializa o Timer do LDR | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment