error message on DHT22/OLED sketch

Hello I have been sitting here for many hours trying to find the problems with this sketch and my libraries and was hoping someone here can help

I have the DHT22 sensor working fine and printing to serial monitor but I would like to display it on an OLED

the sketch I settled on could not find the library but I beileive I have resolved that, I do not think thislibrary was intended for this sketch, that said I cannot find any alternatives, renaming it seemed to do the trick though

changed #include <dht.h> to #include <DHT.h>

full code is here

/******************************************************************************
OLED DHT22 Humidity/Temperature Display using U8GLIB Library

visit https://code.google.com/p/u8glib/ for full details of the U8GLIB library and
full instructions for use.

by Chris Rouse Oct 2015

DHT22 portions Credit Rob Tillaart
https://github.com/RobTillaart/Arduino/tree/master/libraries/DHTlib

Using a IIC 128x64 OLED with DHT22
 
DHT22
Humidity/Temperature Sensor

Wire OLED:
  VCC +5v
  GND GND
  SDA Analog pin 4
  SCL Analog pin 5
Wire DHT22
  Vcc to Arduino 5 volts
  Gnd to Arduino Gnd
  Data to Arduino pin 2

******************************************************************************/

// Add libraries
  #include "U8glib.h"
  #include <SPI.h>
  #include <Wire.h>
  #include <DHT.h> 
  
// setup u8g object
  U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);	// I2C 
//
  dht DHT;
  #define DHT22_PIN 2
  struct
 {
   uint32_t total;
   uint32_t ok;
   uint32_t crc_error;
   uint32_t time_out;
   uint32_t connect;
   uint32_t ack_l;
   uint32_t ack_h;
   uint32_t unknown;
 } stat = { 0,0,0,0,0,0,0,0};  
  
//
  int humidity;
  double temperature;
  double temperatureF;
  int dewPoint1;
  String thisTemp = "";
  String thisHumidity = "";
  String thisDewPoint = "";
//

void draw(void) {
  u8g.setFont(u8g_font_profont12);
  u8g.drawStr(29,10, "DHT22 Sensor");
  u8g.setFont(u8g_font_profont12); 
  // display Centigrade
  thisTemp = String(temperature) + "\260C";
  const char* newTempC = (const char*) thisTemp.c_str();
  u8g.drawStr(70,25, newTempC);
  // display Fahrenheit
  temperatureF = 1.8 * temperature + 32;
  thisTemp = String(temperatureF) + "\260F";
  const char* newTempF = (const char*) thisTemp.c_str();
  u8g.drawStr(15,25, newTempF);  
  
 // now display Humidity
  u8g.setFont(u8g_font_profont29); 
  thisHumidity = String(humidity) + "%";
  const char* newHumidity = (const char*) thisHumidity.c_str();
  u8g.drawStr(15,50, newHumidity);  
  u8g.setFont(u8g_font_profont12);
  u8g.drawStr(65,38, "humidity");
  
 // now display the dew point
  thisDewPoint = String(dewPoint1) + "\260C";
  const char* newDewPoint = (const char*) thisDewPoint.c_str();
  u8g.drawStr(85,50, newDewPoint);  
  u8g.setFont(u8g_font_profont12);
  u8g.drawStr(65,50, "DP="); 
}

void setup(void) {
  Serial.begin(9600);
  Wire.begin();
  Serial.println("DHT22");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
}

void loop(void) {
  uint32_t start = micros();
  int chk = DHT.read22(DHT22_PIN);
  uint32_t stop = micros();
  stat.total++;
  switch (chk)
  {
    case DHTLIB_OK:
      stat.ok++;
      Serial.print("OK,\t");
      break;
    case DHTLIB_ERROR_CHECKSUM:
      stat.crc_error++;
      Serial.print("Checksum error,\t");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      stat.time_out++;
      Serial.print("Time out error,\t");
      break;
    case DHTLIB_ERROR_CONNECT:
      stat.connect++;
      Serial.print("Connect error,\t");
      break;
    case DHTLIB_ERROR_ACK_L:
      stat.ack_l++;
      Serial.print("Ack Low error,\t");
      break;
    case DHTLIB_ERROR_ACK_H:
      stat.ack_h++;
      Serial.print("Ack High error,\t");
      break;
    default:
      stat.unknown++;
      Serial.print("Unknown error,\t");
      break;
  }
  temperature = DHT.temperature;
  humidity = DHT.humidity;
  dewPoint(temperature,humidity);  
  // picture loop
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  // delay needed between sensor reads
  delay(2000);
}

double dewPoint(double celsius, double humidity)
{
  double a = 17.271;
  double b = 237.7;
  double temp = (a * celsius) / (b + celsius) + log(humidity/100);
  dewPoint1 = (b * temp) / (a - temp);
}

there is a problem here dht DHT; but I do not understand what the issue is, I presume this is what is creating all the othere errors

I also seem to have picked up multiple dht.h libraries, how can I consolidate these?

here are the multiple errors I am getting

DHT22HumiditySensorwithOLEDDisplay:38: error: 'dht' does not name a type

dht DHT;

^

C:\Users\danbl\Documents\Arduino\DHT22HumiditySensorwithOLEDDisplay\DHT22HumiditySensorwithOLEDDisplay.ino: In function 'void setup()':

DHT22HumiditySensorwithOLEDDisplay:97: error: 'DHT_LIB_VERSION' was not declared in this scope

Serial.println(DHT_LIB_VERSION);

^

C:\Users\danbl\Documents\Arduino\DHT22HumiditySensorwithOLEDDisplay\DHT22HumiditySensorwithOLEDDisplay.ino: In function 'void loop()':

DHT22HumiditySensorwithOLEDDisplay:103: error: expected primary-expression before '.' token

int chk = DHT.read22(DHT22_PIN);

^

DHT22HumiditySensorwithOLEDDisplay:108: error: 'DHTLIB_OK' was not declared in this scope

case DHTLIB_OK:

^

DHT22HumiditySensorwithOLEDDisplay:112: error: 'DHTLIB_ERROR_CHECKSUM' was not declared in this scope

case DHTLIB_ERROR_CHECKSUM:

^

DHT22HumiditySensorwithOLEDDisplay:116: error: 'DHTLIB_ERROR_TIMEOUT' was not declared in this scope

case DHTLIB_ERROR_TIMEOUT:

^

DHT22HumiditySensorwithOLEDDisplay:120: error: 'DHTLIB_ERROR_CONNECT' was not declared in this scope

case DHTLIB_ERROR_CONNECT:

^

DHT22HumiditySensorwithOLEDDisplay:124: error: 'DHTLIB_ERROR_ACK_L' was not declared in this scope

case DHTLIB_ERROR_ACK_L:

^

DHT22HumiditySensorwithOLEDDisplay:128: error: 'DHTLIB_ERROR_ACK_H' was not declared in this scope

case DHTLIB_ERROR_ACK_H:

^

DHT22HumiditySensorwithOLEDDisplay:137: error: expected primary-expression before '.' token

temperature = DHT.temperature;

^

DHT22HumiditySensorwithOLEDDisplay:138: error: expected primary-expression before '.' token

humidity = DHT.humidity;

^

Multiple libraries were found for "DHT.h"
Used: C:\Users\danbl\Documents\Arduino\libraries\DHT_sensor_library
Not used: C:\Users\danbl\Documents\Arduino\libraries\DHT-sensor-library-master
Using library U8glib at version 1.19.1 in folder: C:\Users\danbl\Documents\Arduino\libraries\U8glib
Using library SPI at version 1.0 in folder: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.10.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\SPI
Using library Wire at version 1.0 in folder: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.10.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\Wire
Using library DHT_sensor_library at version 1.3.0 in folder: C:\Users\danbl\Documents\Arduino\libraries\DHT_sensor_library
Using library Adafruit_Unified_Sensor at version 1.0.2 in folder: C:\Users\danbl\Documents\Arduino\libraries\Adafruit_Unified_Sensor
exit status 1
'dht' does not name a type

amny thanks for any assistance you can offer

Dan

DHT dht;

  int chk = dht.read22(DHT22_PIN);etc,etc

Dan0:
I do not think thislibrary was intended for this sketch, that said I cannot find any alternatives, renaming it seemed to do the trick though

The answer is right there in the comment of the code you posted:

Dan0:
DHT22 portions Credit Rob Tillaart
Arduino/libraries/DHTlib at master · RobTillaart/Arduino · GitHub

thanks Pert, i did download the .h and .cpp files from that link and create a lirary, actually did it a few times after deleting and trying other libraries

the error message is probably not from using that library so did it again this morning and had multiple errors again

when I get home tonight I will start over and see where I get to.

my inital attemp to post here contained more information on the project but when my browser hung and I lost everything I had typed my subsiquent post was missing some detail (mainly because I was tired and fed up with using the very small keyboard and mouse of a GPD Pocket at midnight)

the project is a simple temperature and humidity measuing device, I was using the DHT11 but found it to be innacurate (+/-2DegC as stated in the datasheet) so swapped to the DHT22 which seems ok, it was printing to serial just fine. now I want to add the OLED and incorporate it all into a 3D printed desk ornament.

