//       (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 <OneWire.h>
#include <DallasTemperature.h>
//--------------------------------------------------------------
#include <WiFi.h>
#include <HTTPClient.h>
//#include <ESP8266WiFi.h>
//#include <HTTPSRedirect.h>  //Instalar, no está en el paquete del IDE
//--------------------------------------------------------------
//Declaración de los PULSADORES, VARIABLES Y LED
//--------------------------------------------------------------
//Declaración de DALLAS ESP32
// Pin donde se conecta el bus 1-Wire
const int pinDatosDQ = 23;
//--------------------------------------------------------------
int pinLedError= 4; //Pin para el led error
int pinLedWifi=  15; //Pin para el led WIFI
int pinLedData=  5; //Pin para el led DATA
//--------------------------------------------------------------
//Declaración de DALLAS NODEMCU
// Pin donde se conecta el bus 1-Wire
//const int pinDatosDQ = D7;
//--------------------------------------------------------------
// Instancia a las clases OneWire y DallasTemperature
OneWire oneWireObjeto(pinDatosDQ);
DallasTemperature sensorDS18B20(&oneWireObjeto);
//--------------------------------------------------------------
const int value0 = 196009052; // Constante ID del dispositivo
//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
volatile int valuetemp = 0;  // variable temperatura
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);
  //sensorDS18B20.begin();   // Iniciamos el bus 1-Wire
  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
  pinMode(pinLedError, OUTPUT); //Pin del led modo salida
  digitalWrite(pinLedError, LOW);
  pinMode(pinLedWifi, OUTPUT); //Pin del led modo salida
  digitalWrite(pinLedWifi, LOW);
  pinMode(pinLedData, OUTPUT); //Pin del led modo salida
  digitalWrite(pinLedData, LOW);
//--------------------------------------------------------------
// 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
    //digitalWrite(pinLedWifi, HIGH);
  }
  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
  digitalWrite(pinLedWifi, HIGH);
  delay(3000);
  lcd.clear();
  lcd.setCursor(0, 0); // Selecciono la Primera linea
  lcd.print("Contr.Temp v 1.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() {

sensorDS18B20.requestTemperatures();

// Leemos y mostramos los datos de los sensores DS18B20
    Serial.print("Temperatura sensor 0: ");
    Serial.print(sensorDS18B20.getTempCByIndex(0));
    Serial.println(" C");
    
// incremento de cantidad de registros
  value1 ++;
  valuetemp=sensorDS18B20.getTempCByIndex(0);
  valuetemp= value2;
  value2 = random(0,70);
  value3 = random(0,100000);

// Envía DATOS al lcd
  lcd.setCursor(0, 1);
  lcd.print("Tmp=");
  lcd.print(valuetemp);
  lcd.setCursor(9, 1);
  lcd.print("Celsius");
  digitalWrite(pinLedData, HIGH);
// Destellos del led
  delay(2000);                    // wait for a second
  digitalWrite(pinLedData, LOW);
  delay(2000);
}
Loading
ds18b20