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);
}
-
is it a Wenos D1 mini or a NodeMcu? don't mix numbers and D labels for pins in one sketch. arduino.stackexchange.com/questions/75704/…Juraj– Juraj ♦2022年08月02日 16:59:55 +00:00Commented Aug 2, 2022 at 16:59
-
It is a NodeMcuMereics– Mereics2022年08月02日 17:03:17 +00:00Commented Aug 2, 2022 at 17:03
-
io 5 is D1. it is a good choice for the CS pin, but then you have to wire the SD card's CS pin there, not elsewhere. but D1 and D2 are default pins for I2C (Wire) so I guess you have a sensor wired to D1/D2.Juraj– Juraj ♦2022年08月02日 17:06:54 +00:00Commented Aug 2, 2022 at 17:06
1 Answer 1
You're calling
if (SD.begin(5)){
GPIO5 is not necessarily D5
. Use the D5
constant; it will evaluate to whatever GPIO number D5
actually is. So,
if (SD.begin(D5)){
Although you don't appear to be using the variable, you also have:
int chipSelect = 5;
which should be
int chipSelect = D5;
The difference between GPIO numbers and pin labels leads to problems for a lot of people. GPIOx
is specific to the ESP8266 - it corresponds to a pin number and function on the actual chip; Dx
is specific to the particular breakout board and is not always the same GPIO number (or the same between breakout boards).
-
It is doing the exact same thing. Like I said, the file is getting written onto the SD Card, but if the sd card is getting initialized, the sensors stop working.Mereics– Mereics2022年08月02日 15:52:39 +00:00Commented Aug 2, 2022 at 15:52
-
Are you sure you selected the correct board?romkey– romkey2022年08月02日 16:34:12 +00:00Commented Aug 2, 2022 at 16:34
-
1if it is a Wenos D1 or a NodeMcu it doesn't make sense to use D5 as CS, because D5 is SPI CLK pin.2022年08月02日 17:01:40 +00:00Commented Aug 2, 2022 at 17:01
-
1Nevermind, I figured it out. I used D8 instead of 5, and now it is working. I got tricked because the SD Card was working and D5 too, and I thought that wasn't the problem. Thank you, guys! Cheers!Mereics– Mereics2022年08月02日 17:11:33 +00:00Commented Aug 2, 2022 at 17:11
-
1@Mereics, io 15 (D8) has to be LOW at boot. the use as CS pins can interfere2022年08月02日 18:33:37 +00:00Commented Aug 2, 2022 at 18:33