as an aside could I have the sensor and OLED sketches on different tabs is advised or of any benefit?

Dan

normally I have no problems managing libraries but for the case above I am lost

if I go to that GitHub link I see the .h and .cpp files, I cannot find out how to save these to libraries, if I copy them into a sketch and save to a folder of the same name (not actually sure what name to use in this case either) then the file saves as an ino type file even though the file extension is .h or.cpp

is this correct? it doesn't look like the other files which open in notepad

I have been ok downloading zip files and importing them, this is a different process which I cannot find any information on, perhaps everyone thinks this is obvious...

please assist if you can with clear steps

regards

Dan

I have tried to save these as text files and the compiler does seem to move further along but I still get errors with DHT not being defined

I presume I have the latest version of the sketch and the correct libraries now, I deleted all other libraries that contained DHT

this is my file structure (pictures somewhere below)

sketch (original)

/******************************************************************************

OLED DHT22 Humidity/Temperature Display using U8GLIB Library



visit https://code.google.com/p/u8glib/ for full details of the U8GLIB library and

full instructions for use.



by Chris Rouse Oct 2015



DHT22 portions Credit Rob Tillaart

https://github.com/RobTillaart/Arduino/tree/master/libraries/DHTlib



Using a IIC 128x64 OLED with DHT22

 

DHT22

Humidity/Temperature Sensor



Wire OLED:

  VCC +5v

  GND GND

  SDA Analog pin 4

  SCL Analog pin 5

Wire DHT22

  Vcc to Arduino 5 volts

  Gnd to Arduino Gnd

  Data to Arduino pin 2



******************************************************************************/



// Add libraries

  #include "U8glib.h"

  #include <SPI.h>

  #include <Wire.h>

  #include <dht.h> 

  

// setup u8g object

  U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C 

//

int  dht;

  #define DHT22_PIN 2

  struct

 {

   uint32_t total;

   uint32_t ok;

   uint32_t crc_error;

   uint32_t time_out;

   uint32_t connect;

   uint32_t ack_l;

   uint32_t ack_h;

   uint32_t unknown;

 } stat = { 0,0,0,0,0,0,0,0};  

  

//

  int humidity;

  double temperature;

  double temperatureF;

  int dewPoint1;

  String thisTemp = "";

  String thisHumidity = "";

  String thisDewPoint = "";

//



void draw(void) {

  u8g.setFont(u8g_font_profont12);

  u8g.drawStr(29,10, "DHT22 Sensor");

  u8g.setFont(u8g_font_profont12); 

  // display Centigrade

  thisTemp = String(temperature) + "\260C";

  const char* newTempC = (const char*) thisTemp.c_str();

  u8g.drawStr(70,25, newTempC);

  // display Fahrenheit

  temperatureF = 1.8 * temperature + 32;

  thisTemp = String(temperatureF) + "\260F";

  const char* newTempF = (const char*) thisTemp.c_str();

  u8g.drawStr(15,25, newTempF);  

  

 // now display Humidity

  u8g.setFont(u8g_font_profont29); 

  thisHumidity = String(humidity) + "%";

  const char* newHumidity = (const char*) thisHumidity.c_str();

  u8g.drawStr(15,50, newHumidity);  

  u8g.setFont(u8g_font_profont12);

  u8g.drawStr(65,38, "humidity");

  

 // now display the dew point

  thisDewPoint = String(dewPoint1) + "\260C";

  const char* newDewPoint = (const char*) thisDewPoint.c_str();

  u8g.drawStr(85,50, newDewPoint);  

  u8g.setFont(u8g_font_profont12);

  u8g.drawStr(65,50, "DP="); 

}



void setup(void) {

  Serial.begin(9600);

  Wire.begin();

  Serial.println("DHT22");

  Serial.print("LIBRARY VERSION: ");

//  Serial.println(DHT_LIB_VERSION);

  Serial.println();

}



void loop(void) {

  uint32_t start = micros();

  int chk = DHT.read22(DHT22_PIN);

  uint32_t stop = micros();

  stat.total++;

  switch (chk)

  {

    case DHTLIB_OK:

      stat.ok++;

      Serial.print("OK,\t");

      break;

    case DHTLIB_ERROR_CHECKSUM:

      stat.crc_error++;

      Serial.print("Checksum error,\t");

      break;

    case DHTLIB_ERROR_TIMEOUT:

      stat.time_out++;

      Serial.print("Time out error,\t");

      break;

    case DHTLIB_ERROR_CONNECT:

      stat.connect++;

      Serial.print("Connect error,\t");

      break;

    case DHTLIB_ERROR_ACK_L:

      stat.ack_l++;

      Serial.print("Ack Low error,\t");

      break;

    case DHTLIB_ERROR_ACK_H:

      stat.ack_h++;

      Serial.print("Ack High error,\t");

      break;

    default:

      stat.unknown++;

      Serial.print("Unknown error,\t");

      break;

  }

  temperature = DHT.temperature;

  humidity = DHT.humidity;

  dewPoint(temperature,humidity);  

  // picture loop

  u8g.firstPage();  

  do {

    draw();

  } while( u8g.nextPage() );

  // delay needed between sensor reads

  delay(2000);

}



