M5 Atom+OLED ESP32+ssd1306(0.42/0.49inch i2c) 와이파이 자동 ntp 타이밍 클록

#include 
#include 
#include 
#include 
#include 
#include                // Include NTPClient library
#include                  // Include Arduino time library

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 21, /* data=*/ 25, /* reset=*/ U8X8_PIN_NONE);

const char *ssid     = "123";
const char *password = "123";
 
WiFiUDP ntpUDP;
 
// 'time.nist.gov' is used (default server) with +1 hour offset (3600 seconds) 60 seconds (60000 milliseconds) update interval
NTPClient timeClient(ntpUDP, "cn.ntp.org.cn", 8*3600, 60000);
 
char Time[] = "TIME:00:00:00";
char Date[] = "DATE:2020/00/00";
char sTime[] = "00:00:00";
byte last_second, second_, minute_, hour_, day_, month_;
int year_;

void setup(void) {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
 
  Serial.print("Connecting.");
  while ( WiFi.status() != WL_CONNECTED ) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("connected");
 
  timeClient.begin();
  
  u8g2.begin();
  u8g2.enableUTF8Print();
}

void drawTime(void)
{
  timeClient.update();
  unsigned long unix_epoch = timeClient.getEpochTime();    // Get Unix epoch time from the NTP server
 
  second_ = second(unix_epoch);

  if (last_second != second_) {
 
    minute_ = minute(unix_epoch);
    hour_   = hour(unix_epoch);
    day_    = day(unix_epoch);
    month_  = month(unix_epoch);
    year_   = year(unix_epoch);
 
    sTime[7] = second_ % 10 + 48;
    sTime[6] = second_ / 10 + 48;
    sTime[4]  = minute_ % 10 + 48;
    sTime[3]  = minute_ / 10 + 48;
    sTime[1]  = hour_   % 10 + 48;
    sTime[0]  = hour_   / 10 + 48;

    Time[12] = second_ % 10 + 48;
    Time[11] = second_ / 10 + 48;
    Time[9]  = minute_ % 10 + 48;
    Time[8]  = minute_ / 10 + 48;
    Time[6]  = hour_   % 10 + 48;
    Time[5]  = hour_   / 10 + 48;
 
    Date[13]  = day_   / 10 + 48;
    Date[14]  = day_   % 10 + 48;
    Date[10]  = month_  / 10 + 48;
    Date[11]  = month_  % 10 + 48;
    Date[7] = (year_   / 10) % 10 + 48;
    Date[8] = year_   % 10 % 10 + 48;
 
    // Send time and date to serial monitor
    Serial.println(Time);
    Serial.println(Date);
 
    last_second = second_;
  }
  
  u8g2.setFontMode(1);	// Transparent
  u8g2.setFontDirection(0);
  u8g2.setFont(u8g2_font_7x14_mf);
  u8g2.drawStr(38, 62, sTime);
//  u8g2.drawStr(34, 40, Date);
    
//  u8g2.drawHLine(32, 32, 1); //0.49 
//  u8g2.drawHLine(95, 63, 1);
    
//    u8g2.drawHLine(28, 24, 1); //0.42 
//    u8g2.drawHLine(99, 63, 1);

}

void loop(void) {
  u8g2.clearBuffer();

  u8g2.setFont(u8g2_font_unifont_t_chinese2);  // use chinese2 for all the glyphs of " "
  //u8g2.setFont(u8g2_font_b10_t_japanese1);  // all the glyphs of "こんにちは " are already included in japanese1: Lerning Level 1-6
  u8g2.setFontDirection(0);

    u8g2.setCursor(34, 50);
    u8g2.print(" ");    // Chinese "Hello World" 
    //u8g2.print("こんにちは ");    // Japanese "Hello World" 


  
  drawTime();
  u8g2.sendBuffer();
  delay(1000);
}

좋은 웹페이지 즐겨찾기