Hi,
some weeks ago I submitted the same issue to this forum and I was not able to solve my problem, even if one of the forumers (ZinggJM) spent some time trying to help me.
The problem is that I am an old (73y) newbie and some problems that for you are trivial for me are just ... a problem
However, in these weekis I was able to arrange a datalogger with ESP32, two temp/hum sensors and a RTC (DS3231) in order to save data on a SD card in the following systems:
a) an ESP32 comnnected to SSD 1306 or SH1107;
b) a TTGO-Tdisplay-S3;
c) an ESP32 with integrated OLED display ( even ìf it just works when coded with micropython).
However, when I tried to use an E-ink display I was not able to save data on SD card.
In fact, both using the Lilygo-T5 Eink-display board and a 2.13" display from WeAct Studio connected with ESP32 I always get some errors even if the SD card is initialized and the display correctly shows the data from sensors and from RTC.
The error that I get is "Failed to open file for appending".
I also tried to contact people from Lilygo and I sent my code but they said that there was no error in the code.
So, at the present moment, I'm just stuck
Thanks in advance for your precious help
My code is
#include <SPI.h> //for the SD card
#include <Wire.h>
#include "FS.h"
#include <SD.h> // for the SD card
#define SDCARD_SS 33
#define SDCARD_CLK 25
#define SDCARD_MOSI 26
#define SDCARD_MISO 27
SPIClass sdSPI(VSPI);
bool sdOK = false;
#include <RTClib.h> // for the RTC
RTC_DS3231 rtc;
//libraries for e-paper display
#define ENABLE_GxEPD2_GFX 0
#include <GxEPD2_BW.h>
#include <GxEPD2_3C.h>
#include <Fonts/FreeMonoBold9pt7b.h>
// ESP32 SS=5,SCL(SCK)=18,SDA(MOSI)=23,BUSY=15,RST=2,DC=0 wiring to ESP32 for E_paper as requested by manufacturer
// 2.13'' EPD Module
GxEPD2_3C<GxEPD2_213_Z98c, GxEPD2_213_Z98c::HEIGHT> display(GxEPD2_213_Z98c(/*CS=5*/ 5, /*DC=*/ 0, /*RST=*/ 2, /*BUSY=*/15)); // GDEY0213Z98 122x250, SSD1680
//libraries for BME280 sensor
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
//library for SHT3x sensor
#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;
// variables to define sleep time; the first multiplies the value to get seconds from microseconds; the second says the sleep time in seconds (1200 = 30 minutes)
uint64_t uS_TO_S_FACTOR = 1000000;
uint64_t TIME_TO_SLEEP = 300; //sleep for 5 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;
// initializing string data to save data collected from sensors
String Data;
#define sensor_data(temp,hum,TEMP,HUM);
void gettimeStamp() {
DateTime now = rtc.now();
char timeStamp[9];
sprintf(timeStamp, "%02i:%02i:%02i", now.hour(), now.minute(), now.second());
Serial.println(timeStamp);
}
void Read_TempHum()
{
TEMP = Sensor.GetTemperature();
HUM = Sensor.GetRelHumidity();
DateTime now = rtc.now();
char timeStamp[9];
sprintf(timeStamp, "%02i:%02i:%02i", now.hour(), now.minute(), now.second());
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(" %");
}
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(sensor_data) + ","+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 (DON'T MODIFY THIS FUNCTION)
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 (DON'T MODIFY THIS FUNCTION)
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 displayReadings() {
DateTime now = rtc.now();
char timeStamp[9];
sprintf(timeStamp, "%02i:%02i:%02i", now.hour(), now.minute(), now.second());
display.init(115200,true,50,false);
display.setRotation(1);
display.setFont(&FreeMonoBold9pt7b);
display.setTextColor(GxEPD_BLACK);
display.setFullWindow();
display.firstPage();
do
{
display.fillScreen(GxEPD_WHITE);
display.setCursor(0,10);
display.print("temp_BME= ");
display.print(temp);
display.println(" C");
display.setCursor(0,30);
display.print("temp_SHT= ");
display.print(TEMP);
display.println(" C");
display.setCursor(0,50);
display.print("hum_BME= ");
display.print(hum);
display.println(" %");
display.setCursor(0,70);
display.print("hum_SHT= ");
display.print(HUM);
display.println(" %");
display.setCursor(0,90);
display.setTextColor(GxEPD_RED);
display.print("sleeping for 5 min");
display.setCursor(0,110);
display.print("# ");
display.print(sensor_data);
display.print(" ");
display.print(timeStamp);
}
while(display.nextPage());
delay(100);
display.hibernate();
}
void setup() {
//initializing Serial monitor
Serial.begin(115200);
while(!Serial);
delay(1000);
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
bme.begin(0x76);
temp = bme.readTemperature();
hum= bme.readHumidity();
// checking the BME280 connections and initializing the sensor
//initializing the SHT31 sensor
Sensor.Begin();
Sensor.UpdateData();
TEMP = Sensor.GetTemperature();
HUM = Sensor.GetRelHumidity();
//Initialize DS3231
Serial.println("Initialize DS3231");
// setup for the RTC
while(!Serial);
delay(1000);
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__)));
}
//initializing SD card
sdSPI.begin(SDCARD_CLK, SDCARD_MISO, SDCARD_MOSI, SDCARD_SS);
if (!SD.begin((33), sdSPI)) {
sdOK = false;
Serial.println("Card Mount Failed");
} else {
sdOK = true;
Serial.println("SDcard initialized!!");
}
File file = SD.open("/data.txt");
if(!file) {
Serial.println("File doens't exist");
Serial.println("Creating file...");
writeFile(SD, "/data.txt", "dataMessage \r\n");
}
else {
Serial.println("File already exists");
}
file.close();
display.init(115200,true,50,false);
delay(1000);
Read_TempHum();
gettimeStamp();
logSDCard();
delay(3000);
displayReadings();
delay(4000);
display.hibernate();
// 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(3000);
esp_deep_sleep_start();
}
void loop() {