// Include the necessary library for ESP32 WiFi functionalities
#include <WiFi.h>
#include <HTTPClient.h>
//Include the necessary library for sensor DHT11
#include <DHT.h>
//Include the necessary library for LCD
#include <LiquidCrystal_I2C.h>
// Define WiFi credentials
char ssid[] = "CELERITY_XIME_EXT";
char pass[] = "KEVIN5899";
// Define the Google Apps Script ID for the spreadsheet
String GAS_ID = "AKfycbyjrfSbogQvE_CTwtt5fqkaidjsRrfy6bGC_oZg13SL9_LRoGvYyufAV2unEMCezqgi";
// Define the domain name for the Google Apps Script
const char* host = "script.google.com";
//Declare pines for sensors DHT11
#define DHTTYPE DHT11 //Select type of DHT sensor
#define DHTPIN_amb 32
#define DHTPIN_1 33
#define DHTPIN_2 25
#define DHTPIN_3 26
//Configurate each DHT sensors
DHT dht_amb(DHTPIN_amb,DHTTYPE);
DHT dht_1(DHTPIN_1,DHTTYPE);
DHT dht_2(DHTPIN_2,DHTTYPE);
DHT dht_3(DHTPIN_3,DHTTYPE);
// Declare sensor variables
float Sensor1;
float Sensor2;
float Sensor3;
float Sensor4;
float Sensor5;
float Sensor6;
float Sensor7;
float Sensor8;
//Set the LCD number of columns and rows
int LCD_columns = 16;
int LCD_rows = 2;
//Configurate LCD with address and number of columns and rows
LiquidCrystal_I2C lcd(0x27, LCD_columns, LCD_rows);
//Create symbol of degree
byte degree[8] = {
0b01110,
0b10001,
0b10001,
0b10001,
0b01110,
0b00000,
0b00000,
0b00000
};
//Declare pines for buttons
#define button_amb 13
#define button_1 12
#define button_2 14
#define button_3 27
//Declare button and led for read pH
#define button_pH 4
#define led_pH 2
//Declare initial conditions of buttons
int state_amb = 0;
int state_1 = 0;
int state_2 = 0;
int state_3 = 0;
int state_pH = 0;
//Declare state to start of buttons
int start_amb = 0;
int start_1 = 0;
int start_2 = 0;
int start_3 = 0;
int start_pH = 0;
//Declare time variables
unsigned long previous_sensors = 0; //Previous time for sensors
unsigned long previous_pH = 0; //Previous time for read pH
unsigned long previous_gsheet = 0; //Previous time for export sensor data in google sheet
unsigned long current_time; //Time value for indicate to current time
//Declare interval time for ever conditions
const long time_sensors = 500; //Time value for read sensors data
const long time_pH = 86400000; //Time value for read pH 86400000
const long time_gsheet = 10000; //Time value for export sensor data in google sheet 1800000
// Setup function runs once when the ESP8266 starts
void setup() {
// Initialize serial communication at 115200 baud rate
Serial.begin(115200);
// Start WiFi connection
Serial.print("Connecting");
WiFi.begin(ssid, pass); // Connect to the WiFi network
// Wait until the ESP32 is successfully connected to the WiFi
while (WiFi.status() != WL_CONNECTED){
Serial.print(".");
}
//Configurate of buttons
pinMode(button_amb, INPUT);
pinMode(button_1, INPUT);
pinMode(button_2, INPUT);
pinMode(button_3, INPUT);
pinMode(button_pH, INPUT);
//Configurate of pH led
pinMode(led_pH, OUTPUT);
//Initialize DHT11 sensors
dht_amb.begin();
dht_1.begin();
dht_2.begin();
dht_3.begin();
//Initialize LCD
lcd.init();
lcd.backlight();
lcd.clear();
//Display for initiate system
for(int k=0 ; k<5 ; k++){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Iniciando");
lcd.setCursor(0, 1);
lcd.print("sistema...");
delay(1000);
lcd.clear();
}
}
// Main loop function runs repeatedly after the setup
void loop() {
//Set the current time
current_time = millis();
//Read buttons
state_amb = digitalRead(button_amb);
state_1 = digitalRead(button_1);
state_2 = digitalRead(button_2);
state_3 = digitalRead(button_3);
state_pH = digitalRead(button_pH);
//Read and assign values of sensors
if (current_time - previous_sensors >= time_sensors){
previous_sensors = current_time;
Sensor1 = dht_amb.readTemperature();
Sensor2 = dht_amb.readHumidity();
Sensor3 = dht_1.readTemperature();
Sensor4 = dht_1.readHumidity();
Sensor5 = dht_2.readTemperature();
Sensor6 = dht_2.readHumidity();
Sensor7 = dht_3.readTemperature();
Sensor8 = dht_3.readHumidity();
}
//Warning for read pH value every defined time
if (current_time - previous_pH >= time_pH){
previous_pH = current_time;
digitalWrite(led_pH, HIGH);
//for(int k=0 ; k<5 ; k++){
//lcd.clear();
//lcd.setCursor(5, 0);
//lcd.print("ALERTA");
//lcd.setCursor(4, 1);
//lcd.print("Medir pH");
//delay(1000);
//lcd.clear();}
}
//Call the functions for read state buttons and display sensors in LCD
state_buttons();
display_LCD();
// Call the function to update the Google Sheet with sensor values
if (current_time - previous_gsheet >= time_gsheet){
previous_gsheet = current_time;
update_google_sheet();
}
}
// Function to send sensor data to the Google Sheet
void update_google_sheet(){
Serial.print("connecting to ");
Serial.println(host);
// Create a secure WiFi client instance
WiFiClientSecure client;
// Define the port for HTTPS connection
const int httpPort = 443; // 80 is for HTTP / 443 is for HTTPS!
// Set the client to insecure mode to avoid security conflicts
client.setInsecure();
// Try to connect to the Google Apps Script host
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// Construct the URL with sensor data as parameters
String url = "/macros/s/" + GAS_ID + "/exec?T1=";
url += String(Sensor1);
url += "&T2=" + String(Sensor2);
url += "&T3=" + String(Sensor3);
url += "&T4=" + String(Sensor4);
url += "&T5=" + String(Sensor5);
url += "&T6=" + String(Sensor6);
url += "&T7=" + String(Sensor7);
url += "&T8=" + String(Sensor8);
Serial.print("Requesting URL: ");
Serial.println(url);
// Send the GET request to the Google Apps Script
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println();
Serial.println("closing connection");
}
void state_buttons(){
//Configurate buttons for ever conditions or environment
if(state_amb == 1){
lcd.clear();
start_amb = 1;
start_1 = 0;
start_2 = 0;
start_3 = 0;
delay(300);
}
if(state_1 == 1){
lcd.clear();
start_amb = 0;
start_1 = 1;
start_2 = 0;
start_3 = 0;
delay(300);
}
if(state_2 == 1){
lcd.clear();
start_amb = 0;
start_1 = 0;
start_2 = 1;
start_3 = 0;
delay(300);
}
if(state_3 == 1){
lcd.clear();
start_amb = 0;
start_1 = 0;
start_2 = 0;
start_3 = 1;
delay(300);
}
if(state_pH == 1){
digitalWrite(led_pH, LOW);
delay(300);
}
}
//Function for display the differents environments
void display_LCD(){
//Wait condition until an environment is selected
if (start_amb == 0 && start_1 == 0 && start_2 == 0 && start_3 == 0){
lcd.setCursor(0, 0);
lcd.print("Seleccione");
lcd.setCursor(0, 1);
lcd.print("una lectura");
}
//Conditions to each button for visualize
if (start_amb == 1){
lcd.setCursor(0, 0);
lcd.print("Temp: " + String(Sensor1));
lcd.createChar(0, degree);
lcd.setCursor(13, 0);
lcd.write(0);
lcd.setCursor(14,0);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humedad: "+ String(Sensor2) + " %");
}
if (start_1 == 1){
lcd.print("Temp: " + String(Sensor3));
lcd.createChar(0, degree);
lcd.setCursor(13, 0);
lcd.write(0);
lcd.setCursor(14,0);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humedad: "+ String(Sensor4) + " %");
}
if (start_2 == 1){
lcd.print("Temp: " + String(Sensor5));
lcd.createChar(0, degree);
lcd.setCursor(13, 0);
lcd.write(0);
lcd.setCursor(14,0);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humedad: "+ String(Sensor6) + " %");
}
if (start_3 == 1){
lcd.print("Temp: " + String(Sensor7));
lcd.createChar(0, degree);
lcd.setCursor(13, 0);
lcd.write(0);
lcd.setCursor(14,0);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humedad: "+ String(Sensor8) + " %");
}
}