Browse Source

updated display refresh interval

Toby Chui 1 year ago
parent
commit
86aea77cf6
4 changed files with 88 additions and 34 deletions
  1. 55 30
      InkyDash/InkyDash.ino
  2. 1 2
      InkyDash/draw.ino
  3. 29 2
      InkyDash/time.ino
  4. 3 0
      InkyDash/utils.ino

+ 55 - 30
InkyDash/InkyDash.ino

@@ -16,6 +16,30 @@
 // Generic ESP8266
 // BUSY -> GPIO4, RST -> GPIO2, DC -> GPIO0, CS -> GPIO15, CLK -> GPIO14, DIN -> GPIO13, GND -> GND, 3.3V -> 3.3V
 
+/*
+  Power Settings
+
+  If your device is designed to be plugged in
+  set LOW_POWER_MODE to false
+
+  If your device is powered with battery and
+  GPIO16 / D0 is connected to RST with a resistor
+  (resistor value depends on development board)
+  set LOW_POWER_MODE to true
+
+  If yout are not sure, set it to false
+*/
+#define LOW_POWER_MODE true
+
+/* 
+  Display refresh interval
+  
+  Too frequent interval in low power mode might results getting ban by weather API / NTP servers 
+*/
+#define REFRESH_INTERVAL 900e6 //900 seconds, aka 15 minutes
+
+
+
 /* Display Libraries */
 #include <GxEPD.h>
 #include <GxGDEW075Z09/GxGDEW075Z09.h>    // 7.5" b/w/r
@@ -41,20 +65,7 @@
 #include <TaskScheduler.h>
 
 
-/* 
-  Power Settings 
-
-  If your device is designed to be plugged in
-  set LOW_POWER_MODE to false
-
-  If your device is powered with battery and 
-  GPIO16 / D0 is connected to RST with a resistor
-  (resistor value depends on development board)
-  set LOW_POWER_MODE to true
 
-  If yout are not sure, set it to false
-*/
-#define LOW_POWER_MODE true 
 
 /* WiFi and Connections */
 WiFiManager wifiManager;
