B4R Question ESP32 deepsleep

derez

Expert
Licensed User
Longtime User
I made the ESP32 go to deepsleep with this simple command:
B4X:
#if C
void esp_deep_sleep_start(B4R::Object* o) {
 esp_deep_sleep_start();
}
#end if

I don't know ( because of the types involved) how to write the wakeup command, based on this C code in esp_sleep.h file:
B4X:
/**
 * @brief Enable wakeup using a pin
 *
 * This function uses external wakeup feature of RTC_IO peripheral.
 * It will work only if RTC peripherals are kept on during sleep.
 *
 * This feature can monitor any pin which is an RTC IO. Once the pin transitions
 * into the state given by level argument, the chip will be woken up.
 *
 * @note This function does not modify pin configuration. The pin is
 *       configured in esp_sleep_start, immediately before entering sleep mode.
 *
 * @note In revisions 0 and 1 of the ESP32, ext0 wakeup source
 *       can not be used together with touch or ULP wakeup sources.
 *
 * @param gpio_num  GPIO number used as wakeup source. Only GPIOs which are have RTC
 *             functionality can be used: 0,2,4,12-15,25-27,32-39.
 * @param level  input level which will trigger wakeup (0=low, 1=high)
 * @return
 *      - ESP_OK on success
 *      - ESP_ERR_INVALID_ARG if the selected GPIO is not an RTC GPIO,
 *        or the mode is invalid
 *      - ESP_ERR_INVALID_STATE if wakeup triggers conflict
 */
esp_err_t esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level);

Can someone help ?
Thanks
 

derez

Expert
Licensed User
Longtime User
B4X:
#if C
void esp_deep_sleep_start(B4R::Object* o) {
 esp_deep_sleep_start();
}

void esp_sleep_enable_ext0_wakeup(B4R::Object* o) {
esp_sleep_enable_ext0_wakeup(33, 1); //change 33 with the correct pin number.
}
#end if

I get this Error log:
D:\B4ESP\ESP32_wakeup\Objects\bin\sketch\b4r_main.cpp: In function 'void esp_sleep_enable_ext0_wakeup(B4R::Object*)':
b4r_main.cpp:14:35: error: invalid conversion from 'int' to 'gpio_num_t' [-fpermissive]
esp_sleep_enable_ext0_wakeup(33, 1); //change 33 with the correct pin number.


EDIT: added a type conversion and it works, pin33 is connected with 10Kohm to 3.3V. To wake it up I have to cut the connection momentary.
It works also if I put 1 in the method and connect the pin to ground, disconnecting wakes it up.
B4X:
void esp_sleep_enable_ext0_wakeup(B4R::Object* o) {
esp_sleep_enable_ext0_wakeup((gpio_num_t)33, 0); //change 33 with the correct pin number.
}

חג שמח ושנה טובה !!!
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
I added the sleep by time method and made general the wakeup enable method:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Private Gpio_num As Int 'ignore
    Private Level As Int  ' ignore
    ' connect pin 33 to ground , to wake up disconnect it.
End Sub
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    enable_wakeup(33, 1)
    Log("going to sleep")
'    esp_deep_sleep(30)  ' either sleep by time (seconds ) or sleep until wake up by pin33 - next line
    RunNative("esp_deep_sleep_start",Null)
    Log("i am not asleep !")
  
End Sub
Sub enable_wakeup(pin As Int, lvl As Int)
    Gpio_num = pin
    Level = lvl
    RunNative("esp_sleep_enable_ext0_wakeup",Null)
End Sub
Sub esp_deep_sleep(sec As ULong)
 RunNative("esp_deep_sleep", sec * 1000000)
End Sub
#if C
void esp_deep_sleep_start(B4R::Object* o) {
 esp_deep_sleep_start();
}
void esp_deep_sleep(B4R::Object* o) {
   esp_deep_sleep(o->toULong());
}
void esp_sleep_enable_ext0_wakeup(B4R::Object* o) {
esp_sleep_enable_ext0_wakeup((gpio_num_t)b4r_main::_gpio_num, b4r_main::_level);
}
#end if
 
Last edited:
Upvote 0
Top