Expected unqualified-id before '.' token (DHT11)

I'm trying to read the temp, humidity, and pressure from a DHT11 and BMP280, but I'm getting this error message. What should I do?

The code:

#include <Adafruit_Sensor.h>
#include <SPI.h>               //include Serial Peripheral Interface library
#include <Wire.h>              //include Two Wire Interface library
#include <Adafruit_GFX.h>      // include Adafruit graphics library
#include <Adafruit_ST7735.h>   // include Adafruit ST7735 TFT library
#include <Adafruit_BMP280.h>   // include Adafruit BMP280 sensor library
#include "DHT.h"               //include DHTXX sensor library
#define DHTPIN 2               //DHT11 data pin connected to arduino pin D2
#define DHTTYPE DHT11          //specifying the type of DHT sensor used
#define TFT_RST   8            // TFT RST pin is connected to arduino pin D8
#define TFT_CS    10           // TFT CS  pin is connected to arduino pin D9
#define TFT_DC    9            // TFT DC  pin is connected to arduino pin D10
// initialize ST7735 SERIES TFT library
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

// define device I2C address: 0x76 or 0x77 (0x77 is the library default address)
#define BMP280_I2C_ADDRESS  0x76

Adafruit_BMP280  bmp280;       // initialize Adafruit BMP280 library
int chk;

float temp;

float hum;
void setup(void)
{
  dht.begin()                                         // synchronizing DHT sensor
  tft.initR(INITR_144GREENTAB);                        // initialize a ST7735S chip, black tab
  tft.fillScreen(ST77XX_BLACK);                        // setting black background
  tft.drawFastHLine(0, 15 ,  tft.width(), ST77XX_CYAN);// draw horizontal seperation line at position (0, 15)

  tft.setTextColor(ST77XX_CYAN, ST77XX_BLACK);         // set text color to white and black background
  tft.setTextSize(1);                                  // setting text size to 1
  //tft.setCursor(4, 0);                               // move cursor to position (4, 0) pixel
  //tft.print("ARDUINO + ST7735 TFT");
  tft.setCursor(25, 5);                                // move cursor to position (25, 5) pixel
  tft.print("WEATHER BUDDY");

  // initialize the BMP280 sensor
  if ( bmp280.begin(BMP280_I2C_ADDRESS) == 0 )
  { // connection error or device address wrong!
    tft.setTextColor(ST77XX_RED, ST77XX_CYAN);         // set text color to red and black background
    tft.setTextSize(2);                                // setting text size to 2
    tft.setCursor(5, 76);                              // move cursor to position (5, 76) pixel
    tft.print("Connection");
    tft.setCursor(35, 100);                            // move cursor to position (35, 100) pixel
    tft.print("Error");
    while (1); // stay here
  }

  tft.drawFastHLine(0, 55,  tft.width(), ST77XX_CYAN);  // draw horizontal seperation line at position (0, 55)pixel
  tft.drawFastHLine(0, 95,  tft.width(), ST77XX_CYAN);  // draw horizontal seperation line at position (0, 195)pixel
  tft.setTextColor(ST77XX_RED, ST77XX_BLACK);           // set text color to red and black background
  tft.setCursor(30, 20);                                // move cursor to position (30, 20) pixel
  tft.print("TEMPERATURE ");                            // setting heading for first section
  tft.setTextColor(ST77XX_CYAN, ST77XX_BLACK);          // set text color to cyan and black background
  tft.setCursor(40, 60);                                // move cursor to position (40, 60) pixel
  tft.print("HUMIDITY ");                               // setting heading for second section
  tft.setTextColor(ST77XX_GREEN, ST7735_BLACK);         // set text color to green and black background
  tft.setCursor(40, 100);                               // move cursor to position (40, 100) pixel
  tft.print("PRESSURE ");                               // setting heading for third section
  tft.setTextSize(2);                                   // setting text size to 2
}

// main loop
void loop()
{

  char _buffer[8];
  // read temperature, humidity and pressure from the BMP280 sensor
  float temp = bmp280.readTemperature();    // get temperature in °C
  float hum = dht.readHumidity();           // get humidity in rH%
  float pres = bmp280.readPressure();       // get pressure in hPa

  // print temperature (in °C)
  if (temp < 0)                             // if temperature < 0
    sprintf( _buffer, "-%02u.%02u", (int)abs(temp), (int)(abs(temp) * 100) % 100 );
  else                                      // if temperature >= 0
    sprintf( _buffer, " %02u.%02u", (int)temp, (int)(temp * 100) % 100 );// setting the value approximation
  tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK);  // set text color to yellow and black background
  tft.setCursor(11, 34);                          // move cursor to position (11,34) pixel
  tft.print(_buffer);                             // print temperature from BMP-280 sensor
  tft.drawCircle(89, 34, 2, ST77XX_YELLOW);       // print the degree symbol ( ° )(can be omitted if * is used instead)
  tft.setCursor(95, 34);                          // move cursor to position (95,34) pixel
  tft.print("C");                                 // print the Celcius symbol

  // 2: print humidity (in %)
  sprintf( _buffer, "%02u ", (int)(hum));          // setting the value approximation
  tft.setTextColor(ST77XX_MAGENTA, ST77XX_BLACK);  // set text color to magenta and black background
  tft.setCursor(45, 74);                           // move cursor to position (45,74) pixel
  tft.print(_buffer);                              // print humidity from DHT-11 sensor
  tft.setCursor(75, 74);                           // move cursor to position (75,74) pixel
  tft.print("%");                                  // print the percentage symbol


  // 3: print pressure (in hPa)
  sprintf( _buffer, "%04u.%02u", (int)(pres / 100), (int)((uint32_t)pres % 100) ); // setting the value approximation
  tft.setTextColor(ST77XX_ORANGE, ST77XX_BLACK);  // set text color to orange and black background
  tft.setCursor(3, 112);                          // move cursor to position (3,112)pixel
  tft.print(_buffer);                             // print atmospheric pressure from BMP-280
  tft.setCursor(91, 112);                         // move cursor to position (91,112)pixel
  tft.print("hPa");                               // print unit of atmospheric pressure as hecto pascal

  delay(1000);                                    // wait 1 second before taking next sensor reading

}

