-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SD card not working when using T5_V2.3.1_213] #46
Comments
Please copy your sketch here |
Hi,
sorry for the long delay in answering your request but I was in hospital for a programmed surgery that luckily went fine.
Here you have the sketch that doesn't work for what attains the SD card since on the serial monitor I get the message "opening file failed" and "appending file failed" and no data are saved on the SD
When I use this sketch ( clearly with needed modifications) wth the TFT Lilygo board it works just fine.
Thx for your hekp
Bruno
#include <SD.h>
#include <FS.h>
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h> // for the RTC
RTC_DS3231 rtc;
#include <GxFont_GFX.h>
#include <GxEPD2.h>
#include <GxGDEM0213B74/GxGDEM0213B74.h> // specific gor the Lilygo eink display model I have
#include <Fonts/FreeMonoBold9pt7b.h>
#include <GxIO/GxIO_SPI/GxIO_SPI.h>
#include <GxIO/GxIO.h>
#define SPI_MOSI 23
#define SPI_MISO -1
#define SPI_CLK 18
#define ELINK_SS 5
#define ELINK_BUSY 4
#define ELINK_RESET 16
#define ELINK_DC 17
#define SDCARD_SS 13
#define SDCARD_CLK 14
#define SDCARD_MOSI 15
#define SDCARD_MISO 2
GxIO_Class io(SPI, /*CS=5*/ ELINK_SS, /*DC=*/ ELINK_DC, /*RST=*/ ELINK_RESET);
GxEPD_Class display(io, /*RST=*/ ELINK_RESET, /*BUSY=*/ ELINK_BUSY);
SPIClass sdSPI(VSPI);
bool sdOK = false;
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
#include <SHT3x.h>
SHT3x Sensor;
// variables to store data from sensors ( float type because they are value with a decimal part)
float TEMP;
float HUM;
float temp;
float hum;
String timeStamp;
String Data;
// variables to define sleep time; the first multiplies the value to get seconds from microseconds; the second says the sleep time in seconds (900 = 15 minutes)
uint64_t uS_TO_S_FACTOR = 1000000;
uint64_t TIME_TO_SLEEP = 900; //sleep for 15 minutes
String dataMessage;
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
// setting a counter for data readings; it will increase every time data from sensors are collected
RTC_DATA_ATTR int sensor_data = 0;
#define sensor_data(temp,hum,TEMP,HUM);
// to get time from RTC
void gettimeStamp() {
DateTime now = rtc.now();
char timeStamp[9];
sprintf(timeStamp, "%02i:%02i:%02i", now.hour(), now.minute(), now.second());
Serial.println(timeStamp);
}
// to print on serial monitor data from sensors
void Read_TempHum()
{
Serial.print("Temp_BME280 = ");
Serial.print(temp);
Serial.println(" C");
Serial.print("Hum_BME280 = ");
Serial.print(hum);
Serial.println(" %");
Serial.print("Temp_SHT3x = ");
Serial.print(TEMP);
Serial.println(" C");
Serial.print("Hum_SHT3X = ");
Serial.print(HUM);
Serial.println(" %");
Serial.print(timeStamp);
}
//to display on Eink display data
void displayReadings(){
DateTime now = rtc.now();
char timeStamp[9];
sprintf(timeStamp, "%02i:%02i:%02i", now.hour(), now.minute(), now.second());
display.setCursor(0,0);
display.setTextColor(GxEPD_BLACK);
display.setCursor(0,20);
display.print("temp_BME = ");
display.print(temp);
display.println(" C");
display.print("temp_SHT = ");
display.print(TEMP);
display.println(" C");
display.print("hum_BME = ");
display.print(hum);
display.println(" %");
display.print("hum_SHT = ");
display.print(HUM);
display.println(" %");
display.setCursor(0,100);
display.print(timeStamp);
display.setCursor(100,100);
display.print("Run n ");
display.println(sensor_data);
display.setCursor(0,115);
display.print("Now sleep for 15 min");
display.update();
}
// to save data on SDcard
void logSDCard() {
DateTime now = rtc.now();
char timeStamp[9];
sprintf(timeStamp, "%02i:%02i:%02i", now.hour(), now.minute(), now.second());
dataMessage = String(timeStamp) + "\r\n";
dataMessage = String(timeStamp) + "," +
String(temp) + "," + String(hum)+ "," + String(TEMP) + "," + String(HUM) +"\r\n";
Serial.print("Save data: ");
Serial.println(dataMessage);
appendFile(SD, "/data.txt", dataMessage.c_str());
}
// Write to the SD card
void writeFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file) {
Serial.println("Failed to open file for writing");
return;
}
if(file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}
// Append data to the SD card
void appendFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file) {
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)) {
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
void setup()
{
Serial.begin(115200);
while(!Serial);
delay(100);
// checking the BME280 connections and initializing the sensor
bool status;
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
//initializing SD card
sdSPI.begin(SDCARD_CLK, SDCARD_MISO, SDCARD_MOSI, SDCARD_SS);
if (!SD.begin(SDCARD_SS, sdSPI)) {
sdOK = false;
} else {
sdOK = true;
}
Serial.println("SDcard initialized!!");
delay(500);
File file = SD.open("/data.txt");
if(!file) {
Serial.println("File doens't exist");
Serial.println("Creating file...");
writeFile(SD, "/data.txt", "Time, Temperature, Humidity,TEMP,HUM, \r\n");
}
else {
Serial.println("File already exists");
}
file.close();
//initializing the SHT31 sensor
Sensor.Begin();
Sensor.UpdateData();
TEMP = Sensor.GetTemperature();
HUM = Sensor.GetRelHumidity();
delay(100);
// setup for the RTC
if(! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
else {
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
temp = bme.readTemperature();
hum = bme.readHumidity();
SPI.begin(SPI_CLK, SPI_MISO, SPI_MOSI, ELINK_SS);
display.init(); // enable diagnostic output on Serial
display.setRotation(1);
display.fillScreen(GxEPD_WHITE);
display.setTextColor(GxEPD_BLACK);
display.setFont(&FreeMonoBold9pt7b);
display.setCursor(0, 0);
delay(1500);
display.update();
Read_TempHum();
displayReadings();
logSDCard();
// increasing the counter for the next data readings
sensor_data++;
Serial.println("Sensor data logged successfully! Going to sleep");
// ESP32 going to sleep for the amount of time you decided
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
delay(5000);
esp_deep_sleep_start();
delay(2000);
esp_deep_sleep_start();
}
void loop()
{}
… Il 27/09/2023 14:20 CEST Lewis He ***@***.***> ha scritto:
Please copy your sketch here
—
Reply to this email directly, view it on GitHub #46 (comment), or unsubscribe https://github.com/notifications/unsubscribe-auth/A5BIYK3VSGVXAB4ORXIHVV3X4QK2DANCNFSM6AAAAAA5IIF23I.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
It seems that there is no problem with the SD part. Has the SD card been formatted in FAT32 format? Is the capacity less than 32G? Have you replaced any other SD tests? |
Hi, thx for your prompt feedback. |
Use the factory test file to test to see if it is normal @bruno1950 |
Hi, Thanks in advance for your help. The board would be perfect IF the SD card worked |
You have burned the wrong firmware, you should have burned You should use the compiled binary file for testing, first to rule out whether there is a hardware problem |
Hi,
I just bought a Lilygo T5 V2.3.1.213 version GDEM0213B74 CH9102 and I was able to show on display data from I2C sensors (BME280 and SHT3x) and to get a timestamp from RTC DS3231. Deepsleep also works fine. However, I was not able to use the SD card, neither inserting it directly in the slot nor connected to an external SDcard reader ( MOSI IO15,MISO IO2, SCK IO14, CS IO13). The card is correctly mounted but I keep on getting an error "opening the file" or ""appending the file". Even using a vey simple sketch just to try if it's possible to write something on the SD card is unsuccesful. Since I think that Lilygo is a good brand I have to assume that it's my fault but I was not able to find my mistake. Could anyone tell me which code I have to use just to write data on SD card in order to use it as a datalogger ? Thx in advance
The text was updated successfully, but these errors were encountered: