///       (o\---/o)           
//        ( , , )
// ___,--.(_(Y)_),--.____
//|   "--"       "--"    |
//| Don't Worry Be Happy |
//| HECTOR E. VILLARREAL |
//|         2023         |
//|______________________|
//       )   |   (
//      (___,'.___) 
//Lectura de temperatura de Dallas DS18B20 ver 1.01 29-12 23
//Copyright (C) 2023, Héctor Villarreal
//---------------------------------------------------------------------------------------------------
//Librerias
//--------------------------------------------------------------
#include <LiquidCrystal.h>
#include <Arduino.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//** #include <ESP8266WiFi.h>
//** #include <HTTPSRedirect.h>  //Instalar, no está en el paquete del IDE
//--------------------------------------------------------------
//Declaración de variables
//--------------------------------------------------------------
#define ONE_WIRE_BUS 6             //Se establece el pin 6  como bus OneWire
#define TEMPERATURE_PRECISION 12   // Maxima resolución 12 bits (entre 9 y 12 bits)
//--------------------------------------------------------------
//Declaración del LCD Arduino Nano
//--------------------------------------------------------------
LiquidCrystal lcd(A5, A4, A3, A2, A1, A0);  // Corresponde a (RS, EN, D4, D5, D6, D7)
//--------------------------------------------------------------
//Declaración del LCD NodeMCU
//--------------------------------------------------------------
//** LiquidCrystal lcd(D6, D5, D1, D2, D3, D4);  // Corresponde a (RS, EN, D4, D5, D6, D7)
int pinLed= 5; //Pin para el led error

OneWire oneWire(ONE_WIRE_BUS);        /* Setup a oneWire instance to communicate with any OneWire devices 
                                         (not just Maxim/Dallas temperature ICs)*/
DallasTemperature sensors(&oneWire);  //Se declara una variable u objeto para nuestro sensor
int numberOfDevices;                  // variable para cantidad de despositivos encontrados
DeviceAddress tempDeviceAddress;      // variable para address de despositivos encontrados 
long value0 = 1960090501;             // Constante ID del dispositivo para registro
volatile int tempC = 0;
int e = 0;
//--------------------------------------------------------------
  #include "datos_red.h" //Mensaje de inicio
//--------------------------------------------------------------

//------------------------------------------------------------------------------------------------------
//INICIALIZACION y CONFIGURACION del sistema
//------------------------------------------------------------------------------------------------------
void setup(void) {
  pinMode(pinLed, OUTPUT); //Pin del led modo salida
  digitalWrite(pinLed, LOW);
//--------------------------------------------------------------
  #include "mensaje_inicio.h" //Mensaje de inicio
//--------------------------------------------------------------
  Serial.begin(115200);     // start serial port
  Serial.println("Dallas DS18B20 Control v.1.0");
  sensors.begin();
  Serial.print("Power is: ");  // report power requirements
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else {
  Serial.println("OFF");
   } 
  numberOfDevices = sensors.getDeviceCount();   // almacena la cantidad de dispositivos en el wire
  Serial.print("Locating devices...");          // Buscando dispositivos en el bus
  Serial.print(numberOfDevices, DEC);
  if ((numberOfDevices)== 0) {
      digitalWrite(pinLed, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);                     // Selecciono la Primera linea
      lcd.print("Error:3 not read");           // escribimos en la primera linea el Error 3
      delay(1000);
      lcd.setCursor(0, 2);
      String message = "Dallas no found!"; //mensaje error fuera de rango
      for (byte i = 0; i < message.length(); i++) {
          lcd.print(message[i]);
          delay(100);
       }
  }
  Serial.println(" devices.");
  for (int i = 0; i < numberOfDevices; i++) { // Loop through each device, print out address
  int e = i + 1;
   // Search the wire for address
    if (sensors.getAddress(tempDeviceAddress, i)) {
      Serial.print("Found device ");
      Serial.print(i, DEC);
      lcd.setCursor(0, 0); // Selecciono la Primera linea
      lcd.print("Found device "); // encuentra dispositivo
      lcd.print(e, DEC);  // encuentra dispositivo
      lcd.setCursor(0, 1);
      lcd.print("DS18B20");
      Serial.print(" with address: ");
      printAddress(tempDeviceAddress);
      Serial.println();
      sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);  /* set the resolution to TEMPERATURE_PRECISION bit 
      (Each Dallas/Maxim device is capable of several different resolutions)*/
      Serial.print("Resolution set to: ");
      Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
      Serial.println();
    } 
    else {
      //delay(3000);
      Serial.print("Found ghost device at ");
      Serial.print(i, DEC);
      Serial.print(" but could not detect address. Check power and cabling");
      digitalWrite(pinLed, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0); // Selecciono la Primera linea
      lcd.print("Error:4 not read"); // escribimos en la primera linea
      delay(1000);
      lcd.setCursor(0, 2);
      String message = "Address no found!"; //mensaje error fuera de rango
      for (byte i = 0; i < message.length(); i++) {
            lcd.print(message[i]);
            delay(100);
       }
    }
  }
      //lcd.clear();
      ///lcd.setCursor(0, 0); // Selecciono la Primera linea
      //lcd.print("Found device ");
      //lcd.print(e, DEC);  // encuentra dispositivo
      //lcd.setCursor(0, 1);
      //lcd.print("DS18B20");
 }
