Guest User

Untitled

a guest
Jun 9th, 2021
34
127 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <NTPClient.h>
  3. #include <GxEPD.h>
  4. #include <GxGDE0213B1/GxGDE0213B1.h> // 2.13" b/w
  5. #include <Fonts/FreeMonoBold12pt7b.h>
  6. #include <GxIO/GxIO_SPI/GxIO_SPI.h>
  7. #include <GxIO/GxIO.h>
  8. #include <WiFi.h>
  9. #include <WiFiUdp.h>
  10. #include "yahoo.h"
  11.  
  12. const char * ssid = "Krisi";
  13. const char * ssidPassword = "krisi9404194775";
  14.  
  15. typedef enum
  16. {
  17.   RIGHT_ALIGNMENT = 0,
  18.   LEFT_ALIGNMENT,
  19.   CENTER_ALIGNMENT,
  20. } Text_alignment;
  21.  
  22. #define uS_TO_S_FACTOR 1000000  // Conversion factor for micro seconds to seconds
  23. #define TIME_TO_SLEEP 60 * 30   // Time ESP32 will go to sleep (in seconds)
  24. #define UPDATE_WEATHER_PROID  6 // Three hour update cycle
  25. #define SERIAL_BAUD_RATE 115200 // Serial baud rate
  26.  
  27. GxIO_Class io(SPI, SS, 17, 16); // arbitrary selection of 17, 16
  28. GxEPD_Class display(io, 16, 4); // arbitrary selection of (16), 4
  29.  
  30. WiFiUDP ntpUDP;
  31. NTPClient timeClient(ntpUDP);
  32.  
  33. RTC_DATA_ATTR int wakeupCount = 0;
  34.  
  35. void syncClock();
  36. void displayWeather();
  37. void testDisplay();
  38.  
  39. void startWifi() {
  40.   WiFi.begin(ssid, ssidPassword);
  41.  
  42.   while (WiFi.status() != WL_CONNECTED) {
  43.     delay(500);
  44.     Serial.println("Connecting to wifi...");
  45.   }
  46.  
  47.   Serial.println("Connected to wifi.");
  48. }
  49.  
  50. void setup() {
  51.   Serial.begin(SERIAL_BAUD_RATE);
  52.  
  53.   delay(500);
  54.  
  55.   if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_TIMER) {
  56.     Serial.print("Boot count: ");
  57.     Serial.println(wakeupCount);
  58.  
  59.     if (++wakeupCount >= UPDATE_WEATHER_PROID) {
  60.       wakeupCount = 0;
  61.       startWifi();
  62.       syncClock();
  63.       // displayWeather();
  64.       testDisplay();
  65.     }
  66.   } else {
  67.     startWifi();
  68.     delay(500);
  69.     syncClock();
  70.     delay(1500);
  71.     // displayWeather();
  72.     testDisplay();
  73.   }
  74.  
  75.   esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  76.  
  77.   Serial.println("Going to sleep now");
  78.  
  79.   esp_deep_sleep_start();
  80.  
  81.   Serial.println("This will never be printed");
  82. }
  83.  
  84. void loop() {
  85.   // do nothing
  86. }
  87.  
  88. void syncClock() {
  89.   timeClient.begin();
  90.  
  91.   timeClient.setTimeOffset(3600);
  92.  
  93.   timeClient.update();
  94.  
  95.   Serial.print("Current time: ");
  96.   Serial.println(timeClient.getFormattedTime());
  97. }
  98.  
  99. void displayText(const String & str, int16_t y, uint8_t alignment) {
  100.   int16_t x = 0;
  101.   int16_t x1, y1;
  102.   uint16_t w, h;
  103.   display.setCursor(x, y);
  104.   display.getTextBounds(str, x, y, &x1, &y1, &w, &h);
  105.  
  106.   switch (alignment) {
  107.     case RIGHT_ALIGNMENT:
  108.       display.setCursor(display.width() - w - x1, y);
  109.       break;
  110.     case LEFT_ALIGNMENT:
  111.       display.setCursor(0, y);
  112.       break;
  113.     case CENTER_ALIGNMENT:
  114.       display.setCursor(display.width() / 2 - ((w + x1) / 2), y);
  115.       break;
  116.   }
  117.  
  118.   display.println(str);
  119. }
  120.  
  121. void displayWeather() {
  122.   int i;
  123.   int16_t x, y;
  124.  
  125.   display.init(SERIAL_BAUD_RATE);
  126.  
  127.   display.eraseDisplay();
  128.   display.setTextColor(GxEPD_BLACK);
  129.   display.setFont(&FreeMonoBold12pt7b);
  130.   display.setTextSize(1);
  131.  
  132.   while(!getWeather()) {
  133.     delay(1000);
  134.   }
  135.  
  136.   i = getWeatherInfo().today.code.toInt();
  137.   x = display.width() / 2 - 50;
  138.   y = 0;
  139.  
  140.   display.setCursor(x, y);
  141.   display.drawBitmap(x, y, wth[i].img, wth[i].w, wth[i].h, GxEPD_BLACK);
  142.  
  143.   String temp = getWeatherInfo().today.temp + "*" + getWeatherInfo().units.temperature;
  144.   String humidity = getWeatherInfo().atmosphere.humidity + "% ";
  145.   String city = getWeatherInfo().location.city;
  146.   String logo = "TTGO";
  147.  
  148.   y = wth[i].h + 18;
  149.   displayText(temp, y, RIGHT_ALIGNMENT);
  150.  
  151.   y = display.getCursorY() + 1;
  152.   displayText(humidity, y, RIGHT_ALIGNMENT);
  153.  
  154.   y = display.getCursorY() + 1;
  155.   displayText(city, y, RIGHT_ALIGNMENT);
  156.  
  157.   y = display.getCursorY() + 25;
  158.   display.setTextSize(2);
  159.   displayText(logo, y, CENTER_ALIGNMENT);
  160.  
  161.   display.update();
  162.  
  163.   Serial.println("Display updated");
  164.   Serial.print("Display size: ");
  165.   Serial.print(display.width());
  166.   Serial.print("x");
  167.   Serial.println(display.height());
  168.   Serial.print("Cursor position: ");
  169.   Serial.print(display.getCursorX());
  170.   Serial.print("x");
  171.   Serial.println(display.getCursorY());
  172. }
  173.  
  174. void testDisplay() {
  175.   Serial.println("testDisplay init");
  176.  
  177.   delay(500);
  178.  
  179.   display.init(SERIAL_BAUD_RATE);
  180.   display.setRotation(0);
  181.   display.drawCornerTest();
  182.  
  183.   display.update();
  184.  
  185.   delay(5000);
  186.  
  187.   displayWeather();
  188.  
  189.   delay(1000);
  190. }
RAW Paste Data