1

I would like to change this clock to u8g2.
It works fine with the esp8266 and oled 128x64.

ESP8266 with 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)
}
  • 1
    Without really looking at your code: You will have to replace all code, that does something with the display, with code for the new display library. Have you tried that? What was your problem there? – chrisl Apr 4 '19 at 14:24
  • So I have a litle problem with: display.fillRect(1, 0, 126*timeClient.getSeconds()/59, 2); old u8g2.fillRect(1, 0, 126*timeClient.getSeconds()/59, 2); -> new 'class U8G2_SSD1306_128X64_NONAME_F_SW_I2C' has no member named 'fillRect' and display.drawStringMaxWidth(64, 48, 128, date); -> old u8g2.drawStringMaxWidth(64, 48, 128, date); -> new – else Apr 13 '19 at 9:18
  • 1. Please edit your question do add these information. 2. Since these are completely different libraries, the functions won't be the same. You cannot just rename the display object for the new library. You have to go through the code line by line and replace each call to the former library by a suitable call to the new library, that will reach the goal of that line. Changing between different libraries is mostly a complex task (unless they are really designed for being exchangeable for one another. Have you searched the documentation of the new library for a fitting function? – chrisl Apr 13 '19 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.