#include <DHT.h>
#include <SPIFFS.h>
#define DHT_PIN 15 // Пин, к которому подключен датчик DHT11
DHT dht(DHT_PIN, DHT11);
void setup() {
Serial.begin(115200);
if (!SPIFFS.begin(true)) {
Serial.println("An error occurred while mounting SPIFFS");
return;
}
// Открываем файл для записи
File file = SPIFFS.open("/data.txt", "w");
if (!file) {
Serial.println("There was an error opening the file for writing");
return;
}
while(file.available())Serial.write(file.read ());
// Закрываем файл
file.close();
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Открываем файл для добавления данных
File file = SPIFFS.open("/data.txt", "a");
if (!file) {
Serial.println("There was an error opening the file for appending");
return;
}
// Записываем данные в файл
file.printf("Humidity: %.2f%%, Temperature: %.2f°C\n", humidity, temperature);
// Закрываем файл
file.close();
}