Skip to content

Instantly share code, notes, and snippets.

@futureshocked
Created June 29, 2020 05:26
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save futureshocked/5b8dcb61fa01af7b8c4b556b816894a1 to your computer and use it in GitHub Desktop.
This sketch will blink the LED at GPIO5 once per second and then go to sleep.
// Written by Peter Dalmaris from Tech Explorations.
// Used to find out how long an ESP32 can last on a LiPo battery.
// This sketch will blink the LED at GPIO5 once per second and
// store the number of blinks in an EEPROM location.
// In between blinks, the ESP32 goes to sleep.
#include "EEPROM.h"
#include <DeepSleepScheduler.h>
#ifdef ESP32
#include <esp_sleep.h>
#endif
int addr = 0;
#define EEPROM_SIZE 32
byte led_pin = 5;
void ledOn() {
digitalWrite(led_pin, HIGH);
long count = EEPROM.readULong(addr);
count++;
EEPROM.writeULong(addr, count);
EEPROM.commit();
Serial.println(count);
scheduler.scheduleDelayed(ledOff, 500);
}
void ledOff() {
digitalWrite(led_pin, LOW);
scheduler.scheduleDelayed(ledOn, 500);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("start...");
if (!EEPROM.begin(EEPROM_SIZE))
{
Serial.println("failed to initialise EEPROM"); delay(1000000);
}
#ifdef ESP32
// ESP_PD_DOMAIN_RTC_PERIPH needs to be kept on
// in order for the LED to stay on during sleep
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
#endif
pinMode(led_pin, OUTPUT);
scheduler.schedule(ledOn);
}
void loop() {
scheduler.execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment