/// (o\---/o)
// ( , , )
// ___,--.(_(Y)_),--.____
//| "--" "--" |
//| Don't Worry Be Happy |
//| HECTOR E. VILLARREAL |
//| 2023 |
//|______________________|
// ) | (
// (___,'.___)
//Lectura de temperatura de dos Dallas DS18B20 ver 1.01 26-12 23
//Copyright (C) 2023, Héctor Villarreal
//---------------------------------------------------------------------------------------------------
//Librerias
//--------------------------------------------------------------
#include <LiquidCrystal.h>
#include <Arduino.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 6 // Data wire is plugged into port 6 on the Arduino
#define TEMPERATURE_PRECISION 12 // Max resolution 12 bits
//Set LCD Arduino Nano
LiquidCrystal lcd(A5, A4, A3, A2, A1, A0); // Corresponde a (RS, EN, D4, D5, D6, D7)
int pinLed= 5; //Pin para el led
OneWire oneWire(ONE_WIRE_BUS); /* Setup a oneWire instance to communicate with any OneWire devices
(not just Maxim/Dallas temperature ICs)*/
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
long value0 = 1960090501; // Constante ID del dispositivo
volatile int tempC = 0;
int device= 0;
//------------------------------------------------------------------------------------------------------
//INICIALIZACION y CONFIGURACION del sistema
//------------------------------------------------------------------------------------------------------
void setup(void) {
pinMode(pinLed, OUTPUT); //Pin del led modo salida
digitalWrite(pinLed, HIGH); //me aseguro que esté apagado el led de error
//--------------------------------------------------------------
#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(); // Grab a count of devices on the wire
Serial.print("Locating devices..."); // locate devices on the bus
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
if ((numberOfDevices)== 0) {
digitalWrite(pinLed, LOW);
lcd.clear();
lcd.setCursor(0, 0); // Selecciono la Primera linea
lcd.print("Error:3 not read"); // escribimos en la primera linea
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);
}
while ( true ); //detiene el programa por error de dallas
}
Serial.println(" devices.");
for (int i = 0; i < numberOfDevices; i++) { // Loop through each device, print out address
// Search the wire for address
device= i;
if (sensors.getAddress(tempDeviceAddress, i)) {
Serial.print("Found device ");
Serial.print(i, DEC);
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 {
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
digitalWrite(pinLed, LOW);
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 devices: ");
lcd.print (sensors.getDeviceCount());
lcd.setCursor(0, 1);
lcd.print("Dallas 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, LOW);
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);
}
}
//--------------------------------------------------------------
//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, LOW);
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, HIGH); // no error led apagado
//--------------------------------------------------------------
int num= device++;
// Envía DATOS al puerto serie
Serial.print("Sonda");
Serial.print(num, DEC);
Serial.print("= ");
Serial.print("Temp. ");
Serial.print(tempC);
Serial.println (" °C");
//--------------------------------------------------------------
// Envía DATOS al lcd
lcd.clear();
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(num, DEC);
lcd.print("= ");
lcd.print(tempC);
lcd.setCursor(11, 1);
lcd.print((char)223);
lcd.print("C");
if (device ==3) device = 1;
delay(3000);
}
//--------------------------------------------------------------