Search code examples
c++arduino-esp8266temperature

WEMOS D1 + DallasTemperature: How to print temperature and temperature comparison in if


I make a controller of heating with DS18B20 and Wemos D1 board, but if i try print or check the temperature in if then give back 0 in int. What is the problem?

//  This Arduino sketch reads DS18B20 "1-Wire" digital
//  temperature sensors.
//  Copyright (c) 2010 Mark McComb, hacktronics LLC
//  License: <a href="http://www.opensource.org/licenses/mit-license.php" rel="nofollow">http://www.opensource.org/licenses/mit-license.php</a> (Go crazy)
//  Tutorial:
//  <a href="http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html" rel="nofollow">http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html</a>

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 0

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// <a href="http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html" rel="nofollow">http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html</a>
const int mintemp = 30;
DeviceAddress insideThermometer = { 0x28,  0xFF,  0x83,  0x51,  0xB2,  0x17,  0x4,  0x8A };
DeviceAddress outsideThermometer = { 0x28,  0xFF,  0x4F,  0xAB,  0xC4,  0x17,  0x5,  0x83 };
DeviceAddress dogHouseThermometer = { 0x28,  0xFF,  0xBF,  0xA9,  0xC4,  0x17,  0x4,  0x7C };

void setup(void)
{
  // start serial port
  Serial.begin(112500);
  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(insideThermometer, 10);
  sensors.setResolution(outsideThermometer, 10);
  sensors.setResolution(dogHouseThermometer, 10);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
}

void loop(void)
{
  delay(2000);
  Serial.print("Getting temperatures...\n\r");
  sensors.requestTemperatures();

  Serial.print("Inside temperature is: ");
  printTemperature(insideThermometer);
  Serial.print("\n\r");
  Serial.print("Outside temperature is: ");
  printTemperature(outsideThermometer);
  Serial.print("\n\r");
  Serial.print("Dog House temperature is: ");
  printTemperature(dogHouseThermometer);
  Serial.print("\n\r\n\r");
  int insideThermometer = (int)insideThermometer;
    Serial.print(insideThermometer); //In Serial this give 0.
  if(insideThermometer > mintemp){
    Serial.print("work");
    Serial.print(insideThermometer);
    }
}

Solution

  • In this line:

    int insideThermometer = (int)insideThermometer;
    

    You create a local variable and assign it to itself. Not what you wanted. The global var you are trying to use is

    DeviceAddress insideThermometer = { 0x28,  0xFF,  0x83,  0x51,  0xB2,  0x17,  0x4,  0x8A };
    

    If you look at the source code, DeviceAddress is typdef'd as

    typedef uint8_t DeviceAddress[8];
    

    If you want to get the temperature you need to call sensors.getTempC(insideThermometer), which you already do in the printTemperature function. Since you call that function before testing the temp, just modify it to return the temp:

    float printTemperature(DeviceAddress deviceAddress)
    {
      float tempC = sensors.getTempC(deviceAddress);
      float tempF = 0;
      if (tempC == -127.00) {
        Serial.print("Error getting temperature");
      } else {
        Serial.print("C: ");
        Serial.print(tempC);
        Serial.print(" F: ");
        tempF = DallasTemperature::toFahrenheit(tempC);
        Serial.print(tempF);
      }
      return tempF;
    }
    

    Then change to

    int insideTempF = printTemperature(insideThermometer);
    ....
    if (insideTempF > mintemp) {
        ....
    

    (You may want to change the name of the function to something like printAndReturnTemperature as that more clearly states its new functionality.)