double dewPoint(double celsius, double humidity)

{

  double a = 17.271;

  double b = 237.7;

  double temp = (a * celsius) / (b + celsius) + log(humidity/100);

  dewPoint1 = (b * temp) / (a - temp);

}

and error messages

C:\Users\danbl\Documents\Arduino\TempHumidityOLEDDan6\TempHumidityOLEDDan6.ino: In function 'void loop()':

TempHumidityOLEDDan6:205: error: 'DHT' was not declared in this scope

   int chk = DHT.read22(DHT22_PIN);

             ^

Using library U8glib at version 1.19.1 in folder: C:\Users\danbl\Documents\Arduino\libraries\U8glib 
Using library SPI at version 1.0 in folder: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.10.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\SPI 
Using library Wire at version 1.0 in folder: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.10.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\Wire 
Using library dht in folder: C:\Users\danbl\Documents\Arduino\libraries\dht (legacy)
exit status 1
'DHT' was not declared in this scope

well and truly stuck now

Dan

Capture1.PNG

Capture2.PNG

Capture3.PNG

It's pretty unconventional to have a class name in lowercase and an instance of that class in uppercase.

Instances are normally lowercase.
(I don't have your libraries, so this may not be true in your case)

Dan0:
as an aside could I have the sensor and OLED sketches on different tabs is advised or of any benefit?

The tabs (.ino files) are concatenated before compilation so there is no functional difference between your sketch having one single .ino file or multiple files. The benefit of breaking it into multiple tabs is that it makes it much easier to navigate through your code instead of having to scroll through thousands of lines in a single sketch to find the code you're looking for.

Dan0:
if I go to that GitHub link I see the .h and .cpp files, I cannot find out how to save these to libraries, if I copy them into a sketch and save to a folder of the same name (not actually sure what name to use in this case either) then the file saves as an ino type file even though the file extension is .h or.cpp

is this correct? it doesn't look like the other files which open in notepad

I have been ok downloading zip files and importing them, this is a different process which I cannot find any information on, perhaps everyone thinks this is obvious...

please assist if you can with clear steps

This is a little bit more complicated since that repository contains many libraries. Most often people will use a separate repository for each library, which makes it easier to install. Even so, it's easy enough:

  • Go to the home page of the repository: GitHub - RobTillaart/Arduino: Arduino libraries, code and applications
  • Click "Download" or "Clone or Download" button (which one you see depends on whether you are logged in to GitHub).
  • Click "Download ZIP".
  • Unzip the downloaded file.
  • From the unzipped folder, copy the folder "libraries/DHTlib" to "{your sketchbook folder}/libraries". you can find the location of {your sketchbook folder} in the Arduino IDE at File > Preferences > Sketchbook location.

This is what's known as a "manual library installation".

Dan0:
I have tried to save these as text files and the compiler does seem to move further along but I still get errors with DHT not being defined

That's because you didn't define it.

Add this line near the top of your sketch but after you #include dht.h:

dht DHT;

If you add it after your current line 76:

int  dht;

you'll have an error (because of the conflict between the variable named dht and DHTlib's class name dht. Since you don't use the dht variable anywhere you might want to just delete that line.

After installing the library and adding that line your code compiles for me.

AWOL:
It's pretty unconventional to have a class name in lowercase and an instance of that class in uppercase.

That's how it's done in DHTlib's examples:

awsome advice thank you, I will register to Github then I should see the download button :slight_smile:

these are the little things that make this learning process somewhat longer and more complicated

many thanks

Dan

You don't need to register. Just go look at that page and you'll see the download button.

pert:
You don't need to register. Just go look at that page and you'll see the download button.

I didnt see a download button on that page

Dan0:
I didnt see a download button on that page

OK operator error, if I click on the tab again I get the download button appear, will try again later, sorry for being a Div

after much downloading, unzipping and general fiddling about the sketch now uploads with no errors

unfortunately it doesnt work......

A night of checking the circuitry and OLED for me :frowning:

thanks all for the help on the programming

Dan