Hi, i am trying to turn of the display when esp32 goes to sleep. but it's not doing it.
Not sure what's the issue. i am reading the pin sate before going to sleep and it shows it's 0.
here is the code
#include <WiFi.h>
#include "time.h"
#define sleepPeriod 10000000ul // 10 seconds sleep
#define OFFSET 0 // us (tient compte du temps d'exécution de updateTime)
#include <ESP32Time.h>
unsigned long DELAY_TIME = 10000; // 6 sec
unsigned long delayStart = 0; // the time the delay started
ESP32Time rtc;
RTC_DATA_ATTR byte bootCount = 0;
RTC_DATA_ATTR time_t now;
RTC_DATA_ATTR uint64_t Mics = 0;
RTC_DATA_ATTR struct tm * timeinfo;
#include <Adafruit_ST7789.h>
// For LCD
#define TFT_CS 14
#define TFT_RST 15
#define TFT_DC 32
// For buttons and others
const int LITE = 33;
const int UP = 2;
const int DOWN = 16;
const int TOUCH = 5;
const int BATTERY_M = 34;
int pageNum = 0;
// For LCD
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
void watchFace() {
if (!digitalRead(LITE)) {
digitalWrite(LITE, HIGH);
}
}
void secondFace() {
if (!digitalRead(LITE)) {
digitalWrite(LITE, HIGH);
}
}
void notificationFace() {
if (!digitalRead(LITE)) {
digitalWrite(LITE, HIGH);
}
}
void settingsFace() {
if (!digitalRead(LITE)) {
digitalWrite(LITE, HIGH);
}
}
void movePage() {
if(pageNum<0 || pageNum >3){
pageNum=0;
}
if(pageNum == 1){
secondFace();
Serial.println("secondFace");
delay(500);
} else if( pageNum == 2){
notificationFace();
Serial.println("notificationFace");
delay(500);
} else if (pageNum == 3){
settingsFace();
Serial.println("settingsFace");
delay(500);
} else {
watchFace();
Serial.println("watchFace");
delay(500);
}
}
void printLocalTime() {
time(&now);
timeinfo = localtime (&now);
Serial.printf ("%s\n", asctime(timeinfo));
delay(2); // 26 bytes @ 115200 baud is less than 2 ms
}
void updateTime (uint64_t elapsedTime) { // elapsedTime in micro second
if (elapsedTime == 0) Mics += micros();
else Mics += elapsedTime;
if (Mics > 1000000) {
Mics = Mics % 1000000;
now += Mics / 1000000;
}
}
void sommeil (unsigned long chrono) {
updateTime (sleepPeriod - (micros() - chrono) + OFFSET);
// esp_sleep_enable_timer_wakeup(sleepPeriod - (micros() - chrono));
esp_sleep_enable_ext0_wakeup(GPIO_NUM_35,1); //1 = High, 0 = Low
//If you were to use ext1, you would use it like
//esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);
//Go to sleep now
Serial.println("Going to sleep now");
esp_deep_sleep_start();
}
void setup()
{
Serial.begin(115200);
delayStart = millis();
pinMode(TOUCH, INPUT_PULLUP);
pinMode(UP, INPUT_PULLUP);
pinMode(DOWN, INPUT_PULLUP);
pinMode(BATTERY_M, INPUT);
pinMode(LITE, OUTPUT);
// Set Switch State
digitalWrite(LITE, HIGH);
tft.init(240, 280); // Init ST7789 240x240
tft.setTextWrap(false);
tft.fillScreen(ST77XX_BLACK);
if (bootCount == 0) { // First boot
rtc.setTime(30, 24, 21, 20, 2, 2022);
printLocalTime();
unsigned long chrono = micros();
bootCount++;
}
updateTime (0); // Mise à jour pour utilisation pendant l'éveil
/*
* do what you want here
*/
watchFace();
printLocalTime(); // This sets the internal clock
}
void loop() {
unsigned long chrono = micros();
/* power off the display after 6s */
if ((millis() - delayStart) >= DELAY_TIME) {
digitalWrite(LITE, LOW);
delay(100);
//turn off display
Serial.println(digitalRead(LITE));
Serial.println("ded");
// go to sleep
sommeil (chrono);
}
if (!digitalRead(UP)) {
Serial.println("UP_BTN");
delayStart = millis();
pageNum=0;
Serial.println(pageNum);
movePage();
}
if (!digitalRead(DOWN)) {
Serial.println("UP_DOWN");
delayStart = millis();
pageNum=pageNum+1;
Serial.println(pageNum);
movePage();
}
}
here is the circuit