Browse Source

initial deep sleep code

main
Hendrik Langer 7 years ago
parent
commit
82bcd247f4
  1. 14
      src/hardware.h
  2. 46
      src/main.cpp

14
src/hardware.h

@ -2,11 +2,17 @@
#define _HARDWARE_H #define _HARDWARE_H
/* pin assignments */ /* pin assignments */
// LED_BUILTIN = 2
// KEY_BUILTIN = 0
static constexpr uint8_t buttonPin = 0; static constexpr uint8_t buttonPin = 0;
static constexpr uint8_t sensorPin = 0; static constexpr uint8_t sensorPin = 0;
static constexpr uint8_t rotaryPinA = 39; static constexpr uint8_t rotaryPinA = 39;
static constexpr uint8_t rotaryPinB = 38; static constexpr uint8_t rotaryPinB = 38;
static constexpr uint8_t rotaryPinButton = 22; static constexpr uint8_t rotaryPinButton = 22;
static constexpr uint8_t ext_wakeup_pin_1 = 0;
static constexpr uint8_t ext_wakeup_pin_2 = 0;
static constexpr uint32_t secondsToSleep = 3600;
/* Wiring (Heltec OLED Lora) /* Wiring (Heltec OLED Lora)
@ -14,17 +20,17 @@ static constexpr uint8_t rotaryPinButton = 22;
* 5V O O 5V * 5V O O 5V
* 3V3 O O 3V3 * 3V3 O O 3V3
* GND O < 36 only input, no pullups * GND O < 36 only input, no pullups
* RX * ? * 17 * RX * (3) ? * 17
* TX * < 38 ROTARY_A only input, no pullups * TX * (1) < 38 ROTARY_A only input, no pullups
* RST * BUTTON < 39 ROTARY_B only input, no pullups * RST * BUTTON < 39 ROTARY_B only input, no pullups
* 0 * BUTTON < 34 only input, no pullups * 0 * KEY_BUILTIN < 34 only input, no pullups
* ROTARY_BTN 22 * < 35 only input, no pullups * ROTARY_BTN 22 * < 35 only input, no pullups
* BME280_SDO 19 * LoRa_MISO ? * 32 * BME280_SDO 19 * LoRa_MISO ? * 32
* BME280_CS 23 * ? * 33 * BME280_CS 23 * ? * 33
* 18 x LoRa_CS * 25 MAX98_DIN * 18 x LoRa_CS * 25 MAX98_DIN
* BME280_SCL/SCK 5 * LoRa_SCK LoRa_IRQ * 26 * BME280_SCL/SCK 5 * LoRa_SCK LoRa_IRQ * 26
* 15 * OLED_SCL LoRa_MOSI * 27 BME280_SDA/SDI * 15 * OLED_SCL LoRa_MOSI * 27 BME280_SDA/SDI
* 2 * LoRa_RST * 14 * 2 * LED_B LoRa_RST * 14
* 4 * OLED_SDA * 12 MAX98_BCLK * 4 * OLED_SDA * 12 MAX98_BCLK
* 17 * * 13 MAX98_LRC * 17 * * 13 MAX98_LRC
* 16 * OLED_RST * 21 * 16 * OLED_RST * 21

46
src/main.cpp

@ -27,14 +27,16 @@
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 15, /* data=*/ 4); U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 15, /* data=*/ 4);
char timeStr[20];
char weatherStr[32];
RTC_DATA_ATTR static int boot_count = 0;
//Create a new Basecamp instance called iot //Create a new Basecamp instance called iot
Basecamp iot; Basecamp iot;
WiFiUDP ntpUDP; WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "de.pool.ntp.org", 3600, 60000); NTPClient timeClient(ntpUDP, "de.pool.ntp.org", 3600, 60000);
char timeStr[20];
char weatherStr[32];
BME280 bme280; BME280 bme280;
MP3 mp3; MP3 mp3;
Rotary rotary; Rotary rotary;
@ -56,6 +58,37 @@ void setup() {
pinMode(18, OUTPUT); pinMode(18, OUTPUT);
digitalWrite(18, HIGH); // disable LoRa_CS digitalWrite(18, HIGH); // disable LoRa_CS
boot_count++;
/* time_t now;
struct tm timeinfo;
time(&now);
setenv("TZ", "GMT+1", 1);
tzset();
localtime_r(&now, &timeinfo);*/
esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT0:
Serial.println("wakeup by RTC_IO");
break;
case ESP_SLEEP_WAKEUP_EXT1:
Serial.println("wakeup by RTC_CNTL");
break;
case ESP_SLEEP_WAKEUP_TIMER:
Serial.println("wakeup by timer");
break;
case ESP_SLEEP_WAKEUP_TOUCHPAD:
Serial.println("wakeup by touchpad");
break;
case ESP_SLEEP_WAKEUP_ULP:
Serial.println("wakeup by ULP");
break;
default: // ESP_SLEEP_WAKEUP_UNDEFINED
Serial.println("wakeup not by deep sleep");
break;
}
//Initialize Basecamp //Initialize Basecamp
iot.begin(); iot.begin();
@ -139,7 +172,14 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
void suspend() { void suspend() {
DEBUG_PRINTLN("Entering deep sleep"); DEBUG_PRINTLN("Entering deep sleep");
esp_sleep_enable_timer_wakeup(1000000LL * secondsToSleep);
esp_sleep_enable_ext0_wakeup((gpio_num_t)sensorPin, 0); esp_sleep_enable_ext0_wakeup((gpio_num_t)sensorPin, 0);
const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1;
const uint64_t ext_wakeup_pin_2_mask = 1ULL << ext_wakeup_pin_2;
// esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask | ext_wakeup_pin_2_mask, ESP_EXT1_WAKEUP_ANY_HIGH);
//esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); // power down all peripherals
//properly disconnect from the MQTT broker //properly disconnect from the MQTT broker
iot.mqtt.disconnect(); iot.mqtt.disconnect();
//send the ESP into deep sleep //send the ESP into deep sleep

Loading…
Cancel
Save