DHT22 datalog

I’ve been trying to assemble a datalogger with a DHT22 sensor and a micro SD module. I’m using a D3231-RTC and a 16x2 lcd do display the data. The idea is to collect data every 30 minutes (or 20, I’m still deciding) and the create weekly and monthly reports. The problem is that i keep getting the following error message:

error: ‘datahora’ does not name a type

datahora = rtc.getDateTime();

^

ReadWrite:53: error: expected declaration before ‘}’ token

}

^

exit status 1
‘datahora’ does not name a type

There are some elements of the code in Portuguese (I’m Brazilian). In this case, datahora is the object I created to store de data from the DS3231-RTC clock. I can’t figure how to solve this problem.

Here is the complete code:

// Real time clock, calendar, temperature, humidity data logger using Arduino, DS3231 and DHT22 sensor V2

#include <Wire.h>
#include <DS3231.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Adafruit_Sensor.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include <SPI.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

#define DHTPIN 8
#define DHTTYPE DHT22
const int chipSelect = 10;

DHT dht(DHTPIN, DHTTYPE);
DS3231 rtc;
RTCDateTime datahora;
File  dataFile;


void setup()
{
  Serial.begin(9600);
  rtc.begin();
  rtc.setDateTime (__DATE__, __TIME__);
  dht.begin();
  lcd.begin(16, 2);

  if (!SD.begin(chipSelect)) {
    Serial.println("erro");
    while (1);
  }
  Serial.println("cartão OK");
}

void loop()
{
    dataFile = SD.open("data.txt", FILE_WRITE);

  }

  String leitura = "";
  float temp = 0;
  float um = 0;

  float t = dht.readTemperature();
  float u = dht.readHumidity();

  datahora = rtc.getDateTime();
}

leitura = String(datahora) + ";" + String(temp) + ";" + String(um) + ";";

if (dataFile) {
  Serial.println(leitura);
  dataFile.println(leitura);
  dataFile.close();
}

delay(1800000);
}

  lcd.setCursor(0, 0);
  lcd.print("T:");
  lcd.print(t);
  lcd.setCursor(7, 0);
  lcd.print(" ");
  lcd.setCursor(8, 0):
  lcd.print("U:");
  lcd.print(u);
  lcd.setCursor(0, 1);
  lcd.print(datahora.hour);
  lcd.setCursor(2, 1);
  lcd.print(":");
  lcd.setCursor(3, 1):
  lcd.print(datahora.minute);
  lcd.setCursor(5, 1);
  lcd.print(" ");
  lcd.setCursor(6, 1);
  lcd.print(datahora.day);
  lcd.setCursor(8, 1);
  lcd.print("/");
  lcd.setCursor(9, 1);
  lcd.print(datahora.month);
  lcd.setCursor(11, 1);
  lcd.print("/");
  lcd.setCursor(12, 1);
  lcd.print(datahora.year);
}

Any help will be welcome!

You have some extra closing curly brackets and a couple of places where there are colons where semi-colons belong. Below is your code marked where the errors are (// *****). I can’t try to compile the code as I don’t have the libraries.

// Real time clock, calendar, temperature, humidity data logger using Arduino, DS3231 and DHT22 sensor V2

#include <Wire.h>
#include <DS3231.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Adafruit_Sensor.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include <SPI.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

#define DHTPIN 8
#define DHTTYPE DHT22
const int chipSelect = 10;

DHT dht(DHTPIN, DHTTYPE);
DS3231 rtc;
RTCDateTime datahora;
File  dataFile;


void setup()
{
  Serial.begin(9600);
  rtc.begin();
  rtc.setDateTime (__DATE__, __TIME__);
  dht.begin();
  lcd.begin(16, 2);

  if (!SD.begin(chipSelect)) {
    Serial.println("erro");
    while (1);
  }
  Serial.println("cartão OK");
}

void loop()
{
  dataFile = SD.open("data.txt", FILE_WRITE);

  //***** }

  String leitura = "";
  float temp = 0;
  float um = 0;

  float t = dht.readTemperature();
  float u = dht.readHumidity();

  datahora = rtc.getDateTime();
  
  //****** }

  leitura = String(datahora) + ";" + String(temp) + ";" + String(um) + ";";

  if (dataFile) {
    Serial.println(leitura);
    dataFile.println(leitura);
    dataFile.close();
  }

  delay(1800000);
  //****** }

  lcd.setCursor(0, 0);
  lcd.print("T:");
  lcd.print(t);
  lcd.setCursor(7, 0);
  lcd.print(" ");
  lcd.setCursor(8, 0);//*****:
  lcd.print("U:");
  lcd.print(u);
  lcd.setCursor(0, 1);
  lcd.print(datahora.hour);
  lcd.setCursor(2, 1);
  lcd.print(":");
  lcd.setCursor(3, 1); //*****:
  lcd.print(datahora.minute);
  lcd.setCursor(5, 1);
  lcd.print(" ");
  lcd.setCursor(6, 1);
  lcd.print(datahora.day);
  lcd.setCursor(8, 1);
  lcd.print("/");
  lcd.setCursor(9, 1);
  lcd.print(datahora.month);
  lcd.setCursor(11, 1);
  lcd.print("/");
  lcd.setCursor(12, 1);
  lcd.print(datahora.year);
}