c++ - WEMOS D1 + DallasTemperature:如何在 if 中打印温度和温度比较
问题描述
我用 DS18B20 和 Wemos D1 板制作了一个加热控制器,但如果我尝试打印或检查温度,if
则返回 0 in int
。问题是什么?
// 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 };
如果您查看源代码,DeviceAddress
则 typdef'd 为
typedef uint8_t DeviceAddress[8];
如果你想得到你需要调用的温度sensors.getTempC(insideThermometer)
,你已经在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) {
....
(您可能希望将函数的名称更改printAndReturnTemperature
为更清楚地说明其新功能的名称。)
推荐阅读
- java - 从 android studio 的属性文件中读取 URL
- case - Redshift:窗口聚合案例
- java - 如何在 Netbeans 中使用 Java 连接到 MongoDB?
- java - Eclipse 显示状态为正在启动:启动虚拟机需要很长时间
- c - VSC 编译和运行中的 GCC 错误,未定义对“WinMain@16”的引用
- reactjs - 如何使用 useReducer 钩子将表单数据提交到数组中&我必须获取数据并显示它?
- django - 为什么搜索功能在 django 中不起作用?
- sql - PostgresSql Query
- r - keeping the original values in a data frame based on a condition
- c++ - 从 .cpp 构建 .so 文件并导出所有 .so 依赖项