// (o\---/o)
// ( , , )
// ___,--.(_(Y)_),--.____
//| "--" "--" |
//| Don't Worry Be Happy |
//| HECTOR E. VILLARREAL |
//| 2023 |
//|______________________|
// ) | (
// (___,'.___)
//Datos de temperatura de Dallas DS18B20 a google sheets ver 1.00 09-12 23
//---------------------------------------------------------------------------------------------------
//Librerias
//--------------------------------------------------------------
#include <LiquidCrystal.h>
#include <Arduino.h>
//#include <ESP8266WiFi.h>
//#include <HTTPSRedirect.h> //Instalar, no está en el paquete del IDE
#include <WiFi.h>
#include <HTTPClient.h>
//--------------------------------------------------------------
//Declaración de los PULSADORES, VARIABLES Y LED
//--------------------------------------------------------------
//const int pe = D7; //Pulsador entrada
//const int ps = D8; //Pulsador salida
//volatile int estado = HIGH; //estado inicial
const int ledPin = LED_BUILTIN; // LED EMBEBIDO
unsigned long lastDebounceTimeEntrada = 0; // variable tiempo antirrebotes
unsigned long debounceDelay = 300; // Retardo antirrebotes
//--------------------------------------------------------------
//Declaración del LCD NODEMCU 3.0
//--------------------------------------------------------------
//LiquidCrystal lcd(D6, D5, D1, D2, D3, D4); // Corresponde a (RS, EN, D4, D5, D6, D7)
//--------------------------------------------------------------
//--------------------------------------------------------------
//Declaración del LCD ESP32
//--------------------------------------------------------------
LiquidCrystal lcd(12, 13, 32, 33, 27, 14); // Corresponde a (RS, EN, D4, D5, D6, D7)
//--------------------------------------------------------------
//Datos de la red
//--------------------------------------------------------------
//const char* ssid = "WIFI CORPORATIVO";
//const char* password = "3mnLRPvdBou#";
//--------------------------------------------------------------
//Datos de la red simulador
//--------------------------------------------------------------
const char* ssid = "Wokwi-GUEST"; // change SSID
const char* password = ""; // change password
//--------------------------------------------------------------
//Datos de Google Script Deployment ID:
//--------------------------------------------------------------
const char *GScriptId = "AKfycbxuDGVsHCoiFzj54RJVcAbY15NuTT_CJ_tcGxPVVP4X3ER2PPPjFTqorWZ_E00GFAifwQ";
// COMANDOS PARA GOOGLE SHEETS (default is Sheet1): Cambiar con el nombre de la hoja donde vamos a insertar los datos
String payload = "";
String payload_base = "{\"command\": \"insert_row\", \"sheet_name\": \"Hoja 1\", \"values\": ";
//--------------------------------------------------------------
//Parametros de Google Sheets setup (no editar)
//--------------------------------------------------------------
const char* host = "script.google.com";
const int httpsPort = 443;
const char* fingerprint = "";
String url = String("/macros/s/") + GScriptId + "/exec";
//HTTPSRedirect* client = nullptr;
//--------------------------------------------------------------
// Declare variables that will be published to Google Sheets
const int value0 = 1960090501; // Constante ID del dispositivo
int value1 = 0; // variable de registro
volatile int value2 = 0; // variable Contador de entrada
volatile int value3 = 0; // variable Contador de salida
//--------------------------------------------------------------
// Caracteres de conexión conexión
//--------------------------------------------------------------
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
lcd.setCursor(15, 1);
lcd.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
//--------------------------------------------------------------
/*void buttonEntradaISR() {
// Antirrebote
if ((millis() - lastDebounceTimeEntrada) > debounceDelay) {
value2++;
lastDebounceTimeEntrada = millis();
}
}
void buttonSalidaISR() {
// Antirrebote
if ((millis() - lastDebounceTimeEntrada) > debounceDelay) {
value3++;
lastDebounceTimeEntrada = millis();
}
}*/
//------------------------------------------------------------------------------------------------------
//INICIALIZACION y CONFIGURACION del sistema
//------------------------------------------------------------------------------------------------------
void setup() {
Serial.begin(115200);
delay(100);
Serial.println('\n');
//Configura los 2 Pines de Pulsadores como ENTRADAS digitales
//pinMode(pe,INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(pe), buttonEntradaISR, CHANGE);
//pinMode(ps,INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(ps), buttonSalidaISR, CHANGE);
//--------------------------------------------------------------
//--------------------------------------------------------------
#include "mensaje_inicio.h" //Mensaje de inicio
//--------------------------------------------------------------
// Configura el led BUILTIN
pinMode(LED_BUILTIN, OUTPUT);
//--------------------------------------------------------------
// Conexión a WiFi
WiFi.begin(ssid, password);
lcd.setCursor(0, 0);
lcd.print("Connecting to ..");
lcd.setCursor(0, 1);
lcd.print("WiFi ");
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println(" ...");
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner(); //metodo para caracteres de conexión
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
lcd.clear();
lcd.setCursor(2, 0); // Selecciono la Primera linea
lcd.print("Connection ok!"); // escribimos en la primera linea
lcd.setCursor(3, 1); // Selecciono la Segunda linea
//lcd.print("IP: " + WiFi.localIP()); arduino real
lcd.print(WiFi.localIP()); // arduio wokwi
delay(3000);
lcd.clear();
lcd.setCursor(0, 0); // Selecciono la Primera linea
lcd.print("Contr.Aforo v1.0"); // escribimos en la primera linea
delay(3000);
// Use HTTPSRedirect class to create a new TLS connection
/*client = new HTTPSRedirect(httpsPort);
client->setInsecure();
client->setPrintResponseBody(true);
client->setContentTypeHeader("application/json");
Serial.print("Connecting to ");
Serial.println(host);
//--------------------------------------------------------------
// Prueba 5 Intentos de conexión
//--------------------------------------------------------------
// Try to connect for a maximum of 5 times
bool flag = false;
for (int i=0; i<5; i++){
int retval = client->connect(host, httpsPort);
if (retval == 1){
flag = true;
Serial.println("Connected");
break;
}
else
Serial.println("Connection failed. Retrying...");
}
if (!flag){
Serial.print("Could not connect to server: ");
Serial.println(host);
lcd.clear();
lcd.setCursor(0, 0); // Selecciono la Primera linea
lcd.print("Could not connect"); // escribimos no se pudo conectar
delay(3000);
return;
}
//delete client; // delete HTTPSRedirect object
//client = nullptr; // delete HTTPSRedirect object*/
}
//--------------------------------------------------------------
//--------------------------------------------------------------
//Programa a ejecutar LOOP
//--------------------------------------------------------------
void loop() {
// incremento de cantidad de regiastros
value1 ++;
value2 = random(0,1000);
value3 = random(0,100000);
// Envía DATOS al lcd
lcd.setCursor(0, 1);
lcd.print("IN=");
lcd.print(value2);
lcd.print(" OUT=");
lcd.print(value3);
// Destellos del led
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}