I have an ESP8266 wich is connected to an SDCard module, an SHT21 sensor and an BMP280. The idea is that I want to log data from the sensors to the SD Card, but if I initialize the SD Card with SD.begin(chipSelect), all the data from sensors become NaN or random values. If I initialize it, it is logging only NaN on the sd card, so the card is working, and if I dont initialize it, I get good and normal values into the serial monitor. The Card Module is connected to D5,D6,D7,D8, and the two sensors are connected to D1 and D2, and all powered from 3v. Any idea? This is the code:
#include <Wire.h>
#include <SPI.h>
#include <Sodaq_SHT2x.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp;
#include <SD.h>
File myFile;
float pressure;
float temperature;
float altimeter;
float humidity;
float dewPoint;
float temperature2;
float humidity2;
float dewPoint2;
int chipSelect = 5;
float QNH = 1015;
const int BMP_address = 0x76;
int ledB = D4;
int ledG = D3;
int ledR = D0;
void setup()
{
Serial.println("initialization done.");
Wire.begin();
Serial.begin(9600);
bmp.begin(BMP_address);
Serial.println("PTHBox Logger");
//Checking if SDCard is alive
if (SD.begin(5)){
Serial.println("SD card is alive");
}else{
Serial.println("SD card is ded");
while(1); //halt program
}
//Checking the logfiles from the SDCard and deleting them
if (SD.exists("/csv.txt"))
{
Serial.println("Removing csv.txt");
SD.remove("/csv.txt");
Serial.println("csv.txt is fresh and ready to log");
}
// Writing headers to csv.txt
myFile = SD.open("/csv.txt", FILE_WRITE);
if (myFile) // it opened OK
{
Serial.println("Writing headers to csv.txt");
myFile.println("Altitude,Pressure,Temperature,Humidity,DewPoint");
myFile.close();
Serial.println("Headers written");
}else {
Serial.println("Error opening csv.txt");
}
// Setting pinModes for the LED
pinMode(D0, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
//Set PWM frequency 500, default is 1000
//Set range 0~100, default is 0~1023
analogWriteFreq(500);
analogWriteRange(100);
}
void loop()
{
// Testing the led
analogWrite(ledR, 100);
analogWrite(ledG, 0);
analogWrite(ledB, 60);
Serial.print("Humidity(%RH): ");
Serial.print(SHT2x.GetHumidity());
Serial.print(" Temperature(C): ");
Serial.print(SHT2x.GetTemperature());
Serial.print(" Dewpoint(C): ");
Serial.print(SHT2x.GetDewPoint());
pressure = bmp.readPressure()/100; //and conv Pa to hPa
Serial.print(" BMP PRESSURE");
Serial.print(pressure);
Serial.print(" BMP ALT");
Serial.println(bmp.readAltitude (QNH));
Serial.print(" BMP TEMP");
Serial.print(bmp.readTemperature());
// Assigning values to variables
pressure = bmp.readPressure()/100; //and conv Pa to hPa
temperature = (bmp.readTemperature()); //temperature from bmp
altimeter = bmp.readAltitude (QNH); //QNH is local sea lev pressure
humidity = (SHT2x.GetHumidity());
dewPoint = (SHT2x.GetDewPoint());
temperature2 = (SHT2x.GetTemperature());
// Writing the values to SDCARD
myFile = SD.open("/csv.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile)
{
Serial.println("Writing to csv.txt");
myFile.print(altimeter);
myFile.print(pressure);
myFile.print(temperature);
myFile.print(humidity);
myFile.println(dewPoint);
myFile.close();
}
else
{
Serial.println("error opening csv.txt");
}
delay(1000);
}