Hi,
I tried to use the internal RTC of ESP32 to save at fixed intervals data from a SHT31 sensor on a SD card and to display on an LED 1306SSD because whereI’m going to put the ESP32 I have no WiFi so that I cannot use the NTP server. During the intervals the ESP32 is in deepsleep.
This seems to work but all the Serial.print commands I’ve put in the setup and ina callback function are not displayed on the serial monitor.
When the ESP32 awakes I just read on the monitor the following message
rst:0x5 (DEEPSLEEP_RESET), boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip; 0, SPIWP:0xee
clk_drv:ox00, q_drv:0x00, d_drv:0x00, cs0:drv:0x00, hd_drv:0.×00, wp_drv.0x00
mode:DIO, clock div:1
load:0x3fff0030, len:1344
load:0x40078000, len:13924
ho 0 tail 12 room 4
load:0x40080400, len:3600
entry 0x400400805f0
and then a couple of garbage characters ( a question point in a black square and a white square)
I’ve tried to insert delays of different amount after the Serial.begin(115200) and I’ve tried different baud rates but nothing seems to work.
I don’t understand what’s going on.
I’d be very grateful if you could help me
Thanks
Bruno
Hi.
Can you show me the lines of code where you have the serial.prints? And all other related lines of code for the serial monitor?
Regards,
Sara
Hi Sara,
thx for your help.
However, before sending you the code I have to say that I investigated more deeply the problem and I’ve found that with ESP32 even if I try with a very simple code such as
void setup()
{ Serial.begin(115200);
Serial.println(“Just checking if it’s working properly!”);
}
void loop()
{}
I get the sentence truncated and even if I insert after the Serial.begun command
while (!Serial);
and/or insert a delay such as
delay(1000);
and insert the command
Serial.flush(); after the Serial.print (as suggested by one ot the Arduino forumers)
I still get the same mistake while if I put the same command in the loop everything works fine.
I submitted the problem also to teh Arduino forum but the only suggestion I received that seems to work is this strange arrangement
void
loop ()
{Serial.println(“loop”);
delay(2000);
}
void
setup (void)
{
Serial.begin (9600);
#if 1
delay (1000);
#else
while (! Serial);
#endif
Serial.println(“Just checking if it properly works”);
}
If you try this code you’ll get a lot of “loop” lines and also BEFORE these lines the setup serial.print line but if you put the void loop() after the void setup() as it’s usual no serial.print command is displayed on the serial monitor.
It looks like this trick “gains some time” sufficient for serial monitor to display the serial.print command contained in the setup.
I’m afraid that this behaviour is related to the way with which ESP32 uses the USB port but I’m not able to solve it.
However here you have my code
// library for I2C
#include <Wire.h>
//libraries for SD card
#include “FS.h”
#include “SD.h”
#include “SPI.h”
#define SD_CS 5
//library to use internal RTC
#include <ESP32Time.h>
ESP32Time rtc;
// variables to define sleep time; the first multiplies the value to get seconds from microseconds; the second says the sleep time in seconds (600 = 10 minutes)
uint64_t uS_TO_S_FACTOR = 1000000;
uint64_t TIME_TO_SLEEP = 60;
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);
rtc.setTime(40, 07, 12, 21, 5, 2023); // this must be adjusted when compiling the sketch for example this is for 21/05/2023 12:07:40
break;
}
}
// libraries for OLED display
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_ADDR 0x3C
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 1 // Pin 8
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//library for temp & hum sensor
#include <SHT3x.h>
SHT3x Sensor; //This operates in Celsius
float Temperature = Sensor.GetTemperature();
float Humidity = Sensor.GetRelHumidity();
// initializing string data to save data collected from sensors
String Data;
void obtainReadings(){
//Get sensor readings
Temperature = Sensor.GetTemperature();
Humidity = Sensor.GetRelHumidity();
}
// THE FOLLOWING LINES ARE NOT PRINTED IN THE SERIAL MONITOR AND I DO NOT KNOW WHY
void printData ()
{ Serial.print(“temp_SHT= “);
Serial.print(Sensor.GetTemperature());
Serial.print(“hum_SHT= “);
Serial.print(Sensor.GetRelHumidity());
Serial.println(” %”);
Serial.println(rtc.getTime(“%A, %B %d %Y %H:%M:%S”));
}
void displayValues() {
display.setCursor(0,0);
display.print(“T= “);
display.print(Sensor.GetTemperature()); //Celsius
display.println(” C”);
display.print(“H= “);
display.print(Sensor.GetRelHumidity());
display.println(“%”);
display.setCursor(0,20);
display.print(rtc.getTime(“%A, %B %d %Y %H:%M:%S”));
display.setCursor(0,45);
display.print(“going to sleep for 1 min”);
display.display();
}
void data_logging() {
Data = String(Temperature) + “,” + String(Humidity) +”,”+ String(rtc.getTime(“%A, %B %d %Y %H:%M:%S”)) + “\r\n”;
Serial.print(“Save data: “);
Serial.println(Data);
appendFile(SD, “/temperature_readings.txt”, Data.c_str());
}
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();
}
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(1000);
Serial.println(“JUST CHECKING IF IT’S WORKING PROPERLY”); // THIS IS THE TRUNCATED SENTENCE IN THE SERIAL MONITOR WITH GARBAGE CHARACTERS
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128×64)
delay(100);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
//initializing the SHT31 sensor
Sensor.Begin();
Sensor.UpdateData();
// initializing the microSDcard module
if(!SD.begin(5)) {
Serial.println(“Card Mount Failed”);
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE) {
Serial.println(“No SD card attached”);
return;
}
Serial.println(“Initializing SD card…”);
if (!SD.begin()) {
Serial.println(“SD card initialization failed!”);
return;
}
//try to open the data.txt file on the microSD card where the data collected from the sensors will be stored together with time and date
File file = SD.open(“/temperature_readings.txt”);
if(!file) {
Serial.println(“File does not exist”);
Serial.println(“Creating file…”);
writeFile(SD, “/temperature_readings.txt”, “TEMP, HUM, date and time \r\n”);
}
else {
Serial.println(“File exists”);
}
file.close();
// enabling the timer for wakeup when the slleep time defined has ended
print_wakeup_reason();
Serial.println(rtc.getTime(“%A, %B %d %Y %H:%M:%S”)); // (String) returns time with specified format
display.ssd1306_command(SSD1306_DISPLAYON);
// calling the different functions needed to obtain readings from sensors, date and time, writing data to SD card, display trhe readings on the OLED, turn off the OLED
obtainReadings();
printData();
displayValues();
data_logging();
delay(5000);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println(“Going to sleep now for 10 minutes”);
display.ssd1306_command(SSD1306_DISPLAYOFF);
Serial.flush();
esp_deep_sleep_start();
}
// there is nothing in the loop because the ESP32 stops BEFORE since it is sleeping
void loop() {
}
Thanks for your patience and for your help
Best
Bruno
Hi.
That’s a very weird problem.
I don’t think that happened to me before. I never had issues with displaying something in the Serial monitor with the ESP32.
Which ESP32 model do you have? Which Arduino IDE version are you using?
Hi Sara,
The Arduino IDE I’m using is 2.1.0 and I checked three different ESP32 ( in Arduino IDE I’m using them as ESP32 Dev Module) but the very strange fact is that TODAY Serial monitor is working as expected and all the Serial.print commands I put in the setup are properly displayed but two conditions seem needed to be met:
- serial momitor must be active when uploading
- after the Serial.begin(115200) command i have to insert the command
#if 1
delay (1000);
#else
while (! Serial);
#endif
If one of these conditions is not met the lines are not printed.
Which is your idea about this strange behaviour ?
Thx again an have a nice day
Bruno