Advertisement
jgoy

ESP32 IdeaSpark SNTP clock

May 12th, 2024
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | Source Code | 0 0
  1. #include <WiFiManager.h>          // https://github.com/tzapu/WiFiManager/
  2. #include <Adafruit_SSD1306.h>     // https://github.com/adafruit/Adafruit_SSD1306
  3. #include <Fonts/FreeMono9pt7b.h>  // board: https://nl.aliexpress.com/item/1005006269242344.html
  4. #include <Fonts/FreeSansBold9pt7b.h>
  5. #include <Fonts/FreeSansBold18pt7b.h>
  6.  
  7. WiFiManager myWiFi;
  8. Adafruit_SSD1306 oled(128, 64, &Wire, -1);
  9.  
  10. uint8_t SCREEN_ORIENTATION = 2;
  11. char* timeZone = "CET-1CEST,M3.5.0,M10.5.0/3";  // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
  12. char* days[] = { "zondag", "maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag" };
  13. char* conn[] = { "WiFi: no connection.", "Connect to hotspot", "IDEA + open browser",
  14.                  "address 192.168.4.1", "to enter network name", "and password" };
  15.  
  16. void setup() {
  17.   oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  18.   oled.setRotation(SCREEN_ORIENTATION);
  19.   oled.setTextColor(1);                       // white = 1, black = 0
  20.   myWiFi.setAPCallback(messageNoConnection);  // called when no WiFi connection is found
  21.   myWiFi.autoConnect("IDEA");
  22.   configTzTime(timeZone, "pool.ntp.org");
  23. }
  24.  
  25. void loop() {
  26.   struct tm tInfo;  // https://cplusplus.com/reference/ctime/tm/
  27.   getLocalTime(&tInfo);
  28.   oled.clearDisplay();
  29.   oled.setCursor(10, 25);
  30.   oled.setFont(&FreeSansBold18pt7b);
  31.   oled.printf("%02d:%02d", tInfo.tm_hour, tInfo.tm_min);
  32.   oled.setFont(&FreeSansBold9pt7b);
  33.   oled.printf(" %02d", tInfo.tm_sec);
  34.   oled.setCursor(14, 45);
  35.   oled.printf("%02d -%02d -%04d", tInfo.tm_mday, 1 + tInfo.tm_mon, 1900 + tInfo.tm_year);
  36.   oled.setCursor(0, 60);
  37.   oled.setFont(&FreeMono9pt7b);
  38.   oled.setTextColor(0);                                        // invisible because we just want to
  39.   oled.print(days[tInfo.tm_wday]);                             // check the cursor x-position
  40.   oled.setTextColor(1);                                        // visible
  41.   oled.setCursor((oled.width() - oled.getCursorX()) / 2, 60);  // x-coordinate if we want to
  42.   oled.print(days[tInfo.tm_wday]);                             // display the weekday centered
  43.   oled.display();
  44. }
  45.  
  46. void messageNoConnection(WiFiManager* myWiFi) {  // called when no WiFi connection is found
  47.   oled.clearDisplay();
  48.   oled.setTextSize(1);
  49.   for (uint8_t count = 0; count < 6; count++) {
  50.     oled.setCursor(0, count * 10);
  51.     oled.print(conn[count]);
  52.   }
  53.   oled.display();
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement