Zum Hauptinhalt springen

Wake from deep sleep with the RTC alarm

What this example shows​

Use the RTC alarm as the wake-up source for deep sleep. On each start the sketch shows the current weekday, date and time, sets an alarm 10 seconds ahead, and goes to sleep. When the alarm fires, the board wakes up and does it again.

The RTC alarm interrupt is connected to GPIO39 on the Inkplate 10, so the sketch calls esp_sleep_enable_ext0_wakeup(GPIO_NUM_39, 0) to wake on that pin going low. Deep sleep restarts the program every time, so all the logic is in setup() and loop() stays empty. The RTC keeps the time while the ESP32 sleeps, and isSet() lets the sketch set the time only the first time.

Example code​

In the Arduino IDE, choose the Inkplate 10 board under Tools, Board, Inkplate Boards before you upload.

Select Board
Inkplate10_RTC_Alarm_With_Deep_Sleep.ino
// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
#if !defined(ARDUINO_INKPLATE10) && !defined(ARDUINO_INKPLATE10V2)
#error \
"Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
#endif

#include "Inkplate.h" // Include Inkplate library to the sketch
#include "driver/rtc_io.h" // Include ESP32 library for RTC pin I/O (needed for rtc_gpio_isolate() function)
#include <rom/rtc.h> // Include ESP32 library for RTC (needed for rtc_get_reset_reason() function)

Inkplate display(INKPLATE_1BIT); // Create an object on Inkplate library and also set library into 1-bit mode (BW)

void setup()
{
display.begin(); // Init Inkplate library (you should call this function ONLY ONCE)

display.rtc.clearAlarmFlag(); // Clear alarm flag from any previous alarm

if (!display.rtc.isSet()) // Check if RTC is already is set. If ts not, set time and date
{
// display.setTime(hour, minute, sec);
display.rtc.setTime(13, 30, 00); // 24H mode, ex. 13:30:00
// display.setDate(weekday, day, month, yr);
display.rtc.setDate(1, 5, 12, 2022); // 0 for Sunday, 1 for Monday, ex. Monday, 5.12.2022.

// display.rtc.setEpoch(1589610300); // Or use epoch for setting the time and date
}

printCurrentTime(); // Display current time and date
display.display();

display.rtc.setAlarmEpoch(display.rtc.getEpoch() + 10, RTC_ALARM_MATCH_DHHMMSS); // Set RTC alarm 10 seconds from now

// Enable wakup from deep sleep on gpio 39 where RTC interrupt is connected
esp_sleep_enable_ext0_wakeup(GPIO_NUM_39, 0);

// Go to sleep
esp_deep_sleep_start();
}

void loop()
{
// Never here! If you are using deep sleep, the whole program should be in setup() because the board restarts each
// time. loop() must be empty!
}

void printCurrentTime()
{
display.setCursor(100, 300);
display.setTextSize(3);

display.rtc.getRtcData();

switch (display.rtc.getWeekday())
{
case 0:
display.print("Sunday , ");
break;
case 1:
display.print("Monday , ");
break;
case 2:
display.print("Tuesday , ");
break;
case 3:
display.print("Wednesday , ");
break;
case 4:
display.print("Thursday , ");
break;
case 5:
display.print("Friday , ");
break;
case 6:
display.print("Saturday , ");
break;
}

display.print(display.rtc.getDay());
display.print(".");
display.print(display.rtc.getMonth());
display.print(".");
display.print(display.rtc.getYear());
display.print(". ");
print2Digits(display.rtc.getHour());
display.print(':');
print2Digits(display.rtc.getMinute());
display.print(':');
print2Digits(display.rtc.getSecond());
}

void print2Digits(uint8_t _d)
{
if (_d < 10)
display.print('0');
display.print(_d, DEC);
}

Trimmed from Inkplate10_RTC_Alarm_With_Deep_Sleep.ino: the file header comment is left out. One comment is corrected: the weekday comment said 0 means Monday, but the sketch prints 0 as Sunday, and 1 with 5 December 2022 is a Monday.

Functions used​

Inkplate(uint8_t mode)returns None

Creates the Inkplate object and chooses the display mode. `INKPLATE_1BIT` is black and white and supports partial updates. `INKPLATE_3BIT` is grayscale and only supports full updates.

ReturnsConstructor; no return value.

Parameters

TypeNameDescription
uint8_tmodeDisplay mode: INKPLATE_1BIT (0) or INKPLATE_3BIT (1).
display.begin()returns void

Initializes the library, the peripherals and the frame buffers. Call it once in `setup()`, later calls are ignored.

ReturnsNothing.

display.rtc.clearAlarmFlag()returns void

Clears the RTC alarm flag. Call it after an alarm has gone off so the next one can be detected.

ReturnsNothing.

display.rtc.isSet()returns bool

Checks whether the RTC was already set. `setTime()` and `setDate()` leave a marker in the RTC, and this function looks for it.

