expected primary-expression before '.' token

I am working on an assignment for school but I keep running into this issue no matter what i try.

Here's the code:

#include <SPI.h>
#include <SD.h>
#include <DHT.h>
#include <DHT_U.h>


#include "DHT.h"

#define dhtPin 12            // data pin


#define dhtType DHT11        // DHT 11

DHT dht(dhtPin, dhtType);    // Initialiseer de DHT bibliotheek

float humidityVal;           // luchtvochtigheid
float tempValC;              // temperatuur in graden Celcius
float heatIndexC;            // gevoelstemperatuur in graden Celcius

const int chipSelect = 4;

void setup() {
  Serial.begin(9600);        // stel de seriële monitor in
  dht.begin();               // start het DHT sensor uitlezen
  
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop() {

  humidityVal = dht.readHumidity();        // vraag de luchtvochtigheid aan de DHT sensor
  tempValC = dht.readTemperature();        // vraag de temperatuur in graden Celcius aan de DHT sensor

  // Controleer of alle waarden goed zijn uitgelezen, zo niet probeer het opnieuw
  if (isnan(humidityVal) || isnan(tempValC)){
    Serial.println("Uitlezen van DHT sensor mislukt!");

    // Beëindig de loop() functie
    return;
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

 if (dataFile) {
    dataFile.println(DHT.temperature);
    dataFile.println(DHT.humidity);
    dataFile.close();
    // print to the serial port too:
  Serial.print(humidityVal);
  Serial.print("\t");
  Serial.print(tempValC);
  Serial.print("\n");
  delay(2000);
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
 }
}

It shows that the error occurs in this line:

dataFile.println(DHT.humidity);

I do not understand why this happens as I am very new to coding if someone can help me out I'd appreciate it.

Thanks in advance.

DHT and dht are not the same. Also, most DHT libraries I know don't have a public 'temperature' and 'humidity' variable.

Also missing a matching '}' for "if (datafile) {". It should be just before the 'else' clause.

You probably want to use the variables you set at the top of loop():

    dataFile.println(tempValC);
    dataFile.println(humidityVal);

i tried substituting humidity for humidityVal however it still shows me the same error.

Did you try the suggestions in replies #1 and #2?

Post your new code.

If you make changes, post ALL your code. Don't make us guess!

But if I have to guess, if you substitute just 'humidity' by 'humidityVal' you end up with

dataFile.println(DHT.humidityVal);

Which is:
a) still wrong
b) not what johnwasser showed you.

And if it's the only thing you did you still have the bug from reply #2...

I copied johnwasser's bit of code and that eventually did the trick.

This is my code now:

#include <DHT.h>
#include <DHT_U.h>

#include <DHT.h>
#include <DHT_U.h>

#include <SPI.h>
#include <SD.h>

#include "DHT.h"             // Bibliotheek voor DHT sensoren

#define dhtPin 12  // data pin


#define dhtType DHT11        // DHT 11
//#define dhtType DHT22      // DHT 22  (AM2302), AM2321
//#define dhtType DHT21      // DHT 21 (AM2301)

const int chipSelect = 4;

DHT dht(dhtPin, dhtType);    // Initialiseer de DHT bibliotheek

float humidityVal;           // luchtvochtigheid
float tempValC;              // temperatuur in graden Celcius
float tempValF;              // temperatuur in graden Fahrenheit
float heatIndexC;            // gevoelstemperatuur in graden Celcius
float heatIndexF;            // gevoelstemperatuur in graden Fahrenheit

void setup() {
  Serial.begin(9600);        // stel de seriële monitor in
  dht.begin();               // start het DHT sensor uitlezen

  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop() {

  humidityVal = dht.readHumidity();        // vraag de luchtvochtigheid aan de DHT sensor
  tempValC = dht.readTemperature();        // vraag de temperatuur in graden Celcius aan de DHT sensor
  tempValF = dht.readTemperature(true);    // vraag de temperatuur in graden Fahrenheit aan de DHT sensor

  // Controleer of alle waarden goed zijn uitgelezen, zo niet probeer het opnieuw
  if (isnan(humidityVal) || isnan(tempValC) || isnan(tempValF)) {
    Serial.println("Uitlezen van DHT sensor mislukt!");

    // Beëindig de loop() functie
    return;
  }

  // Bereken de gevoelstemperatuur in graden Celcius
  heatIndexC = dht.computeHeatIndex(tempValC, humidityVal, false);

  // Bereken de gevoelstemperatuur in graden Fahrenheit
  heatIndexF = dht.computeHeatIndex(tempValF, humidityVal);

  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  if (dataFile) {
        dataFile.println(tempValC);
    dataFile.println(humidityVal);
    dataFile.close();

    // Print alle waarden naar de seriële monitor
    // \t print een tab, \n print een nieuwe regel karakter
    Serial.print(humidityVal);
    Serial.print("\t");
    Serial.print(tempValC);
    Serial.print("\n");

    delay(2000);
  }

  else {
    Serial.println("error opening datalog.txt");
  }
}

This code is correct however it now has problems with reading the sensor and the sd card.

here's a look at the serial monitor:

You're repeating yourself.
You're repeating yourself.
You're repeating yourself. :wink:

And why are you interest in degree F as a Dutch? :wink:

I'm not interested in Fahrenheit but i didn't write the code and when i tried to take it out it only created problems so i just left it.

Do you have an idea as to what the problem is regarding the sensor and sd card not working?

Doubt that is the output from that piece of code... Unless you also implemented the Google Translate API....

But let's start with how you connected it. Schematic (please NO Fritzing breadboard mess, hand drawing will do) and a photo of the setup for starters :slight_smile:

here is a photo of the setup and a drawing that i made:

Pin 12, that's a bit of a problem when using a SD card. An SD card uses SPI and pin 12 is SPI MISO. Try a pin that's not used :wink:

PS When drawing a schematic, I could not care less about the breadboard :wink: But I do care about the pin numbers and function of the sensor :slight_smile:

Okay thanks for the advice i will definetly try the sensor on a different pin :slight_smile: