diff --git a/NightscoutEInkClient.ino b/NightscoutEInkClient.ino index 0fb1467..ee92961 100644 --- a/NightscoutEInkClient.ino +++ b/NightscoutEInkClient.ino @@ -3,6 +3,7 @@ #include #include #include +#include "esp_sleep.h" //PIN LAYOUTS #define SRAM_CS 32 @@ -10,8 +11,9 @@ #define EPD_DC 33 #define EPD_RESET -1 // can set to -1 and share with microcontroller Reset! #define EPD_BUSY 27 // can set to -1 to not use a pin (will wait a fixed delay) - - + #define EPD_ENABLE 12 + #define BATTERY_PIN A13 + ThinkInk_290_Tricolor_Z10 display(EPD_DC, EPD_RESET, EPD_CS, SRAM_CS, EPD_BUSY); #define COLOR0 EPD_WHITE @@ -37,6 +39,10 @@ void setup() { while (!Serial) { delay(10); } Serial.println("Nightscout CGM BS Monitor starting..."); + //turn EPD on + pinMode(EPD_ENABLE, OUTPUT); + digitalWrite(EPD_ENABLE, HIGH); + ConnectToWifi(); String nightscoutResponse = httpGETRequest(nightscoutUrl); Serial.println(nightscoutResponse); @@ -57,6 +63,7 @@ void setup() { RenderGraph(array); RenderRightPane(array[0]); + RenderBatteryPercentage(); display.display(); @@ -101,6 +108,7 @@ void SleepWithTimeSync(int lastReadingInSeconds) { secondsToSleep = secondsToSleep + 7; esp_sleep_enable_timer_wakeup(secondsToSleep * uS_TO_S_FACTOR); Serial.flush(); + TurnOffEPD(); esp_deep_sleep_start(); } else { @@ -109,9 +117,38 @@ void SleepWithTimeSync(int lastReadingInSeconds) { } +void RenderBatteryPercentage() { + int batteryPinInput = analogRead(BATTERY_PIN); + Serial.printf("raw adc value %d", batteryPinInput); + //Vin = 3.3(read/4098) + float batteryVoltage = 3.3 * (float(batteryPinInput) / 4098.0); + //ESP32 A13 returns half the voltage, so double this reading + batteryVoltage = batteryVoltage * 2.0; + Serial.print("calc. voltage "); + Serial.println(batteryVoltage); + + //Render on screen + display.setCursor(245, 110); + display.setTextSize(2); + if(batteryVoltage < 3.6) + display.setTextColor(COLOR2); + else + display.setTextColor(COLOR1); + display.print(batteryVoltage); + display.print("v"); +} + +void TurnOffEPD() { + digitalWrite(EPD_ENABLE, LOW); + delay(1000); + gpio_hold_en(GPIO_NUM_12); + gpio_deep_sleep_hold_en(); +} + void SleepFiveMins() { esp_sleep_enable_timer_wakeup(285 * uS_TO_S_FACTOR); Serial.flush(); + TurnOffEPD(); esp_deep_sleep_start(); }