1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M5NanoC6でOLED時計。(SSD1306)(SNTP)

Last updated at Posted at 2025-02-04

参考

x 過去ログを見よ!!!

部品

o_coq794.jpg

o_coq795.jpg

o_coq796.jpg

o_coq797.jpg

o_coq798.jpg

o_coq799.jpg

o_coq800.jpg

配線(配線は、各自、データシートを参照)

SDA - SDA
SCL - SCL
5V - 5V
GND - GND

o_coq456.jpg

書き換える


const char *ssid = "ご自宅のSSID";
const char *password = "ご自宅のパスワード";

結果

o_coq793.jpg

プログラム



//SSD1306_SNTP_M5NanoC6_1


//ヘッダーファイル
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <time.h>


//定義
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define JST 3600 * 9
const char *ssid = "ご自宅のSSID";
const char *password = "ご自宅のパスワード";


//初期化
void setup() {

  // I2Cアドレスは使用するディスプレイに合わせて変更する
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);

  //WiFiの初期化
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  delay(3000);//決め打ちなので、おかしかったら調整してね! wifiの待ち0.5*6
  configTime(JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");

}//setup


//メインループ
void loop() {

  // 画面表示をクリア
  display.clearDisplay();

  // テキストサイズを設定
  display.setTextSize(2);
  // テキスト色を設定
  display.setTextColor(WHITE);
  // テキストの開始位置を設定
  display.setCursor(0, 10);

  //時計の処理 ↓ここから
  time_t t;      //経過秒
  struct tm *tm; //時間の実体とポインター
  //static const char *wd[7] = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };

  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);

  //表示変換する パック二進化十進を文字列にする
  char cn1[16];  //桁
  cn1[0] = '0' + (tm->tm_hour) / 10; //0 時 上位
  cn1[1] = '0' + (tm->tm_hour) % 10; //1 時 下位
  cn1[2] = ':';
  cn1[3] = '0' + (tm->tm_min) / 10; //2 分 上位
  cn1[4] = '0' + (tm->tm_min) % 10; //3 分 下位
  cn1[5] = ':';
  cn1[6] = '0' + (tm->tm_sec) / 10; //4 秒 上位
  cn1[7] = '0' + (tm->tm_sec) % 10; //5 秒 下位
  cn1[8] = 0;
  //時計の処理 ↑ここまで
  
  display.println(cn1); //時刻を表示する

  // 描画バッファの内容を画面に表示
  display.display();

  delay(1000); //1秒待つ

}//loop



1
1
0

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?