diff --git a/src/hardware.h b/src/hardware.h index e2dfb1a..26cfb60 100644 --- a/src/hardware.h +++ b/src/hardware.h @@ -2,11 +2,17 @@ #define _HARDWARE_H /* pin assignments */ +// LED_BUILTIN = 2 +// KEY_BUILTIN = 0 static constexpr uint8_t buttonPin = 0; static constexpr uint8_t sensorPin = 0; static constexpr uint8_t rotaryPinA = 39; static constexpr uint8_t rotaryPinB = 38; 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) @@ -14,17 +20,17 @@ static constexpr uint8_t rotaryPinButton = 22; * 5V O O 5V * 3V3 O O 3V3 * GND O < 36 only input, no pullups - * RX * ? * 17 - * TX * < 38 ROTARY_A only input, no pullups + * RX * (3) ? * 17 + * TX * (1) < 38 ROTARY_A 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 * BME280_SDO 19 * LoRa_MISO ? * 32 * BME280_CS 23 * ? * 33 * 18 x LoRa_CS * 25 MAX98_DIN * BME280_SCL/SCK 5 * LoRa_SCK LoRa_IRQ * 26 * 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 * 17 * * 13 MAX98_LRC * 16 * OLED_RST * 21 diff --git a/src/main.cpp b/src/main.cpp index a77739c..1be0329 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,14 +27,16 @@ 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 Basecamp iot; WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "de.pool.ntp.org", 3600, 60000); -char timeStr[20]; -char weatherStr[32]; - BME280 bme280; MP3 mp3; Rotary rotary; @@ -56,6 +58,37 @@ void setup() { pinMode(18, OUTPUT); 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 iot.begin(); @@ -139,7 +172,14 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties void suspend() { DEBUG_PRINTLN("Entering deep sleep"); + esp_sleep_enable_timer_wakeup(1000000LL * secondsToSleep); 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 iot.mqtt.disconnect(); //send the ESP into deep sleep