From a3f967e747f5f185ffc954d475eebc5fdd68c543 Mon Sep 17 00:00:00 2001 From: choelzl Date: Tue, 3 May 2022 13:13:37 +0200 Subject: [PATCH] [WIP] Powermanagement --- metesp.ino | 49 +++++++++++++++++++++++++++++++------ src/influxdb/influx.h | 6 +++++ src/ntp/ntp.cpp | 1 + src/ntp/ntp.h | 1 + src/owm/owm.h | 2 ++ src/sensor/dev/pmsa003.cpp | 3 +++ src/sensor/dev/pmsa003.h | 3 +-- src/sensor/dev/sgp40.cpp | 1 + src/sensor/dev/sgp40.h | 4 +-- src/sensor/dev/tsl25911.cpp | 1 + src/sensor/dev/tsl25911.h | 3 +-- src/touch/gt1151.cpp | 3 --- src/touch/gt1151.h | 6 +++++ 13 files changed, 65 insertions(+), 18 deletions(-) diff --git a/metesp.ino b/metesp.ino index 40e967f..b7c5ae4 100644 --- a/metesp.ino +++ b/metesp.ino @@ -27,8 +27,12 @@ #define PIN_SCL 23 +#define uS_TO_S_FACTOR 1000000 //Conversion factor for micro seconds to seconds +#define TIME_TO_SLEEP (60*5) + /* Global Variables -----------------------------------------------------------*/ +RTC_DATA_ATTR int bootCount = 0; static WiFiMulti wifiMulti; static TwoWire I2C = TwoWire(0); @@ -108,9 +112,6 @@ void IRAM_ATTR onTouch(int8_t contacts, GTPoint *points) { } /* Setup Functions -----------------------------------------------------------*/ -void setup_EPD(){ - Display::setup(); -} void setup_TOUCH() { touch->setHandler(onTouch); @@ -147,23 +148,53 @@ void setup_TIMEDFUN() { REGISTER_TIMEDFUN_CALL(NTP_TF, 60); } +void setup_POWER() { + esp_sleep_enable_ext0_wakeup(GPIO_NUM_15, 1); + esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, 1); + esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); + esp_sleep_pd_config(ESP_PD_DOMAIN_VDDSDIO,ESP_PD_OPTION_ON); + +} + + +void print_wakeup_reason(){ + esp_sleep_wakeup_cause_t wakeup_reason; + + wakeup_reason = esp_sleep_get_wakeup_cause(); + + switch(wakeup_reason) + { + case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break; + case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break; + case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break; + case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break; + case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break; + default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break; + } +} + + /* Setup Functions --------------------------------------------------------------*/ void setup(){ Serial.begin(115200); - - I2C.begin(PIN_SDA, PIN_SCL, 100000); + I2C.begin(PIN_SDA, PIN_SCL); I2C.setPins(PIN_SDA,PIN_SCL); - setup_EPD(); + + ++bootCount; + Serial.println("Boot number: " + String(bootCount)); + print_wakeup_reason(); + + Display::setup(); setup_TOUCH(); setup_WIFI(); setup_TIMER(); setup_TIMEDFUN(); - iflx->check(); Display::setViews(views,views_size); - + iflx->check(); tf->updateForce(); + shutdownc = 0; xTaskCreatePinnedToCore( loop_MEASURE, "Task LMeasure", @@ -173,6 +204,7 @@ void setup(){ loop_DRAW, "Task LDraw", 10000, NULL, 1, &task_draw, 1); + Serial.println("Completed Setup"); } @@ -185,6 +217,7 @@ void loop_DRAW(void * pvParameters ){ for(;;){ touch->update(); if(shutdownc >= 10){ + Serial.println("Shuting down"); Display::refresh(); EPD_2IN13_V2_Sleep(); esp_deep_sleep_start(); diff --git a/src/influxdb/influx.h b/src/influxdb/influx.h index a93935d..58ac7b0 100644 --- a/src/influxdb/influx.h +++ b/src/influxdb/influx.h @@ -3,6 +3,7 @@ #define DEVICE "ESP32" #include +#include #include @@ -60,6 +61,7 @@ class Influx { } void record(){ + if(WiFi.status() != WL_CONNECTED) return; uint8_t i = 0; while(owm && owm[i] != NULL){ record_weather(owm[i]); @@ -69,6 +71,7 @@ class Influx { } void record(bool w, bool s){ + if(WiFi.status() != WL_CONNECTED) return; uint8_t i = 0; while(w && owm && owm[i] != NULL){ record_weather(owm[i]); @@ -80,6 +83,8 @@ class Influx { void record_local(){ if(!sensor) return; + if(WiFi.status() != WL_CONNECTED) return; + dp.clearFields(); dp.clearTags(); dp.addTag("device", "WESP0"); @@ -125,6 +130,7 @@ class Influx { void record_weather(OWM* owm){ if(!owm) return; if(!owm->valid_weather) return; + if(WiFi.status() != WL_CONNECTED) return; JsonObject weather_0 = owm->weather["weather"][0]; JsonObject weather_main = owm->weather["main"]; diff --git a/src/ntp/ntp.cpp b/src/ntp/ntp.cpp index 406f2b8..5284074 100644 --- a/src/ntp/ntp.cpp +++ b/src/ntp/ntp.cpp @@ -226,6 +226,7 @@ strDateTime NTPtime::getNTPtime(float _timeZone, boolean _DayLightSaving) { void NTPtime::updateNTPtime(){ + if(WiFi.status() != WL_CONNECTED) return; strDateTime ndt = getNTPtime(1.0f, true); if(ndt.valid) dt = ndt; }; \ No newline at end of file diff --git a/src/ntp/ntp.h b/src/ntp/ntp.h index 0dc3c9c..f090ceb 100644 --- a/src/ntp/ntp.h +++ b/src/ntp/ntp.h @@ -2,6 +2,7 @@ #define NTPtime_h #include +#include struct strDateTime { byte hour; diff --git a/src/owm/owm.h b/src/owm/owm.h index 3e61b5b..e86330c 100644 --- a/src/owm/owm.h +++ b/src/owm/owm.h @@ -3,6 +3,7 @@ #include #include +#include #define OPENWEATHER_URL(loc, api) ("https://api.openweathermap.org/data/2.5/weather?q=" + loc + "&appid=" + api + "&units=metric") @@ -17,6 +18,7 @@ class OWM{ bool valid_weather = false; bool update(){ + if(WiFi.status() != WL_CONNECTED) return false; String response = httpGETRequest(OPENWEATHER_URL(location,key).c_str()); DeserializationError error = deserializeJson(weather, response); diff --git a/src/sensor/dev/pmsa003.cpp b/src/sensor/dev/pmsa003.cpp index 2fd5fa3..3965e7b 100644 --- a/src/sensor/dev/pmsa003.cpp +++ b/src/sensor/dev/pmsa003.cpp @@ -3,6 +3,9 @@ #include + +#define SENSOR_DATA_LENGTH 32 + /****************************************************************/ bool PMSA003::Initialize(){ bool success(true); diff --git a/src/sensor/dev/pmsa003.h b/src/sensor/dev/pmsa003.h index 5e40d91..5169619 100644 --- a/src/sensor/dev/pmsa003.h +++ b/src/sensor/dev/pmsa003.h @@ -7,7 +7,6 @@ #define PMSA003_ADDRESS (0x12) -#define SENSOR_DATA_LENGTH 32 typedef struct PMSAQIdata { uint16_t pm10_standard, ///< Standard PM1.0 @@ -95,7 +94,7 @@ private: bool ReadChipID(); bool ReadRegister(uint8_t addr,uint8_t data[],uint8_t length); - bool ReadData(uint8_t data[SENSOR_DATA_LENGTH]); + bool ReadData(uint8_t* data); bool WriteSettings(); bool WriteRegister(uint16_t addr,uint8_t data); diff --git a/src/sensor/dev/sgp40.cpp b/src/sensor/dev/sgp40.cpp index d881718..ccaaee9 100644 --- a/src/sensor/dev/sgp40.cpp +++ b/src/sensor/dev/sgp40.cpp @@ -1,5 +1,6 @@ #include "sgp40.h" +#define SENSOR_DATA_LENGTH 3 /****************************************************************/ bool SGP40::Initialize(){ diff --git a/src/sensor/dev/sgp40.h b/src/sensor/dev/sgp40.h index ad47c61..e591081 100644 --- a/src/sensor/dev/sgp40.h +++ b/src/sensor/dev/sgp40.h @@ -8,8 +8,6 @@ #define SGP40_ADDRESS (0x59) -#define SENSOR_DATA_LENGTH 3 - ////////////////////////////////////////////////////////////////// /// SGP40 - Driver class for SGP40 sensor /// @@ -55,7 +53,7 @@ private: bool ReadChipID(); bool ReadRegister(uint8_t addr,uint8_t data[],uint8_t length); - bool ReadData(uint8_t data[SENSOR_DATA_LENGTH], float RH, float T); + bool ReadData(uint8_t* data, float RH, float T); bool WriteSettings(); bool WriteRegister(uint16_t addr,uint8_t data); diff --git a/src/sensor/dev/tsl25911.cpp b/src/sensor/dev/tsl25911.cpp index 68e9269..53b0b51 100644 --- a/src/sensor/dev/tsl25911.cpp +++ b/src/sensor/dev/tsl25911.cpp @@ -1,6 +1,7 @@ #include "tsl25911.h" +#define SENSOR_DATA_LENGTH 4 #define ENABLE_POWEROFF (0x00) ///< Flag for ENABLE register to disable #define ENABLE_POWERON (0x01) ///< Flag for ENABLE register to enable diff --git a/src/sensor/dev/tsl25911.h b/src/sensor/dev/tsl25911.h index 9013996..2b5fbf4 100644 --- a/src/sensor/dev/tsl25911.h +++ b/src/sensor/dev/tsl25911.h @@ -6,7 +6,6 @@ #define TSL25911_ADDRESS (0x29) -#define SENSOR_DATA_LENGTH 4 ////////////////////////////////////////////////////////////////// /// TSL25911 - Driver class for TSL25911 sensor @@ -111,7 +110,7 @@ private: bool ReadChipID(); bool ReadRegister(uint8_t addr,uint8_t data[],uint8_t length); - bool ReadData(uint32_t data[SENSOR_DATA_LENGTH]); + bool ReadData(uint32_t* data); void WriteSettings(); bool WriteRegister(uint8_t addr,uint8_t data); diff --git a/src/touch/gt1151.cpp b/src/touch/gt1151.cpp index b12b5b5..1a691c9 100644 --- a/src/touch/gt1151.cpp +++ b/src/touch/gt1151.cpp @@ -1,8 +1,5 @@ #include "gt1151.h" -#define GT1151_RESET_PIN 02 -#define GT1151_INT_PIN 15 - volatile uint8_t gtIRQ = 0; void IRAM_ATTR _gt_irq_handler() { diff --git a/src/touch/gt1151.h b/src/touch/gt1151.h index 84e39ea..9da2bd1 100644 --- a/src/touch/gt1151.h +++ b/src/touch/gt1151.h @@ -4,6 +4,12 @@ #ifndef __GT1151_H #define __GT1151_H + +#define GT1151_RESET_PIN 02 +#define GT1151_INT_PIN 15 //RTC_GPIO13 + + + #define GT1151_ADDRESS 0x14 #define GT1151_ADDRESS_28 0x14 #define GT1151_ADDRESS_BA 0x5D