WEMOS D1 + DallasTemperature:如何在以下情况下打印温度和温度比较

时间:2018-10-12 14:11:32

标签: c++ arduino-esp8266 temperature

我用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);
    }
}

1 个答案:

答案 0 :(得分:0)

在这一行:

price = keyboard.readLine();
  info[i] = new Libri2();
  info[i].setPrice(price);

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

int insideThermometer = (int)insideThermometer;

如果您查看source code,则将DeviceAddress insideThermometer = { 0x28, 0xFF, 0x83, 0x51, 0xB2, 0x17, 0x4, 0x8A }; 键入为

DeviceAddress

如果要获取温度,则需要调用typedef uint8_t DeviceAddress[8]; ,您已经在sensors.getTempC(insideThermometer)函数中进行了此操作。由于您是在测试温度之前调用该函数的,因此只需对其进行修改以返回该温度:

printTemperature

然后更改为

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) { .... 之类的名称,因为它可以更清楚地说明其新功能。)