I checked out that tutorial, but I guess I'm just not grasping it all. I am sorry for any inconvenience I have caused. Since the BMP280 is an analog sensor, must the code be a different function in order to write it to the SD Card? I made some changes to my code and added a float function for the pressure sensor, and now I'm not getting any error messages. It says the SD Card initializes and the Serial port data shows up fine, but it is not writing the data to the SD Card. I just want the pressure from the BMP280 sensor to be included in this DHT22 code that works great to write to an SD Card. I also want to add real time clock data from a DS3231 sensor, once I get that sensor in the mail hopefully soon, so any help with that would greatly be appreciated as well. Here is the DHT22 SD Card code that works fine:
#include <stdlib.h>
#include "DHT.h"
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
Serial.println("DHTxx test!");
dht.begin();
}
void loop()
{
// Wait a few seconds between measurements.
delay(60000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Read temperature as Fahrenheit
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;
}
// Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.computeHeatIndex(f, h);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(f);
Serial.println(" *F\t");
// make a string for assembling the data to log:
String dataString = "";
char buffer[10];
String stringH = dtostrf(h,6,2,buffer);
dataString = stringH;
dataString += ", ";
String stringF = dtostrf(f,6,2,buffer);
dataString += stringF;
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
And here is the updated code to try to add pressure data, but as I stated before this code does not write to my SD Card:
#include "BMP280.h"
#include "Wire.h"
#define P0 1013.25
BMP280 bmp;
#include "DHT.h"
#include <SPI.h>
#include <SD.h>
#include <stdlib.h>
#define DHTPIN 2 // what digital pin we're connected to
const int chipSelect = 10;
// Uncomment whatever type you're using!
#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
Serial.println("Test");
bmp.begin();
dht.begin();
bmp.setOversampling(4);
}
void loop()
{
delay(5000);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
double T,P;
char result = bmp.startMeasurment();
if(result!=0){
delay(result);
result = bmp.getTemperatureAndPressure(T,P);
if(result!=0)
{
double A = bmp.altitude(P,P0);
float p = bmp.getTemperatureAndPressure(T,P);
Serial.print("P: ");
Serial.print(P,2);
Serial.print(" mBar, ");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C, ");
Serial.print(f);
Serial.print(" *F, ");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println(" %, ");
}
else {
Serial.println("Error.");
}
}
else {
Serial.println("Error.");
// make a string for assembling the data to log:
String dataString = "";
char buffer[40];
String stringH = dtostrf(h,6,2,buffer);
dataString = stringH;
dataString += ", ";
String stringF = dtostrf(f,6,2,buffer);
dataString += stringF;
dataString += ", ";
String stringP = dtostrf(P,15,5,buffer);
dataString += stringP;
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalogtest.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalogtest.txt");
}
}}