ESP8266 Project : DHT11 readings stored on an SD card

In this particular example we are going to store temperature readings from a DHT11 on an sd card we will be using a Wemos mini and a variety of shields which make it easy to create projects like this with no wiring.

Lets take a look a the shields and boards that are required

 

Image Summary
The Wemos mini – ESP8266 based board, it comes with various headers. This is the beauty of it you can create stackable projects with the board and pin compatible shields
This shield features the DHT sensor, there are actually a couple of variations of this sensor which use slightly different sensors. The original was a dht11 and the newer one is a DHT12
This allows you to use micro sd cards in your projects, The shield uses the SPI bus pins
This is simply a base, you plug the Wemos Mini into one side and you can plug a shield or shields into the other side

 

 

 

Parts List

1 x Wemos Mini
1 x Wemos Dual Base
1 x Micro SD shield
1 x DHT shield (DHt11)

I connect the Wemos Mini to the dual base and then put the SD card shield along side this, I then place the DHT shield on top of the Wemos Mini.

 

Code

Various libraries required – you can install these via the library manager, here are links to them

https://github.com/adafruit/DHT-sensor-library
https://github.com/adafruit/Adafruit_Sensor

[codesyntax lang=”cpp”]

#include <SPI.h>
#include <SD.h>
#include “DHT.h”

#define DHTPIN D4 // what pin we’re connected to
#define DHTTYPE DHT11 // DHT 11

const int chipSelect = D8;
File myFile;
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);

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

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

}

void loop()
{
myFile = SD.open(“tempdata.csv”, FILE_WRITE);//change the file name if you want
delay(2000);

float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f))
{
Serial.println(“Failed to read from DHT sensor!”);
return;
}

//debug
Serial.print(“Humidity: “);
Serial.print(h);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(t);
Serial.print(” *C “);
Serial.print(f);
Serial.print(” *F\t”);
// if the file opened okay, write to it:
if (myFile)
{
Serial.print(“Writing to tempdata.csv…”);
myFile.print(h);
myFile.print(“,”);
myFile.print(t);
myFile.print(“,”);
myFile.println(f);
// close the file:
myFile.close();
Serial.println(“done.”);
}
else
{
// if the file didn’t open, print an error:
Serial.println(“error opening tempdata.csv”);
}

}

[/codesyntax]

 

Again if you don’t want to type all of this in then you can download at wemos_sdcard_dht11

 

Output

Here is the output from the serial monitor and there will also be data on the sd card

 

Initializing SD card…initialization done.
Humidity: 19.00 % Temperature: 20.00 *C 68.00 *F Writing to tempdata.csv…done.
Humidity: 19.00 % Temperature: 20.00 *C 68.00 *F Writing to tempdata.csv…done.
Humidity: 19.00 % Temperature: 20.00 *C 68.00 *F Writing to tempdata.csv…done.
Humidity: 19.00 % Temperature: 20.00 *C 68.00 *F Writing to tempdata.csv…done.
Humidity: 20.00 % Temperature: 20.00 *C 68.00 *F Writing to tempdata.csv…done.
Humidity: 20.00 % Temperature: 20.00 *C 68.00 *F Writing to tempdata.csv…done.
Humidity: 19.00 % Temperature: 21.00 *C 69.80 *F Writing to tempdata.csv…done.
Humidity: 19.00 % Temperature: 23.00 *C 73.40 *F Writing to tempdata.csv…done.
Humidity: 19.00 % Temperature: 23.00 *C 73.40 *F Writing to tempdata.csv…done.
Humidity: 19.00 % Temperature: 23.00 *C 73.40 *F Writing to tempdata.csv…done.

 

Links

 

Smart Electronics D1 mini – Mini NodeMcu 4M bytes development board based ESP8266 by WeMos

Dual Base Expansion board for WeMos D1 mini NodeMCU ESP8266

DHT Shield for WeMos D1 mini DHT11 Single-bus digital temperature and humidity sensor module sensor

Micro SD Card Shield IoT Wireless Control for D1 Mini ESP8266 WiFi WeMos Module

Related posts

Creating an infrared thermometer with a Wemos Mini and a MLX90614

ESP8266 based TMP175 temperature display project

ESP8266 MPL3115A2 pressure sensor project