dht does not name a type.

Hello everybody. Im trying to run the code of a weather station in Arduino but I keep getting the dht does not name a type error. And it gives me the error on a line where I've written this: dht DHT;

I have included the libraries and even installed them on the side but I keep getting that. If needed I can even post the full log of the error. If anyone can help me resolve it I would be very grateful.

Here is the code:

#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 "password!" //replace YYYYY by your router password
#define IP "184.106.153.149" // thingspeak.com IP
#define DHT22_PIN 2
String GET = "GET /update?key=link="; //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);

//Variables
int luminancePin = A0;
int uvPin = A1;
int dustPin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long delay_time = 60000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;

//setup
void setup()
{
//start serial communications
Serial.begin(9600);
monitor.begin(9600);
Serial.println("Initializing...");

//configure Arduino pins
pinMode(dustPin, INPUT);

//initialize pressure sensor
Serial.println("Detecting BMP085 pressure sensor...");
if(!bmp.begin())
{
Serial.println("BMP085 sensor wasn't detected. Verify your connections or I2C ADDR!");
while(1);
}
Serial.println("BMP085 detected!");

//communication with wifi module
monitor.flush();
monitor.println("AT");
delay(2000);

if(monitor.find("OK")){
Serial.println("Communication with ESP8266 module: OK");
}
else {
Serial.println("ESP8266 module ERROR");
}

//connect wifi router
connectWiFi();

Serial.print("Sampling (");
Serial.print(sampletime_ms/1000);
Serial.println("s)...");

//initialize timer
starttime = millis();

}

void loop(){

//measuring dust particles
duration = pulseIn(dustPin, LOW);
lowpulseoccupancy = lowpulseoccupancy + duration;

//30 seconds cicle
if ((millis() - starttime) >= sampletime_ms)
{
ratio = lowpulseoccupancy/(sampletime_ms10.0); // percentage (de 0 a 100%)
concentration = 1.1
pow(ratio,3)-3.8pow(ratio,2)+520ratio+0.62; // from datsheet
lowpulseoccupancy = 0;

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

//convert sensor values to strings
String luminanceStr = dtostrf(luminance, 4, 1, buffer);
luminanceStr.replace(" ","");
String uvStr = dtostrf(uv, 4, 1, buffer);
uvStr.replace(" ","");
String humidityStr = dtostrf(humidity, 4, 1, buffer);
humidityStr.replace(" ","");
String temperatureStr = dtostrf(temperature, 4, 1, buffer);
temperatureStr.replace(" ","");
String dustStr = dtostrf(concentration, 4, 1, buffer);
dustStr.replace(" ","");
String pressureStr = dtostrf(pressure, 4, 1, buffer);
pressureStr.replace(" ","");
String temperature1Str = dtostrf(temperature1, 4, 1, buffer);
temperature1Str.replace(" ","");

//send data to ThingSpeak
updateSensors(luminanceStr, humidityStr, temperatureStr, uvStr, dustStr, pressureStr, temperature1Str);

//wait next sampling cycle
Serial.print("Wait ");
Serial.print(delay_time/1000);
Serial.println("s for next sampling");
Serial.println();
delay(delay_time);

//initialize new cycle
Serial.println();
Serial.print("Sampling (");
Serial.print(sampletime_ms/1000);
Serial.println("s)...");
starttime = millis();
}
}

//Send data to ThingSpeak
void updateSensors(String luminanceStr, String humidityStr, String temperatureStr, String uvStr, String dustStr, String pressureStr, String temperature1Str) {

String cmd = "AT+CIPSTART="TCP","";
cmd += IP;
cmd += "",80";
monitor.println(cmd);
delay(2000);

cmd = GET;
cmd += luminanceStr;
cmd += "&field2=";
cmd += humidityStr;
cmd += "&field3=";
cmd += temperatureStr;
cmd += "&field4=";
cmd += uvStr;
cmd += "&field5=";
cmd += dustStr;
cmd += "&field6=";
cmd += pressureStr;
cmd += "&field7=";
cmd += temperature1Str;
cmd += "\r\n";
delay(1000);
int strsize = cmd.length();
monitor.println("AT+CIPSEND=" + String(strsize));
delay(2000);

monitor.print(cmd);
if(monitor.find("OK")){
Serial.println("Transmission completed with success");
}else{
Serial.println("Transmission failed!");
}
}

void sendDebug(String cmd){
Serial.print("SEND: ");
Serial.println(cmd);
monitor.println(cmd);
}