//-----------------------------------------------------------------------------------------------
//Programa a ejecutar LOOP
//-----------------------------------------------------------------------------------------------
void loop(void)  {
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  sensors.requestTemperatures(); // Send the command to get temperatures
  delay(3000);
  for (int i = 0; i < numberOfDevices; i++) {       // Loop through each device, print out temperature data
    if (sensors.getAddress(tempDeviceAddress, i)) { // Search the wire for address
  // It responds almost immediately. Let's print out the data
      printTemperature(tempDeviceAddress); // Use a simple function to print out the data
    }
    else  { 
      Serial.println(" Error:1 ghost device! Check your power requirements and cabling ");
      digitalWrite(pinLed, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0); // Selecciono la Primera linea
      lcd.print("Error:1 not read"); // escribimos en la primera linea
      delay(1000);
      lcd.setCursor(0, 2);
      String message = "Check cabling!";
      for (byte i = 0; i < message.length(); i++) {
          lcd.print(message[i]);
          delay(100);
      } 
    }
  }
}
//-----------------------------------------------------------------------------------------------
//FUNCIONES a usar
//-----------------------------------------------------------------------------------------------
//Función para presentar la dirección del dispositivo
//--------------------------------------------------------------
void printAddress(DeviceAddress deviceAddress) {   // function to print a device address
  for (uint8_t i = 0; i < 8; i++) {
      if (deviceAddress[i] < 16) Serial.print("0");
      Serial.print(deviceAddress[i], HEX);
      lcd.setCursor(0, 1);
  }
}
//--------------------------------------------------------------
//Función para presentar la Temperatura del dispositivo
//--------------------------------------------------------------
void printTemperature(DeviceAddress deviceAddress) {   // function to print the temperature for a device
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == DEVICE_DISCONNECTED_C){
      Serial.println("Error:2 Could not read temperature data");
      digitalWrite(pinLed, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0); // Selecciono la Primera linea
      lcd.print("Error:2 not read"); // escribimos en la primera linea
      delay(1000);
      lcd.setCursor(0, 2);
      String message = "Out of range!"; //mensaje error fuera de rango
      for (byte i = 0; i < message.length(); i++) {
            lcd.print(message[i]);
            delay(100);
       }
     return;
  }
  digitalWrite(pinLed, LOW);
  lcd.clear();
  Serial.print("Temp °C: ");
  Serial.println(tempC);
  // Envía DATOS al lcd
  lcd.setCursor(0, 0); // Selecciono la Primera linea
  lcd.print("Contr.Temp v 1.0"); // escribimos en la primera linea
  lcd.setCursor(0, 1);
  lcd.print("T= ");
  lcd.print(tempC);
  lcd.setCursor(10, 1);
  lcd.print((char)223);
  lcd.print("C");
 }
//--------------------------------------------------------------
Loading chip...chip-ds18b20