Esp8266 + am2301 + sd card

Hi,
I am using this code and it is working well, but the problem is that the sensor doesn’t measure the real temperature and humidity. Also on SD Card are written just one time.

Why is happening this ?


#include “DHTesp.h”
#ifdef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY!)
#error Select ESP8266 board.
#endif
DHTesp dht;

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

File myFile;

void setup() {

float humidity = dht.getHumidity();
float temperature = dht.getTemperature();

// Open serial communications and wait for port to open:
Serial.begin(115200);
dht.setup(16, DHTesp::DHT11);

Serial.print(“Initializing SD card…”);

if (!SD.begin(4)) {
Serial.println(“initialization failed!”);
return;
}
Serial.println(“initialization done.”);

myFile = SD.open(“test.txt”, FILE_WRITE);

if (myFile) {
Serial.print(“Writing to test.txt…”);
myFile.print(dht.getStatusString());
myFile.println(“Lageshtia e ajrit:”);
//myFile.println("\t");
myFile.println(humidity, 1);
myFile.println("\t\t");
myFile.println(“Temperatura:”);
// myFile.println("\t");
myFile.println(temperature, 1);
delay (2000);

myFile.close();
Serial.println("done.");

} else {

Serial.println("error opening test.txt");

}

myFile = SD.open(“test.txt”);
if (myFile) {
Serial.println(“test.txt:”);

while (myFile.available()) {
  Serial.write(myFile.read());
}

myFile.close();

} else {

Serial.println("error opening test.txt");

}
}

void loop() {

delay(2000);
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
Serial.print(dht.getStatusString());
Serial.print("\t");
Serial.print(“Lageshtia e ajrit:”);
Serial.print("\t");
Serial.print(humidity, 1);
Serial.print("\t\t");
Serial.print(“Temperatura:”);
Serial.print("\t");
Serial.println(temperature, 1);

}

i hope you understand what was the mistake in your code and don’t just copy-paste the code:-

#include "DHTesp.h"
#ifdef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY!)
#error Select ESP8266 board.
#endif
DHTesp dht;

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

File myFile;

unsigned long lastMillis = 0;


void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  dht.setup(16, DHTesp::DHT11);

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

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");



}

void loop() {

  if (millis() - lastMillis > 10000) {
    lastMillis = millis();

    float humidity = dht.getHumidity();
    float temperature = dht.getTemperature();
    Serial.print(dht.getStatusString());
    Serial.print("\t");
    Serial.print("Lageshtia e ajrit:");
    Serial.print("\t");
    Serial.print(humidity, 1);
    Serial.print("\t\t");
    Serial.print("Temperatura:");
    Serial.print("\t");
    Serial.println(temperature, 1);

    myFile = SD.open("test.txt", FILE_WRITE);
    if (myFile) {
      Serial.print("Writing to test.txt…");
      myFile.print(dht.getStatusString());
      myFile.println("Lageshtia e ajrit:");
      //myFile.println("\t");
      myFile.println(humidity, 1);
      myFile.println("\t\t");
      myFile.println("Temperatura:");
      // myFile.println("\t");
      myFile.println(temperature, 1);
      myFile.close();
      Serial.println("done.");
    }
  }
}

Same problem again. The sensor doesn’t measure the real temperature and humidity.

For example these is shown humidity 4 % and temperature 2.

is it DHt11 or DHT22??

It’s AM2301.

i am note sure if the library you used supports DHt21. Try this code:-

#include <DHT.h>

#define DHTPIN D4     // Digital pin connected to the DHT sensor

#define DHTTYPE DHT21   // DHT 21 (AM2301)



DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor.

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {

float temp = dht.readTemperature();

float hum = dht.readHumidity();

Serial.println(temp);
Serial.println(hum);
}

Yes, with this code the sensor is measuring the real temperature and humidity.

What do i need to change to my code to include this library ?

replace the new DHT working code with your old not working DHT code.