【问题标题】:'dht' does not name a type'dht' 没有命名类型
【发布时间】:2021-02-03 06:08:05
【问题描述】:

我正在运行这段代码,但它一直给我这个错误。下面我放代码。这是一个气象站arduino代码。我已经添加并导入了这些库,但我一直收到同样的错误。

#include <stdlib.h>
#include <SoftwareSerial.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#define SSID "DroidSpot" //replace XXXXX by your router SSID
#define PASS "gggg" //replace YYYYY by your router password
#define IP "184.106.153.149" // thingspeak.com IP
#define DHT22_PIN 2
String GET = "GET /update?key=GDQ0LAAXLDGYMXW1&field1="; //replace ZZZZZ by your ThingSpeak channel write key
SoftwareSerial monitor(10, 11); //Serial communication to ESP8266 module (RX, TX)


dht DHT;
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

.....
    
    //read other sensors
    char buffer[10];
    //light sensor
    float luminance = analogRead(luminancePin);
    //UV sensor
    float uv = analogRead(uvPin);
    uv = uv * 0.0049; //convert values to volts
    uv = uv * 307; //convert to mW/m²
    uv = uv/200; //calculate UV index
    //temperature and humidity
    int chk = DHT.read22(DHT22_PIN);
    float humidity = DHT.humidity;
    float temperature = DHT.temperature;
    //pressure and temperature1
    sensors_event_t event;
    bmp.getEvent(&event);
    float pressure = 0;
    float temperature1 = 0;
    if (event.pressure)
    {
      pressure = event.pressure;
      bmp.getTemperature(&temperature1);
    }


错误在 dht DHT 中;线。它是:

'dht' does not name a type

【问题讨论】:

    标签: arduino arduino-uno dht


    【解决方案1】:

    检查您正在使用哪个库。您可能会尝试使用两个不同的库组合两个不同的源代码示例。


    您的代码主体似乎暗示您想要一个不同的库。这个库定义了你想要的类型:https://github.com/RobTillaart/DHTstable,正如 Juraj 指出的那样,带有适当的字段。

    您还必须更改标题,如here。特别是:

    #include <dht.h>
    

    如果您确实打算使用 Adafruit 库,正如您的包含所暗示的那样:

    • 正如错误所说,dht 没有类或类型定义。类名是DHT,而不是dht

      在 Github 存储库中查看 DHT.h,在同一存储库中查看 example

    切换令牌:

    DHT dht;
    

    并将所有其他 DHT 重构为 dht。您还需要确保调用正确的类方法,因为此库中未定义 read22

    【讨论】:

    • @Juraj 我不确定图书馆是这里的问题。其他头文件似乎表明它正在使用 Adafruit 传感器库,该库使用 DHT.h 而不是 dht.h。但我认为这是一个很好的 B 计划。
    • @Juraj 嗯...我明白了。似乎 OP 可能正在混合来自不同来源的代码。我会针对这种可能性进行修改。
    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 2023-04-07
    • 2017-04-01
    • 2015-06-09
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多