macca-nz

OLED Analog Clock with NTP

Apr 25th, 2023 (edited)
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.69 KB | Software | 0 0
  1. /*
  2.  * Simple Analog Clock Using a SSD1306 OLED I2C Display
  3.  * Time is made from NTP Local Time using the ezTime library
  4.  * Default 30 minute NTP re-sync by the ezTime library
  5.  * This sketch will run on an ESP32 or ESP8266
  6.  *
  7.  * Provide official timezone names for "myTZ.setLocation(F("TZ_Name"));"
  8.  * https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  9.  *
  10.  * NOTE: Only tested on ESP32
  11.  */
  12.  
  13. #ifdef ESP32
  14.   #include <WiFi.h>
  15. #else ifdef ESP8266
  16.   #include <ESP8266WiFi.h>
  17. #endif
  18. #include <ezTime.h>
  19. #include <Wire.h>
  20. #include <Adafruit_GFX.h>
  21. #include <Adafruit_SSD1306.h>
  22. /*Define I2C Address and OLED screen size*/
  23. Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, -1);
  24.  
  25. const char* ssid = "Your_ssid";            
  26. const char* password = "Your_password";
  27.  
  28. const int radius = 35;    
  29.  
  30. Timezone myTZ;                    //Lets us use local time and auto daylight time correction
  31.  
  32. int lastSec;
  33.  
  34. void setup() {
  35.     if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {   /*I2C Address for OLED*/
  36.         Serial.println(F("SSD1306 allocation failed"));
  37.         for(;;);
  38.     }
  39.     delay(1000);
  40.     display.clearDisplay();                     /*Clear OLED display*/
  41.     display.setTextSize(1);                     /*Text size set*/
  42.     display.setCursor(0,0);                     /*OLED cursor set*/
  43.     display.setTextColor(WHITE);                /*text color set white*/
  44.  
  45.     WiFi.begin(ssid, password);  
  46.     while (WiFi.status() != WL_CONNECTED){;}
  47.     waitForSync();                              //Default method for NTP sync every 30min
  48.     myTZ.setLocation(F("Pacific/Auckland"));    //Sets our Timezone object
  49. }
  50. void loop() {
  51.     events();                                   //This enables the 30 minute NTP update cycle
  52.     if(lastSec != myTZ.second()){
  53.         lastSec = myTZ.second();
  54.         display.clearDisplay();
  55.         display.drawCircle(64, 32, radius-4, SSD1306_WHITE);
  56.  
  57.         //Draw Clock Second Hand
  58.         float angle = myTZ.second()*6 ;
  59.         angle=(angle/57.29577951) ;
  60.         int x2=(64+(sin(angle)*(radius-4)));
  61.         int y2=(32-(cos(angle)*(radius-4)));
  62.         display.drawLine(64,32,x2,y2,WHITE);
  63.  
  64.         // Draw Clock Minute Hand
  65.         angle = myTZ.minute() * 6 ;
  66.         angle=(angle/57.29577951) ;
  67.         x2=(64+(sin(angle)*(radius-8)));
  68.         y2=(32-(cos(angle)*(radius-8)));
  69.         display.drawLine(64,32,x2,y2,WHITE);
  70.  
  71.         // Draw Clock Hour Hand
  72.         angle = myTZ.hour() * 30 + ((myTZ.minute() / 12) * 6 );
  73.         angle=(angle/57.29577951) ;
  74.         x2=(64+(sin(angle)*(radius-14)));
  75.         y2=(32-(cos(angle)*(radius-14)));
  76.         display.drawLine(64,32,x2,y2,WHITE);
  77.  
  78.         display.display();
  79.     }
  80. }
Tags: OLED NTP ezTime
Advertisement
Add Comment
Please, Sign In to add comment