9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ESP32 と 小型128x32OLED液晶ディスプレイ(時計表示)

Last updated at Posted at 2017-11-04

#はじめに
前回の記事の続きです。ライブラリ付属のサンプルを動かしてみましたが、ESP32に繋げているので NTP サーバより時刻を取得して表示させたいと思います。

esp32-SSD1306-clock1.jpg

#ハード、ソフト、組み立て
前回と同じ

#スケッチ
NTP1に関するところはh_nari氏のところから頂きました。m(_ _)m

esp32-SSD1306-clock.ino
#include <WiFi.h>
#include <time.h>

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// OLED Setting
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

// WiFi Setting
#define WIFI_SSID   "your-ssid"
#define WIFI_PASSWORD   "ssid-pass"
#define JST     3600*9

void setup() {
  Serial.begin(115200);
  delay(100);
  Serial.print("\n\nReset:\n");
  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  // Clear the buffer.
  display.clearDisplay();
  display.setTextColor(WHITE);

  // WiFi starting
  drawLog("WiFi connecting...");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while(WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(500);
  }
  Serial.println();
  Serial.printf("Connected, IP address: ");
  Serial.println(WiFi.localIP());
  drawLog("WiFi connected!");

  // NTP start
  configTime( JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");
  delay(1000);
}

void loop() {
  time_t t;
  struct tm *tm;
  static const char *wd[7] = {"Sun","Mon","Tue","Wed","Thr","Fri","Sat"};
  char rdate[30], rtime[30];

  t = time(NULL);
  tm = localtime(&t);
  
  Serial.printf(" %04d/%02d/%02d(%s) %02d:%02d:%02d\n",
        tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
        wd[tm->tm_wday],
        tm->tm_hour, tm->tm_min, tm->tm_sec);
  sprintf(rdate, " %04d/%02d/%02d(%s)",
        tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, wd[tm->tm_wday]);
  sprintf(rtime, " %02d:%02d:%02d", 
        tm->tm_hour, tm->tm_min, tm->tm_sec);
  drawClock(rdate, rtime);      
  delay(1000 - millis()%1000);
}

void drawClock(const char* rdate, const char* rtime) {
  display.clearDisplay();
  display.setCursor(0,0);
  
  display.setTextSize(1);
  drawText(rdate);
  
  display.setCursor(0,13);
  
  display.setTextSize(2);
  drawText(rtime);
  
  display.display();
  delay(1);
}

void drawText(const char* text) {
  for (uint8_t i=0; i < strlen(text); i++) {
    display.write(text[i]);
  }    
}

void drawLog(const char* msg) {
  display.clearDisplay();
  display.setCursor(0,0);
  display.setTextSize(1);
  drawText(msg);
  
  display.display();
  delay(1);
}

#結果

#SpecialThanks!!
https://qiita.com/h_nari/items/d0374d1e1e36b9d988c0
https://qiita.com/exabugs/items/b1b7430d185b268a1abf

9
11
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?