Hi , i try to bring my microcontroller to deep sleep for 10 second on and 10 second off . However , when i put the deep sleep mode , the microcontroller seem not go into do the task of charge.cpp. It just on for 10seconds and off for 10 seconds.
Reason I want deep sleep is ;
To put the microcontroller in save power mode .
main code:
#include <Arduino.h>
#include "charge.h"
#define uS_TO_S_FACTOR 1000000ULL
#define TIME_TO_SLEEP 100
charge battery ;
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32!");
//set timer0 interrupt at 2kHz
battery.begin();
battery.check_voltage();
esp_sleep_enable_timer_wakeup( TIME_TO_SLEEP *uS_TO_S_FACTOR );
esp_deep_sleep_start();
}
void loop() {
}
charge.h :
#ifndef charge_h
#define charge_h
class charge {
public:
void begin();
void check_voltage();
void turnon_contactor();
};
#endif
charge.cpp :
#include "charge.h"
#include <Arduino.h>
float cell[10];
float total_cell_charge;
float difference_cell_charge;
int cell_pin[10] = { 12, 13 ,14,25,26,27,32,33,34,35 };
volatile int interruptCounter_charge;
hw_timer_t * timer1 = NULL;
portMUX_TYPE timerMux1 = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR onTimer1() {
portENTER_CRITICAL_ISR(&timerMux1);
interruptCounter_charge++;
portEXIT_CRITICAL_ISR(&timerMux1);
}
void charge::begin(){
pinMode (22 ,OUTPUT);
timer1 = timerBegin(1, 80, true);
timerAttachInterrupt(timer1 , &onTimer1, true);
timerAlarmWrite(timer1, 2000000, true);
timerAlarmEnable(timer1);
}
void charge::check_voltage(){
if (interruptCounter_charge > 0) {
portENTER_CRITICAL(&timerMux1);
interruptCounter_charge--;
portEXIT_CRITICAL(&timerMux1);
for (int check_cell = 0 ; check_cell++; check_cell < 10){
for (int cell_pin_check = 0 ; cell_pin_check ++ ; cell_pin_check <10 ){
cell [check_cell] = analogRead(cell_pin[cell_pin_check]);
}
}
total_cell_charge = cell[0]+cell[1]+cell[2]+cell[3]+cell[4]+cell[5]+cell[6]+cell[8]+cell[9] ;
difference_cell_charge = cell[0]-cell[1]-cell[2]-cell[3]-cell[4]-cell[5]-cell[6]-cell[8]-cell[9];
Serial.print("test voltage");
Serial.print(total_cell_charge);
}
}
void charge :: turnon_contactor(){
if (total_cell_charge > 24 ) {
digitalWrite(22, HIGH);
}
if (total_cell_charge < 2 ) {
digitalWrite(22,HIGH);
}
}
Have you considered using code tags when posting code?
A ESP32 is a 32 bit thingy. What size number do you expect with a ULL?
nghiep23:
Hi , i try to bring my microcontroller to deep sleep for 10 second on and 10 second off . However , when i put the deep sleep mode , the microcontroller seem not go into do the task of charge.cpp. It just on for 10seconds and off for 10 seconds.
What does that mean?
Are you with issue that when the MCU has went into deep sleep it no longer preforms a function?
Size of number is 32 bit
So if i dont insert the deep sleep function the charge function work fine. However, when i put the deep sleep function, the charge function dont work anymore. What i want is to make the charge function to work with deep sleep.
What do you think the CPU is doing during deep sleep?
How many programs do you think are being are being run while in deepsleep?
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/sleep_modes.html?highlight=deep%20sleep
In Deep-sleep mode, the CPUs, most of the RAM, and all digital peripherals that are clocked from APB_CLK are powered off. The only parts of the chip that remain powered on are:
RTC controller
ULP coprocessor
RTC fast memory
RTC slow memory
When the CPU is powered off how many programs will the CPU be able to run without power?
Hi ,
what you mean is if I want to run the function , I will run sleep mode until something trigger then I wake it up and disable the sleep mode right?
The function cannot be running on a sleeping CPU.
The CPU can be sleeping, triggerd to awaken, runs a function, and then goes back to sleep.
could you please show me how to awake the cpu by timer ? I have look up some example and I use similar to example, however it not wake up the cpu.
here is the link that i use :
Learn about ESP32 Deep Sleep along with its Wake-up sources - Timer, Touch, External wakeup(ext0 & ext1) + Code & Wiring.
Estimated reading time: 22 minutes
According to the link , they should able to count the number when wake up. However, when i put to my esp32 to test. It keep reset to initial. Thus , i believe this link dont wake up the cpu as well.
There also many link similar to this. Hence I not sure how to wake up cpu
could you, please, put your code in code tags?
At your side it not in tag? Cause at my side it does. That weird. You use
3 ` at both side to tag right?
void fSleepyTime( void *pvParmeters )
{
for (;;)
{
xEventGroupWaitBits (eg, evtSleepyTime, pdTRUE, pdTRUE, portMAX_DELAY );
xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY );
MQTTclient.disconnect();
WiFi.disconnect();
esp_sleep_enable_timer_wakeup( 60000000 ); // set timer to wake up once a minute 60000000uS
esp_deep_sleep_start();
}
vTaskDelete( NULL );
} //void fSleepyTime( void *pvParmeters )
////
Notice I do not require a ULL?
Thank you for putting your code in code tags.
Have you tried to see if the sleepytime is working?
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32!");
//set timer0 interrupt at 2kHz
//battery.begin();
//battery.check_voltage();
esp_sleep_enable_timer_wakeup( 60000000 );
//esp_sleep_enable_timer_wakeup( TIME_TO_SLEEP *uS_TO_S_FACTOR );
esp_deep_sleep_start();
}
????
I have try the sleep , it no different , the system just sleep and wake up in the 1sec duration.
The esp_sleep_enable_timer_wakeup( 60000000 ) should wake up every 1 minute right ?
Hello, ESP32!
ets Jul 29 2019 12:21:46
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1156
load:0x40078000,len:11456
ho 0 tail 12 room 4
load:0x40080400,len:2972
entry 0x400805dc
Hello, ESP32!
ets Jul 29 2019 12:21:46
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1156
load:0x40078000,len:11456
ho 0 tail 12 room 4
load:0x40080400,len:2972
The serial output like this. And the wake up happen in 1 sec which not what it should be .
Did you try the code from post12? Does it wake up once a second?
Yup I had tried the post 12 and 11 . I think i have limited reply post. I might edit this post to reply you if I reach restriction
And that code from post 11/12 only wakes up once a second?
I'm sorry I'll be unable to help you. Good luck.