'dht' does not name a type

4.4k views Asked by At

I'm running this code and it keeps giving me this error. Below I'm putting the code. It's a weather station arduino code. I already added and imported the libraries but I keep getting the same error.

#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);
    }


The error is in the dht DHT; line. It is:

'dht' does not name a type
1

There are 1 answers

3
General Grievance On

Check which library you are using. You might be trying to combine two different source code examples using two different libraries.


The body of your code seems to suggest you want a different library. This library defines the type you want: https://github.com/RobTillaart/DHTstable with the appropriate fields as Juraj has pointed out.

You'll have to change your header(s) as well, as here. Especially:

#include <dht.h>

If you do intend to use the Adafruit library, as your includes seem to suggest:

  • As the error says, there's no class or type definition for dht. The class name is DHT, not dht.

    See DHT.h in the Github repo, and this example in the same repo.

Switch the tokens around:

DHT dht;

and refactor all other DHT to dht. You'll also need to make sure you're calling the right class method, as read22 is not defined in this library.