boolean connectWiFi(){
Serial.println("Connecting wi-fi...");
String cmd ="AT+CWMODE=1";
monitor.println(cmd);
delay(2000);
monitor.flush(); //clear buffer
cmd="AT+CWJAP="";
cmd+=SSID;
cmd+="","";
cmd+=PASS;
cmd+=""";
monitor.println(cmd);
delay(5000);

if(monitor.find("OK")){
Serial.println("Connection succeeded!");
return true;
}else{
Serial.println("Connection failed!");
return false;
}
Serial.println();
}

There are a LOT of DHT libraries. You have apparently installed one that does not match the one used by your sketch. Try a different one.

Im currently using the DHT11. Can you name me a library that I need to use?

Where did you get the code from and what library did they say to use?

Steve

I got the code from the current link.

https://create.arduino.cc/projecthub/igorF2/arduino-uno-mini-weather-station-31b555?ref=platform&ref_id=424_trending__intermediate_tutorial&offset=8

I used the libraries that where mentioned in the explanation by the project creator.

Usually, the type is in capitals, not the variable name.

I already tried doing that. But if I change it it gives me another error: no matching function for call to 'DHT::DHT()'

Well that's an improvement. You need to tell it which pin the DHT is on. Possibly what type it is too. Look at the examples for the library you're using. For the Adafruit version it would be:

DHT dht(DHTPIN, DHTTYPE);

I did that too. I added this two parts at the beginning
#define DHTPIN 2
#define DHTTYPE DHT22

And then I wrote this thing DHT dht(DHTPIN, DHTTYPE);
But I get an error on this part
float temperature = DHT.temperature;

The error I get is this one. expected primary-expression before '.' token

If I change the temperature in dht.temperature it asks me if I meant readTemperature and then If I change that it gives me an error here if I change it in dht from DHT
int chk = dht.read22(DHT22_PIN);

Instead of

float temperature = DHT.temperature;

try

float temperature = dht.temperature;

(i.e. lower-case dht...)

Again it's library dependent but I would expect that to be a function call. It should be called on the dht object too. From Adafruit's example:

float t = dht.readTemperature();

The library referenced by that project is: GitHub - adafruit/DHT-sensor-library: Arduino library for DHT11, DHT22, etc Temperature & Humidity Sensors
The example for that library sys that it also requires:
// - Adafruit Unified Sensor Lib: GitHub - adafruit/Adafruit_Sensor: Common sensor library

Looks like adafruit must have changed the DHT library since that sketch was written. The current examples for that library use this syntax:

#include "DHT.h"

#define DHTPIN 2     // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));

  dht.begin();
}
1 Like

Ok I resolved that issue by changing the DHT to the new libraries. What I'm having trouble with right now is the wifi module. I can't make it work. I try to initialise it and connect him with the wifi by giving him the ssid and password but it keeps failing the if cycle and does not connect. My wifi module is Az Delivery ESP8266.

I try to initialise it as follows and the if cycle is below. But its keeps failing and not connecting

#define SSID "SSID NAME" //replace XXXXX by your router SSID
#define PASS "PASSWORD!" //replace YYYYY by your router password
#define IP "184.106.153.149" // thingspeak.com IP

//communication with wifi module
monitor.flush();
monitor.println("AT");
delay(2000);

if(monitor.find("OK")){
Serial.println("Communication with ESP8266 module: OK");
}
else {
Serial.println("ESP8266 module ERROR");
}

//connect wifi router
connectWiFi();

Serial.print("Sampling (");
Serial.print(sampletime_ms/1000);
Serial.println("s)...");

//initialize timer
starttime = millis();

}

boolean connectWiFi(){
Serial.println("Connecting wi-fi...");
String cmd ="AT+CWMODE=1";
monitor.println(cmd);
delay(2000);
monitor.flush(); //clear buffer
cmd="AT+CWJAP="";
cmd+=SSID;
cmd+="","";
cmd+=PASS;
cmd+=""";
monitor.println(cmd);
delay(5000);

if(monitor.find("OK")){
Serial.println("Connection succeeded!");
return true;
}else{
Serial.println("Connection failed!");
return false;
}
Serial.println();
}

I get no error in the terminal but when I use the serial monitor it fails the if cycle and I get module error

mattiakristo:
But its keeps failing and not connecting

It's too bad that monitor.find() doesn't show what it found instead of "OK". That might be a helpful error message.

johnwasser:
It's too bad that monitor.find() doesn't show what it found instead of "OK". That might be a helpful error message.

And how can I do that? I found this if cycle already in the original code of the Arduino project so I'm trying to understand why it fails the cycle even if there is no compile error

The monitor gives me this answers but I think that the problem is that 1 between barometer ok and module error(which is the wifi problem). I can't understand why is that 1 coming out

Please do us all a favour and post your complete sketch. When you do please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

Try this:

  String result = monitor.readStringUntil('\n');


  if(result.contains("OK")){
    Serial.println("Connection succeeded!");
    return true;
  } else {
    Serial.println("Connection failed!\nError received: \"");
    Serial.print(result);
    Serial.println("\"");
    return false;
  }

in place of this:

  if(monitor.find("OK")){
    Serial.println("Connection succeeded!");
    return true;
  }else{
    Serial.println("Connection failed!");
    return false;
  }

Here is my complete code with all the changes I've done until now. Im attaching the file so you can download it and see for yourself without me risking to use bad formatting etc

codiceArduinoFUNZIA.ino (5.44 KB)

johnwasser:
Try this:

  String result = monitor.readStringUntil('\n');

if(result.contains("OK")){
   Serial.println("Connection succeeded!");
   return true;
 } else {
   Serial.println("Connection failed!\nError received: "");
   Serial.print(result);
   Serial.println(""");
   return false;
 }



in place of this:


if(monitor.find("OK")){
   Serial.println("Connection succeeded!");
   return true;
 }else{
   Serial.println("Connection failed!");
   return false;
 }

I tried it. I had to change contains in indexOf because contains was an error according to Arduino IDE. Now the if cycle tells me connection established but if you see under that message you can see that it tries to reconnect again. So its not really connected. I will post the terminal result below.