#include "DHT.h"
#include "SPI.h"
#include "SD.h"
#define DHTPIN 32
#define DHTTYPE DHT22
File DataFile;
DHT dht(DHTPIN, DHTTYPE);
void writeFile(const char *path, const char *message)
{
DataFile = SD.open(path, FILE_WRITE);
if (DataFile) {
Serial.printf("Writing to %s ", path);
Serial.printf(message);
DataFile.close();
Serial.print("complete");
}
else {
Serial.printf("unable to open SD card");
}
}
void ReadFile(const char * path){
// open the file for reading:
DataFile = SD.open(path);
if (DataFile) {
Serial.printf("Reading file from %s\n", path);
// read from the file until there's nothing else in it:
while (DataFile.available()) {
Serial.write(DataFile.read());
}
DataFile.close(); // close the file:
}
else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void readDHT22() {
delay(2000);
// The DHT22 returns at most one measurement every 2s
float h = dht.readHumidity();
// Reads the humidity in %
float t = dht.readTemperature();
// Reads the temperature in degrees Celsius
float f = dht.readTemperature(true);
// true returns the temperature in Fahrenheit
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed reception"));
return;
// Returns an error if the ESP32 does not receive any measurements
}
Serial.print("Humidite: ");
Serial.print(h);
Serial.print("% Temperature: ");
Serial.print(t);
Serial.print("°C, ");
Serial.print(f);
Serial.println("°F");
}
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
readDHT22();
}