#include <Adafruit_GFX.h>
#include <SPI.h>
#include <WiFi.h>
#include <Adafruit_ILI9341.h>
#include <Arduino.h>
#include <Adafruit_FT6206.h>
#include "token.h"
#include <SD.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define API_KEY "AIzaSyBStVNCahF00hNSd-SrgI8RbDRJm4Z6PrM"
#define USER_EMAIL "[email protected]"
#define USER_PASSWORD "00000000"
#define DATABASE_URL "https://esp32-7eeb0-default-rtdb.firebaseio.com/"
struct Sector {
float temperatura, humedad, pH;
bool estado_sector = false, estado_riego;
} Sector [10];
#define CS_PIN 5
TaskHandle_t Task2 , Task3, Task4;
bool estado = false;
Adafruit_FT6206 ctp = Adafruit_FT6206();
// // The display also uses hardware SPI, plus #9 & #10
// #define TFT_CS 10
// #define TFT_DC 9
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
String dato;
void setup(void) {
Serial.println("INICIO CORRECTO.");
Serial.begin(115200);
const int entrada [] = {19,20,21};
for (byte i = 0; i < sizeof(entrada) / sizeof(entrada[0]); i++) {
pinMode(entrada[i], OUTPUT);
}
pinMode(47, INPUT);
WIFI_SETUP();
xTaskCreatePinnedToCore(loop2,"Task_2",1600,NULL,2,&Task2,1);
xTaskCreatePinnedToCore(loop3,"Task_3",1500,NULL,1,&Task3,0);
xTaskCreatePinnedToCore(loop4,"Task_4",1400,NULL,1,&Task4,0);
Firebase_Setup();
SD_Setup();
TFT_SETUP();
}
void loop() {
delay(10);
if (!ctp.touched()) {
return;
}
TS_Point p = ctp.getPoint();
int tempX = p.x;
p.x = map(p.y, 0, 320, 240, 0);
p.y = map(tempX, 0, 240, 0, 320);
Serial.print("Coordenadas: (");
Serial.print(p.x);
Serial.print(", ");
Serial.print(p.y);
Serial.println(")");
estado = true;
delay(100);
}
void loop3(void *parameter){
for(;;){
if(estado == true){
print_tft(80, 120, ILI9341_RED, 4, "CLICK ");
delay(100);
clear_linea(80, 120, 4, " ");
estado = !estado;
}
else delay(10);
}
vTaskDelay(10);
}
void loop4(void *parameter) {
for (;;) {
bool estado1 = false;
String dato = "";
if (Serial.available() ) {
while (Serial.available()) {
char c = Serial.read();
if (c == '\0') {
break;
}
dato += c;
}
estado1 = true;
}
if(estado1){
if(parcela(dato)){
extraccion(dato);
Serial.println("Exito");
}else{
Serial.println("Error de dato");
}
Serial.println("Datos ingresados " + dato);
estado1 = false;
}
if(digitalRead(47) == 1){
Serial.println("DATOS DE SECTORES\n");
for(byte i =0; i<10; i++){
if(Sector[i].estado_sector){
Serial.print("Sector ");
Serial.println(i+1);
Serial.print("Temperatura: ");
Serial.println(Sector[i].temperatura);
Serial.print("Humedad: ");
Serial.println(Sector[i].humedad);
Serial.print("pH: ");
Serial.println(Sector[i].pH);
Serial.print("Estado Riego: ");
if(Sector[i].estado_riego){
Serial.println("ENCENDIDO");
}
else{
Serial.println("APAGADO");
}
Serial.println("");
}
else{
Serial.print("Sector ");
Serial.print(i+1);
Serial.println(" APAGADO");
}
}
while(digitalRead(14)==1){delay(10);}
}
delay(50);
}
}
//FUNCION PARA CONECTAR AL WIFI
void loop2(void *parameter){
for(;;){
delay(1000);
conect();
}
vTaskDelay(10);
}
void extraccion(String Datos){ // EXTRACCION DE DATOS
char ext [5];
byte indice = 0;
for(byte i=0; i<2; i++){ //extrae los primeros dos digitos del sector
ext [i] = Datos[i];
}
indice = atoi(ext); // Conviert de cadena a entero (Sector)
lvariable(ext); // Limpia el arreglo
Sector[indice-1].estado_sector = true;
/* 01 30.4 45.6 67.9 1*/
Sector[indice-1].temperatura = lecturaFloat (3, 6, ext, Datos);
Sector[indice-1].humedad = lecturaFloat (8, 12, ext, Datos);
Sector[indice-1].pH = lecturaFloat (13, 16, ext, Datos);
Sector[indice-1].estado_riego = ((Datos[18] - '0') == 1 ) ;
return;
}
void lvariable(char* ext){ //Limpia el arreglo
for(int i= 0; i<5; i++){
ext[i] = '\0';
}
}
float lecturaFloat (int com , int lim, char* ext, String Datos){
for(int i = com; i<lim; i++){
ext [i-com] = Datos[i];
}
float valor = atof(ext);
return valor ;
}
bool parcela(String contenido ){ //VERIFICACION DE DATOS POR COM SERIAL
for(byte i= 0; i<2; i++){
if(!isdigit(contenido[i])){
return false;
}
}
return true;
}
void print_tft(int16_t x, int16_t y, uint16_t color, uint8_t textSize, const char* text) {
tft.setCursor(x, y);
tft.setTextColor(color);
tft.setTextSize(textSize);
tft.println(text);
}
void clear_linea(int16_t x, int16_t y, uint8_t textSize, const char* newText) {
int16_t textWidth = textSize * 6 * strlen(newText);
int16_t textHeight = textSize * 8;
tft.fillRect(x, y, textWidth, textHeight, ILI9341_BLACK);
delay(50);
print_tft(x, y, ILI9341_BLUE, textSize, newText);
}
void WIFI_SETUP() {
digitalWrite(19, LOW);
digitalWrite(20, LOW);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to WiFi ..");
byte contador = 0;
while (WiFi.status() != WL_CONNECTED && contador <=10) {
Serial.print('.');
contador++;
digitalWrite(21, HIGH);
delay(200);
digitalWrite(21, LOW);
delay(200);
}
(WiFi.status() == WL_CONNECTED) ? digitalWrite(19, HIGH): digitalWrite(20, HIGH);
Serial.println("\n" + WiFi.localIP().toString());
/*
Serial.println(WiFi.localIP());
IPAddress staticIP(192, 168, 0, 20);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(8, 8, 8, 8);
WiFi.config(staticIP, gateway, subnet, dns);
Serial.println("IP Address configured: " + WiFi.localIP().toString());
Serial.println();*/
}
void conect (){
if (!(WiFi.status() == WL_CONNECTED)){
digitalWrite(19, LOW);
digitalWrite(20, LOW);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("\nIntentando conectar al WiFi ..");
byte contador = 0;
while (WiFi.status() != WL_CONNECTED && contador <=10) {
Serial.print('.');
contador++;
digitalWrite(21, HIGH);
delay(200);
digitalWrite(21, LOW);
delay(200);
}
(WiFi.status() == WL_CONNECTED) ? digitalWrite(19, HIGH): digitalWrite(20, HIGH);
}
else {
Serial.println("GO");
delay(5000);
}
}
void Firebase_Setup(){
Serial.println("FIrebase datos ");
}
void SD_Setup(){
if (!SD.begin(CS_PIN)) {
Serial.println("ERROR EN SD!");
}
File textFile = SD.open("/doc.txt");
if (textFile) {
while (textFile.available()) {
dato += textFile.readStringUntil('\n');
dato += " ";
}
textFile.close();
}
Serial.println(dato);
}
void TFT_SETUP(){
Wire.setPins(10, 8);
tft.begin();
if (! ctp.begin(40)) {
Serial.println("ERROR EN CONTROLADOR DE TOUCH");
}
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
}