I would like to change this clock to u8g2.
It works fine with the esp8266 and oled 128x64.
I would start by including the u8g2 library with #include
and all display. with u8g2. replace.
What else should I consider.?
Thanks Else
This is my Code
#include <ESP8266WiFi.h>
#include <WifiUDP.h>
#include <String.h>
#include <Wire.h>
#include <SSD1306.h>
#include <SSD1306Wire.h>
#include <NTPClient.h>
#include <Time.h>
#include <TimeLib.h>
#include <Timezone.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
// Define NTP properties
#define NTP_OFFSET 60 * 60 // In seconds
#define NTP_INTERVAL 60 * 1000 // In miliseconds
#define NTP_ADDRESS "ca.pool.ntp.org" // change this to whatever pool is closest (see ntp.org)
// Set up the NTP UDP client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);
// Create a display object // SSD1306 display(0x3c, SDA, SCL);
SSD1306 display(0x3c, D2, D1); //0x3d for the Adafruit 1.3" OLED, 0x3C being the usual address of the OLED
//U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ 5, /* data=*/ 4, /* reset=*/ U8X8_PIN_NONE);
const char* ssid = "my ssid"; // insert your own ssid
const char* password = "my password"; // and password
String date;
String t;
const char * days[] = {"So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"} ;
const char * months[] = {"Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"} ;
void setup ()
{
Serial.begin(115200); // most ESP-01's use 115200 but this could vary
timeClient.begin(); // Start the NTP UDP client
Wire.pins(4, 5);
Wire.begin(4, 5);
display.begin();
display.flipScreenVertically();
// Connect to wifi
Serial.println("");
Serial.print("Connecting to ");
Serial.print(ssid);
display.drawString(0, 10, "Connecting to Wifi...");
display.display();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi at ");
Serial.print(WiFi.localIP());
Serial.println("");
display.drawString(0, 24, "Connected.");
display.display();
delay(1000);
}
void loop()
{
if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
{
date = ""; // clear the variables
t = "";
// update the NTP client and get the UNIX UTC timestamp
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
// convert received time stamp to time_t object
time_t local, utc;
utc = epochTime;
/* // Then convert the UTC UNIX timestamp to local time
TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, -0}; //UTC - 5 hours - change this as needed
TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, -60}; //UTC - 6 hours - change this as needed
Timezone usEastern(usEDT, usEST); */
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 60}; //Central European Summer Time
TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 0}; //Central European Standard Time
Timezone CE(CEST, CET);
local = CE.toLocal(utc);
// now format the Time variables into strings with proper names for month, day etc
date += days[weekday(local)-1];
date += ", ";
date += day(local);
date += " ";
date += months[month(local)-1];
date += " ";
date += year(local);
// format the time to 12-hour format with AM/PM and no seconds
if(hour(local) < 10) // add a zero if houre is under 10
t += "0";
t += hour(local);
t += ":";
if(minute(local) < 10) // add a zero if minute is under 10
t += "0";
t += minute(local);
t += ":";
if(second(local) < 10) // add a zero if minute is under 10
t += "0";
t+= second(local);
// Display the date and time
Serial.println("");
Serial.print("Local date: ");
Serial.print(date);
Serial.println("");
Serial.print("Local time: ");
Serial.print(t);
// print the date and time on the OLED
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_24);
display.drawStringMaxWidth(64, 10, 128, t);
display.setFont(ArialMT_Plain_10);
display.drawStringMaxWidth(64, 48, 128, date);
display.fillRect(1, 0, 126*timeClient.getSeconds()/59, 2);
display.display();
}
else // attempt to connect to wifi again if disconnected
{
display.clear();
display.drawString(0, 10, "Connecting to Wifi...");
display.display();
WiFi.begin(ssid, password);
display.drawString(0, 24, "Connected.");
display.display();
delay(1000);
}
delay(1000); //Send a request to update every 10 sec (= 10,000 ms)
}
