C++ WEMOS D1和x2B;达拉斯温度:如何在if中打印温度和温度比较
我用DS18B20和Wemos D1板制作了一个加热控制器,但如果我尝试在C++ WEMOS D1和x2B;达拉斯温度:如何在if中打印温度和温度比较,c++,arduino-esp8266,temperature,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/li
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);
}
}
//此Arduino草图显示DS18B20“单线”数字
//温度传感器。
//版权所有(c)2010 Mark McComb,hacktronics LLC
//执照:(发疯)
//教程:
//
#包括
#包括
//数据线插入Arduino上的针脚3
#定义一条\u线\u总线0
//设置oneWire实例以与任何oneWire设备通信
单线单线(单线总线);
//将我们的单线参考传递到达拉斯温度。
达拉斯温度传感器(单线);
//分配单线温度传感器的地址。
//请参阅有关如何获取这些地址的教程:
//
const int mintemp=30;
DeviceAddress InsideEtherMometer={0x28、0xFF、0x83、0x51、0xB2、0x17、0x4、0x8A};
外部温度计的设备地址={0x28、0xFF、0x4F、0xAB、0xC4、0x17、0x5、0x83};
DeviceAddress DogHouse温度计={0x28、0xFF、0xBF、0xA9、0xC4、0x17、0x4、0x7C};
作废设置(作废)
{
//启动串行端口
序列号开始(112500);
//启动图书馆
begin();
//将分辨率设置为10位(足够好吗?)
传感器。设置分辨率(内部风速计,10);
传感器。设置分辨率(外部温度计,10);
传感器.设置分辨率(狗屋温度计,10);
}
无效打印温度(设备地址设备地址)
{
float tempC=sensors.getTempC(设备地址);
如果(tempC==-127.00){
串行打印(“获取温度错误”);
}否则{
连载印刷体(“C:”);
串行打印(tempC);
连续打印(“F:”);
串行打印(达拉斯温度:托法伦海特(tempC));
}
}
无效循环(无效)
{
延迟(2000年);
Serial.print(“获取温度…\n\r”);
传感器。请求温度();
串行打印(“内部温度为:”);
打印温度(内部风速计);
串行打印(“\n\r”);
串行打印(“外部温度为:”);
打印温度(外部温度计);
串行打印(“\n\r”);
串行打印(“狗舍温度为:”);
打印温度(狗屋温度计);
Serial.print(“\n\r\n\r”);
int-insideThermometer=(int)insideThermometer;
Serial.print(InsideEtherMometer);//在Serial中,此值为0。
如果(内部风速计>最小风速计){
连续打印(“工作”);
串行打印(内部风速计);
}
}
在这行中:
int insideThermometer = (int)insideThermometer;
您可以创建一个局部变量并将其分配给自身。不是你想要的。您尝试使用的全局变量是
DeviceAddress insideThermometer = { 0x28, 0xFF, 0x83, 0x51, 0xB2, 0x17, 0x4, 0x8A };
如果查看,设备地址
的类型定义为
typedef uint8_t DeviceAddress[8];
如果要获取温度,需要调用sensors.getTempC(InsideEtherMometer)
,这已经在printTemperature
函数中完成了。由于您在测试temp之前调用了该函数,因此只需修改它以返回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) {
....
(您可能希望将函数的名称更改为类似于
print和turnTemperature
,因为这更清楚地说明了它的新功能。)不清楚您在问什么。我查看了列表,但不清楚您所引用的是什么或打印的内容。我建议您使用调试器逐步完成代码。int-insideThermometer=(int)insideThermometer代码>使用其他名称。此外,全局InsideEtherMometer
是一个数组。那条线应该做什么?哦,我不知道那是一个数组。我想打印阵列中的温度。这怎么可能?