Hi ZinggJM
I'm writing again because, after having successfully displayed the data collected from temp and hum sennsor together with time stamp on the eink display I met another problem that I was not able to solve even after many trials.
The problem is: how can I connect a SD card module to my ESP32 while the eink display is connected with SDA to 23, SCL to 18, CS to 5, D/C to 0, RES to 2, BUSY to 15 ?
I tried to define CS, SCK, MOSI and MISO for the SD card as 33, 25, 26, 32 but I encountered in one case a "Stack smashing protect failure" with continuous rebooting and after some changes in the cose now simply the message "Card Mount failed".
In any case I was not able to make the SD card work.
I know that ESP32 has a VSPI and a HSPI with GPIO 13 GPIO 12 GPIO 14 GPIO 15 for HSPI and GPIO 23 GPIO 19 GPIO 18 GPIO 5 for VSPI ( MOSI MISO SCLK CS) but the eink is already connected to pin 5 for CS and to pin 15 for BUSY. so I was forced to define "custom SPI pins" that however in my hands do not work.
If you can help me I'd be very grateful.
Thx for your patience and attention but as a newbie as I am I'm completely lost.
I'm enclosing the NOT WORKING code even if compiling and uploading are OK
#include <SPI.h>
#include <Wire.h>
#include <SD.h> // library for the SD card
#define SD_CS 33
#define SD_SCK 25
#define SD_MISO 32
#define SD_MOSI 26
// Create a file to store the data
File myFile;
#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
// 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
#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;
// 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 = 180; //sleep for 10 minutes
String dataMessage;
// 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()
{
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 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 15 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(100);
// Initialize SD card
if (!SD.begin(SD_CS)){
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
}
//initializing E-paper display
display.init(115200,true,50,false);
delay(3000);
//initializing BME280
bme.begin(0x76);
temp = bme.readTemperature();
hum= bme.readHumidity();
// checking the BME280 connections and initializing the sensor
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
//initializing the SHT31 sensor
Sensor.Begin();
Sensor.UpdateData();
TEMP = Sensor.GetTemperature();
HUM = Sensor.GetRelHumidity();
//Initialize DS3231
Serial.println("Initialize DS3231");
while(!Serial);
delay(100);
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__)));
}
Read_TempHum();
logSDCard();
SDwriting();
displayReadings();
delay(3000);
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(4000);
esp_deep_sleep_start();
}
void logSDCard() {
DateTime now = rtc.now();
char timeStamp[9];
sprintf(timeStamp, "%02i:%02i:%02i", now.hour(), now.minute(), now.second());
dataMessage = String(sensor_data) + "," + String(timeStamp) + "," +
String(temp) + "," + String(hum)+ "," + String(TEMP)+","+ String(HUM) +"\r\n";
Serial.print("Save data: ");
Serial.println(dataMessage);
}
void SDwriting() {
DateTime now = rtc.now();
char dateString[11];
sprintf(dateString, "%04i-%02i-%02i ", now.year(), now.month(), now.day());
char timeString[9];
sprintf(timeString, "%02i:%02i:%02i", now.hour(), now.minute(), now.second());
myFile = SD.open("DATA.txt", FILE_WRITE);
if (myFile) {
myFile.print(dataMessage);
myFile.close();
}
}
void loop()
{}