@@ -69,6 +80,9 @@ GxEPD_Class display(io, /*RST=D4*/ 2, /*BUSY=D2*/ 4); // default selection of D4
 const String weekDays[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
 const String weeksDaysFull[7] = { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
 const String months[12] = {"JANUARY",  "FEBRUARY",  "MARCH",  "APRIL",  "MAY",  "JUNE",  "JULY",  "AUGUST",  "SEPTEMBER",  "OCTOBER",  "NOVEMBER",  "DECEMBER"};
+
+/* NTP Servers*/
+//Add more if you want to refresh more frequently in low power mode
 const char* ntpServers[] = {
   "0.debian.pool.ntp.org",
   "1.debian.pool.ntp.org",
@@ -82,8 +96,16 @@ const char* ntpServers[] = {
   "0.arch.pool.ntp.org",
   "1.arch.pool.ntp.org",
   "2.arch.pool.ntp.org",
-  "3.arch.pool.ntp.org"
+  "3.arch.pool.ntp.org",
+  "time.google.com",
+  "time.cloudflare.com",
+  "time.windows.com",
+  "time.apple.com",
+  "time-a-wwv.nist.gov",
+  "time-b-wwv.nist.gov",
+  "time-c-wwv.nist.gov"
 };
+const int ntpServerCounts = 20;
 
 /* Global Variables */
 const String deviceName = "InkyDash v1.0";
@@ -95,7 +117,7 @@ float minTemp = 0.0;
 
 /* Schedulers */
 void datetimeUpdateCallback();
-Task t_dt(1800000, TASK_FOREVER, &datetimeUpdateCallback); //Frame update 
+Task t_dt(1800000, TASK_FOREVER, &datetimeUpdateCallback); //Frame update
 Scheduler runner;
 
 void setup() {
@@ -105,6 +127,9 @@ void setup() {
   Serial.println("Starting up InkyDash");
   display.init(115200);
 
+  //Set D0 to wakeup pin
+  pinMode(D0,WAKEUP_PULLUP);
+  
   //Draw starting frame
   display.setRotation(3);
   if (!LOW_POWER_MODE) {
@@ -114,21 +139,21 @@ void setup() {
 
   // Connect to Wi-Fi
   /*
-  //Hard code version 
-  WiFi.begin("SSID", "password");
-  while (WiFi.status() != WL_CONNECTED) {
+    //Hard code version
+    WiFi.begin("SSID", "password");
+    while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print('.');
-  }
-  Serial.println('\n');
-  Serial.println("Connection established!");  
-  Serial.print("IP address:\t");
-  Serial.println(WiFi.localIP());   
+    }
+    Serial.println('\n');
+    Serial.println("Connection established!");
+    Serial.print("IP address:\t");
+    Serial.println(WiFi.localIP());
   */
-  
+
   wifiManager.setAPCallback(configModeCallback);
   wifiManager.autoConnect("InkyDash");
-  
+
   if (WiFi.status() == WL_CONNECTED) {
     Serial.println("WiFi Connected");
   } else {
@@ -138,9 +163,9 @@ void setup() {
   }
 
   delay(1000);
-  
-  //Randomly get one of the NTP server from the list
-  const char* randomNtpServer = getRandomNtpServer(ntpServers, 13);
+
+  //Randomly get one of the NTP server from the list and request time
+  const char* randomNtpServer = getRandomNtpServer(ntpServers);
   Serial.println("Using NTP Server: " + String(randomNtpServer));
   NTPClient timeClient(UDPconn, randomNtpServer);
   ntpClient.begin();
@@ -153,8 +178,8 @@ void setup() {
     //Render the calender page
     display.drawPaged(drawHomeFrame);
     Serial.println("Display updated. Entering deep sleep mode");
-    ESP.deepSleep(1800e6);
-    delay(10);
+    ESP.deepSleep(REFRESH_INTERVAL);
+    delay(100);
     //After deep sleep mode, device will reset and not entering the loop() session
   } else {
     runner.init();

+ 1 - 2
InkyDash/draw.ino

@@ -7,8 +7,7 @@
 
 //Draw the frame for the home information panel
 void drawHomeFrame() {
-  //Update the render information
-  ntpClient.update();
+  updateNTPTimeWithRetry();
   //Reset the screen with white
   display.fillScreen(GxEPD_WHITE);
   //Draw elements

+ 29 - 2
InkyDash/time.ino

@@ -4,6 +4,33 @@
    This script handle date time related features
 */
 
+//Update the current time with retry
+void updateNTPTimeWithRetry() {
+  bool succ = false;
+  int counter = 0;
+  while (!succ && counter < 5) {
+    //Update the render information
+    succ = ntpClient.update();
+    if (succ) {
+      //Break out of loop immediately
+      break;
+    }
+
+    //NTP update failed. Retry in a few seconds
+    Serial.println("Update NTP failed. Retrying in 3 seconds");
+    //Stop the current NTP client
+    ntpClient.end();
+    delay(3000);
+    //Switch another NTP server
+    const char* randomNtpServer = getRandomNtpServer(ntpServers);
+    Serial.println("Changing NTP Server to: " + String(randomNtpServer));
+    NTPClient timeClient(UDPconn, randomNtpServer);
+    ntpClient.begin();
+    ntpClient.setTimeOffset(28800);
+    counter++;
+  }
+}
+
 //Entry point for scheduler
 void datetimeUpdateCallback() {
   //Get weather information from API
@@ -45,8 +72,8 @@ bool isNight() {
 }
 
 //Grab a random NTP server
-const char* getRandomNtpServer(const char* ntpServers[], int numServers) {
-  int randomIndex = ESP8266TrueRandom.random(numServers - 1);
+const char* getRandomNtpServer(const char* ntpServers[]) {
+  int randomIndex = ESP8266TrueRandom.random(ntpServerCounts - 1);
   //int randomIndex = 0;
   // Return the NTP server at the randomly chosen index
   return ntpServers[randomIndex];

+ 3 - 0
InkyDash/utils.ino

@@ -4,6 +4,9 @@
  * 
  * Handle utilities functions
  */
+
+
+ //Instruction for WiFi setup
 void configModeCallback(WiFiManager *myWiFiManager) {
   Serial.println("WiFi not setup or not found. Entered config mode");
   display.drawPaged(drawWifiSetupInstruction);