简体   繁体   English

Arduino - 为基于芯片的 DHT 传感器选择接头(ESP32 与 ESP8266)

[英]Arduino - Select header for DHT Sensor Based on Chip (ESP32 vs ESP8266)

I'll start by saying I'm not a C++/Arduino expert but I've played enough with this issue and googled enough where I can't figure it out.我首先要说我不是 C++/Arduino 专家,但我对这个问题已经玩够了,并且在我无法弄清楚的地方搜索了足够多的谷歌。

I have some Arduino code that is calling a sensor library file which then is calling the DHT library ( https://github.com/RobTillaart/Arduino/tree/master/libraries/ ).我有一些 Arduino 代码正在调用一个传感器库文件,然后调用 DHT 库( https://github.com/RobTillaart/Arduino/tree/master/libraries/ )。 There are two main versions of this library, one in DHTStable which maintains compatibility with AVR and ARM based chips and one in DHTLib which is optimized for AVR.这个库有两个主要版本,一个在 DHTStable 中,它保持与基于 AVR 和 ARM 的芯片的兼容性,另一个在 DHTLib 中,它针对 AVR 进行了优化。 I am compiling the same sketch for both a ESP8266 (which requires the DHTStable) and a ESP32 (which works better with the DHTlib).我正在为 ESP8266(需要 DHTStable)和 ESP32(与 DHTlib 配合使用效果更好)编译相同的草图。

Right now I compile and download to a ESP32 then when I'm done I move the DHTLib library out, copy the DHTStable in, then compile and download for the ESP8266.现在我编译并下载到 ESP32,然后当我完成后我将 DHTLib 库移出,复制 DHTStable,然后为 ESP8266 编译和下载。 Few days or a week later I do this copying and pasting again.几天或一周后,我再次进行复制和粘贴。

In my sensor library I am already doing a check for the ESP32 for certain commands and if not a ESP32 I do the commands for a ESP8266 using #if defined(ARDUINO_ARCH_ESP32) and this works great for commands.在我的传感器库中,我已经在检查 ESP32 的某些命令,如果不是 ESP32,我会使用#if defined(ARDUINO_ARCH_ESP32)为 ESP8266 执行命令,这对命令非常#if defined(ARDUINO_ARCH_ESP32) However I am trying to use it for the defines and it "kinda" works.但是我试图将它用于定义并且它“有点”有效。 I have to take one of the two libraries and rename the header and cpp file along with all references internally to be different then the other.我必须采用两个库中的一个,并将头文件和 cpp 文件以及所有内部引用重命名为与另一个不同。 Then in my If I can do a if ESP32 use dht-avr.h (renamed) else dht.h (original).然后在我的 If I can do a if ESP32 中使用 dht-avr.h(重命名) else dht.h(原始)。

This all works but I'm using git to clone and keep updated the libraries so each time there is a update I have to go through and rename everything.这一切都有效,但我使用 git 来克隆并保持更新库,因此每次有更新时,我都必须通过并重命名所有内容。 So I tried #include <DHTlib\\dht.h> and <DHTStable\\dht.h> but these don't work (can't find library).所以我尝试了 #include <DHTlib\\dht.h><DHTStable\\dht.h>但这些不起作用(找不到库)。

Is what I want to do, calling the actual include path or two different headers when they are named identical, possible?我想要做的是,在命名相同时调用实际包含路径或两个不同的标头,可能吗? Since the If statement works I'm 90% there (100% if I keep renaming everything) but I'd really like this to be automatic.由于 If 语句有效,我 90% 在那里(如果我继续重命名所有内容,则为 100%)但我真的希望这是自动的。

@BMelis put me on the right track with the .. in the path. @BMelis 让我走上了正确的轨道,路径中的 .. The final code ended up being:最终的代码是:

#ifdef ARDUINO_ARCH_ESP32
#include "..\DHTlib\DHT.h"
#else
#include "..\DHTStable\DHT.h"
#endif

An option would be to hardcode the path to the libraries in your code.一种选择是对代码中库的路径进行硬编码。 You'll have to use quotation marks for the include instead of <...> :您必须对 include 使用引号而不是<...>

#ifdef ESP8266
#include "../path/to/esp8266-header.h"
#endif
#ifdef ARDUINO_ARCH_ESP32
#include "../path/to/esp32-header.h"
#endif

你可以试试这个库: https : //github.com/adafruit/DHT-sensor-library我在一些项目中使用并且效果很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM