WEMOS D1 + DallasTemperature:如何在if中打印温度和温度比较

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

我用 DS18B20 和 Wemos D1 板制作了一个加热控制器,但如果我尝试在 if 中打印或检查温度,然后在 int 中返回 0。 有什么问题?

//  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);
    }
}

这一行:

int insideThermometer = (int)insideThermometer;

您创建了一个局部变量并将其分配给自己。不是你想要的。您尝试使用的全局变量是

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

如果您查看 source codeDeviceAddress 的类型定义为

typedef uint8_t DeviceAddress[8];

如果你想获得温度,你需要调用 sensors.getTempC(insideThermometer),你已经在 printTemperature 函数中调用了。由于您在测试温度之前调用该函数,只需将其修改为 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;
}

然后改为

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

(您可能希望将函数名称更改为类似 printAndReturnTemperature 的名称,因为这样可以更清楚地说明其新功能。)