The error:

D:\Downloads\sketch_nov24a\sketch_nov24a.ino: In function 'void setup()':
sketch_nov24a:27:6: error: expected unqualified-id before '.' token
   dht.begin()                                         // synchronizing DHT sensor
      ^
D:\Downloads\sketch_nov24a\sketch_nov24a.ino: In function 'void loop()':
sketch_nov24a:72:18: error: expected primary-expression before '.' token
   float hum = dht.readHumidity();           // get humidity in rH%
                  ^
Using library Adafruit_Sensor-1.1.4 at version 1.1.4 in folder: C:\Users\xxxx\OneDrive\Documents\Arduino\libraries\Adafruit_Sensor-1.1.4 
Using library SPI at version 1.0 in folder: D:\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.51.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\SPI 
Using library Wire at version 1.0 in folder: D:\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.51.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\Wire 
Using library Adafruit-GFX-Library-1.10.12 at version 1.10.12 in folder: C:\Users\xxxx\OneDrive\Documents\Arduino\libraries\Adafruit-GFX-Library-1.10.12 
Using library Adafruit-ST7735-Library-1.7.5 at version 1.7.5 in folder: C:\Users\xxxx\OneDrive\Documents\Arduino\libraries\Adafruit-ST7735-Library-1.7.5 
Using library Adafruit_BMP280_Library-2.4.2 at version 2.4.2 in folder: C:\Users\jxxxx\OneDrive\Documents\Arduino\libraries\Adafruit_BMP280_Library-2.4.2 
Using library Adafruit_BusIO at version 1.9.7 in folder: C:\Users\xxxx\OneDrive\Documents\Arduino\libraries\Adafruit_BusIO 
exit status 1
expected unqualified-id before '.' token

Where is the

dht DHT;

?

Better yet look at some example code for the DHT.

How to use the DHT11 with Arduino - Code, Circuit Diagram, Video Tutorial (techzeero.com)

I added the
DHT dht(DHTPIN, DHTTYPE);
and now I get this:

C:\Users\xxxx\AppData\Local\Temp\cc8JrtsE.ltrans0.ltrans.o: In function `global constructors keyed to 65535_0_sketch_nov24a.ino.cpp.o.4294':
<artificial>:(.text.startup+0x1ce): undefined reference to `DHT::DHT(unsigned char, unsigned char, unsigned char)'
C:\Users\xxxx\AppData\Local\Temp\cc8JrtsE.ltrans0.ltrans.o: In function `setup':
D:\Downloads\sketch_nov24a/sketch_nov24a.ino:29: undefined reference to `DHT::begin(unsigned char)'
C:\Users\xxxx\AppData\Local\Temp\cc8JrtsE.ltrans0.ltrans.o: In function `loop':
D:\Downloads\sketch_nov24a/sketch_nov24a.ino:74: undefined reference to `DHT::readHumidity(bool)'
collect2.exe: error: ld returned 1 exit status

You forgot to initialize an instance for your DHT sensor: see how I inserted the
line 'DHT dht(DHTPIN, DHTTYPE); '^above in the excerpt of your code

1 Like

After I added that in, I got this:

C:\Users\xxxx\AppData\Local\Temp\cclEKYDs.ltrans0.ltrans.o: In function `global constructors keyed to 65535_0_sketch_nov24a.ino.cpp.o.4294':
<artificial>:(.text.startup+0x1ce): undefined reference to `DHT::DHT(unsigned char, unsigned char, unsigned char)'
C:\Users\xxxx\AppData\Local\Temp\cclEKYDs.ltrans0.ltrans.o: In function `setup':
D:\Downloads\sketch_nov24a/sketch_nov24a.ino:29: undefined reference to `DHT::begin(unsigned char)'
C:\Users\xxxx\AppData\Local\Temp\cclEKYDs.ltrans0.ltrans.o: In function `loop':
D:\Downloads\sketch_nov24a/sketch_nov24a.ino:74: undefined reference to `DHT::readHumidity(bool)'
collect2.exe: error: ld returned 1 exit status

maybe try


#include <DHT.h>

instead of


#include "DHT.h"

so the "" instead of the <> is the difference between referring to a file vs referring to a library

did it work?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.