Browse Source

added weather api

Toby Chui 1 year ago
parent
commit
2d3568f50a
3 changed files with 118 additions and 32 deletions
  1. 24 9
      InkyDash/InkyDash.ino
  2. 5 2
      InkyDash/time.ino
  3. 89 21
      InkyDash/weather.ino

+ 24 - 9
InkyDash/InkyDash.ino

@@ -35,13 +35,26 @@
 #include <WiFiManager.h>
 #include <ESP8266TrueRandom.h>
 #include <NTPClient.h>
+#include <ESP8266HTTPClient.h>
 #include <WiFiUdp.h>
 #include <ArduinoJson.h>
 #include <TaskScheduler.h>
 
 
-/* Power Settings */
-#define LOW_POWER_MODE true
+/* 
+  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;
@@ -74,16 +87,15 @@ const char* ntpServers[] = {
 
 /* Global Variables */
 const String deviceName = "InkyDash v1.0";
-int currentTemp = 25;
-int currentHumd = 50;
+float currentTemp = 25.0;
+float currentHumd = 50.0;
+float currentRain = 0.0;
+float maxTemp = 0.0;
+float minTemp = 0.0;
 
 /* Schedulers */
 void datetimeUpdateCallback();
-//void todayWeatherUpdateCallback();
-//void weatherForcastCallback();
-Task t_dt(1800000, TASK_FOREVER, &datetimeUpdateCallback); //Date-time update task
-//Task t_wt(3600000, TASK_FOREVER, &todayWeatherUpdateCallback); //Weather update task
-//Task t_wf(3600000, TASK_FOREVER, &weatherForcastCallback); //Forcast update task
+Task t_dt(1800000, TASK_FOREVER, &datetimeUpdateCallback); //Frame update 
 Scheduler runner;
 
 void setup() {
@@ -136,6 +148,9 @@ void setup() {
 
   //Draw home page
   if (LOW_POWER_MODE) {
+    //Get weather information from API
+    updateCurrentWeatherInfo();
+    //Render the calender page
     display.drawPaged(drawHomeFrame);
     Serial.println("Display updated. Entering deep sleep mode");
     ESP.deepSleep(1800e6);

+ 5 - 2
InkyDash/time.ino

@@ -5,7 +5,10 @@
 */
 
 //Entry point for scheduler
-void datetimeUpdateCallback(){
+void datetimeUpdateCallback() {
+  //Get weather information from API
+  updateCurrentWeatherInfo();
+  //Render the calender page
   display.drawPaged(drawHomeFrame);
 }
 
@@ -18,7 +21,7 @@ String timeZeroPad(int input) {
 }
 
 //Get the day of week of the first day of this month
-int getFirstDayDayOfWeek(){
+int getFirstDayDayOfWeek() {
   time_t epochTime = ntpClient.getEpochTime();
   struct tm *ptm = gmtime((time_t *)&epochTime);
   return (ntpClient.getDay() - (ptm->tm_mday % 7) + 7) % 7;

+ 89 - 21
InkyDash/weather.ino

@@ -1,47 +1,63 @@
 /*
     Weather Access API
-    Information provided by the Hong Kong Observatory open data API
+    Information provided by the open-meteo.com open data API
 
 */
 
+//Latitude & Longitude of your location
+/* Hong Kong */
+//const String lat = "22.2783";
+//const String lon = "114.1747";
+
+/* Tainan */
+const String lat = "23.0028";
+const String lon = "120.1307";
+
+/* Timezone */
+const String timezone = "Asia/Singapore";
 
 //Draw weather pallete, by default it align right
 void drawWeatherPallete(int x, int y) {
-  display.setFont(&FreeSans24pt7b);
-  uint16_t textColor;
+  display.setFont(&FreeSans18pt7b);
+  display.setTextColor(GxEPD_BLACK);
   int16_t tbx, tby; uint16_t tbw, tbh;
-  if (isNight()) {
-    textColor = GxEPD_BLACK;
-  } else {
-    textColor = GxEPD_RED;
-  }
-  display.setTextColor(textColor);
-  int lineHeight = 44;
 
   //Print current temperature and humd
-  display.getTextBounds(String(currentTemp) + " C", 0, 0, &tbx, &tby, &tbw, &tbh);
+  display.getTextBounds(String(currentTemp, 1) + " C", 0, 0, &tbx, &tby, &tbw, &tbh);
   int tempX = display.width() - tbw - 10;
   display.setCursor(tempX, y + tbh);
-  display.print(String(currentTemp) + " C");
+  display.print(String(currentTemp, 1) + " C");
   //Draw the degree sign as it is not supported in ASCII
-  display.getTextBounds(String(currentTemp), 0, 0, &tbx, &tby, &tbw, &tbh);
-  display.fillCircle(tempX + tbw + 10, y + 7, 6, textColor);
-  display.fillCircle(tempX + tbw + 10, y + 7, 3, GxEPD_WHITE);
+  display.getTextBounds(String(currentTemp, 1), 0, 0, &tbx, &tby, &tbw, &tbh);
+  display.fillCircle(tempX + tbw + 8, y + 5, 5, GxEPD_BLACK);
+  display.fillCircle(tempX + tbw + 8, y + 5, 2, GxEPD_WHITE);
   
+  //Min max daily temp
   y += tbh + 10;
-  display.setFont(&FreeSans18pt7b);
-  display.getTextBounds(String(currentHumd) + "%", 0, 0, &tbx, &tby, &tbw, &tbh);
+  display.setFont(&FreeSans9pt7b);
+  String detailInfo = String(minTemp, 1) + " / " + String(maxTemp, 1) + "'C";
+  display.getTextBounds(detailInfo, 0, 0, &tbx, &tby, &tbw, &tbh);
   display.setCursor(display.width() - tbw - 10, y + tbh);
-  display.print(String(currentHumd) + "%");
+  display.print(detailInfo);
+
+  //Humidity
+  y += tbh + 10;
+  detailInfo = "RH " + String(currentHumd,0) + "%";
+  display.getTextBounds(detailInfo, 0, 0, &tbx, &tby, &tbw, &tbh);
+  display.setCursor(display.width() - tbw - 10, y + tbh);
+  display.print(detailInfo);
+
 }
 
+
+
 //Draw the sun or moon icon base on day / night time
-void drawSunMoon(int x, int y){
-  if (isNight()){
+void drawSunMoon(int x, int y) {
+  if (isNight()) {
     //Draw moon
     display.fillCircle(x, y, 40, GxEPD_RED);
     display.fillCircle(x - 30, y - 15, 38, GxEPD_WHITE);
-  }else{
+  } else {
     //Draw sun
     display.fillCircle(x, y, 40, GxEPD_RED);
     //some clouds
@@ -49,3 +65,55 @@ void drawSunMoon(int x, int y){
     display.fillCircle(x + 40, y + 20, 30, GxEPD_WHITE);
   }
 }
+
+bool updateCurrentWeatherInfo() {
+  // Make HTTP request
+  HTTPClient http;
+  WiFiClient client;
+  http.begin(client, "http://api.open-meteo.com/v1/forecast?latitude=" + lat + "&longitude=" + lon + "&current=temperature_2m,relative_humidity_2m,rain&daily=temperature_2m_max,temperature_2m_min&timezone=" + timezone + "&forecast_days=1");
+  int httpCode = http.GET();
+
+  if (httpCode > 0) {
+    if (httpCode == HTTP_CODE_OK) {
+      String payload = http.getString();
+
+      // Parse JSON
+      DynamicJsonDocument doc(1024);
+      DeserializationError error = deserializeJson(doc, payload);
+
+      // Extract temperature_2m and relative_humidity_2m
+      if (!error) {
+        currentTemp = doc["current"]["temperature_2m"];
+        currentHumd = doc["current"]["relative_humidity_2m"];
+        currentRain = doc["current"]["rain"];
+        minTemp = doc["daily"]["temperature_2m_min"][0];
+        maxTemp = doc["daily"]["temperature_2m_max"][0];
+        
+
+        Serial.print("Temperature: ");
+        Serial.print(currentTemp);
+        Serial.println("°C");
+
+        Serial.print("Humidity: ");
+        Serial.print(currentHumd);
+        Serial.println("%");
+
+        Serial.print("Rain: ");
+        Serial.print(currentRain);
+        Serial.println("mm");
+      } else {
+        Serial.println("Failed to parse JSON");
+        return false;
+      }
+    } else {
+      Serial.printf("HTTP error code: %d\n", httpCode);
+      return false;
+    }
+  } else {
+    Serial.println("Failed to connect to server");
+    return false;
+  }
+
+  http.end();
+  return true;
+}