Returns`true` if the RTC has been set, `false` if not.

display.rtc.setTime(uint8_t hour, uint8_t minute, uint8_t sec, bool isPM = false)returns void

Writes the time to the onboard real-time clock (RTC). It also marks the RTC as set, which `isSet()` reports.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_thourHour, in 24-hour form unless the RTC is switched to 12-hour mode.
uint8_tminuteMinute.
uint8_tsecSecond.
boolisPMOnly used in 12-hour mode. Defaults to false.
display.rtc.setDate(uint8_t weekday, uint8_t day, uint8_t month, uint16_t yr)returns void

Writes the date to the RTC. It also marks the RTC as set.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_tweekdayDay of the week, 0 to 6. The example sketches print 0 as Sunday and 1 as Monday.
uint8_tdayDay of the month.
uint8_tmonthMonth, 1 to 12.
uint16_tyrFull year, for example 2022. The function subtracts 2000 before writing it to the RTC.
display.display(bool _leaveOn = 0)returns void

Refreshes the whole panel from the frame buffer (a full update).

ReturnsNothing.

Parameters

TypeNameDescription
bool_leaveOnIf true, the e-paper supply stays on after the update, which saves time on the next update. Defaults to 0.
display.rtc.setAlarmEpoch(uint32_t _epoch, uint8_t _match)returns void

Sets the RTC alarm from an epoch value and enables it. The match argument chooses which fields of that time have to match for the alarm to fire.

ReturnsNothing.

Parameters

TypeNameDescription
uint32_t_epochEpoch of the alarm time.
uint8_t_matchWhich fields to match, for example RTC_ALARM_MATCH_DHHMMSS (day, hour, minute, second). The library also defines RTC_ALARM_MATCH_SS, RTC_ALARM_MATCH_MMSS, RTC_ALARM_MATCH_HHMMSS and RTC_ALARM_MATCH_WHHMMSS.
display.rtc.getEpoch()returns uint32_t

Reads the RTC and returns the current time and date as an epoch value.

ReturnsCurrent epoch (seconds since 1 January 1970).

esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level)returns esp_err_t

Makes a change on one pin wake the ESP32 from deep sleep. It comes from the ESP32 Arduino core, not from the Inkplate library.

ReturnsError code, not checked by the sketch.

Parameters

TypeNameDescription
gpio_num_tgpio_numPin that wakes the board, for example GPIO_NUM_36.
intlevelPin level that causes the wake-up: LOW (0) or HIGH (1).
esp_deep_sleep_start()returns void

Puts the ESP32 into deep sleep. The program stops here, and on wake-up it starts again from the beginning of `setup()`. It comes from the ESP32 Arduino core, not from the Inkplate library.

ReturnsDoes not return.

display.setCursor(int16_t x, int16_t y)returns void

Sets where the next printed text starts, in pixels from the top left corner. This comes from the Adafruit GFX base class that the library builds on.

ReturnsNothing.

Parameters

TypeNameDescription
int16_txX coordinate in pixels.
int16_tyY coordinate in pixels.
display.setTextSize(uint8_t s)returns void

Sets the text magnification. Size 1 is the classic 5x7 pixel font, size 5 is five times bigger. This comes from the Adafruit GFX base class that the library builds on.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_tsText size multiplier.
display.rtc.getRtcData()returns void

Reads the time and date from the RTC.

ReturnsNothing.

display.rtc.getWeekday()returns uint8_t

Returns the current day of the week from the RTC.

ReturnsDay of the week, 0 to 6.

display.print(value, format)returns size_t

Prints text, a character or a number at the cursor position, in the frame buffer. The display object builds on the Adafruit GFX base class, which extends the Arduino `Print` class, so it prints like `Serial` does. Call `display()` or `partialUpdate()` to show the result.

ReturnsNumber of bytes printed.

Parameters

TypeNameDescription
text, char or numbervalueWhat to print.
intformatOptional. Number base for integers, for example DEC.
display.rtc.getDay()returns uint8_t

Returns the current day of the month from the RTC.

ReturnsDay of the month.

display.rtc.getMonth()returns uint8_t

Returns the current month from the RTC.

ReturnsMonth, 1 to 12.

display.rtc.getYear()returns uint16_t

Returns the current year from the RTC.

ReturnsFull year, for example `2022`.

display.rtc.getHour()returns uint8_t

Returns the current hour from the RTC.

ReturnsHours.

display.rtc.getMinute()returns uint8_t

Returns the current minute from the RTC.

ReturnsMinutes.

display.rtc.getSecond()returns uint8_t

Returns the current second from the RTC.

ReturnsSeconds.

What to expect​

On the first start, the screen shows the weekday, date and time in the form Monday , 5.12.2022. 13:30:00. The board then sleeps and wakes about every 10 seconds, redrawing the screen with the current time each time. The sketch does not print to Serial, and its header calls Serial optional.