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?

XIAO ESP32C6+SSD1306、時計で遊ぶ。((S)シンプル)NTP

Last updated at Posted at 2025-04-17

参考

いろいろ注意

  • 過去ログを見よ!!!
  • 親切な人からコメントをいただいたので小修正(0418)
  • アンテナ系が抜けてた。追加(0418)

  //アンテナの設定
  pinMode(WIFI_ENABLE, OUTPUT); // pinMode(3, OUTPUT);
  digitalWrite(WIFI_ENABLE, LOW); // digitalWrite(3, LOW); // Activate RF switch control
  delay(100);
  pinMode(WIFI_ANT_CONFIG, OUTPUT); // pinMode(14, OUTPUT);
  digitalWrite(WIFI_ANT_CONFIG, LOW); // digitalWrite(14, LOW); // Use IN antenna

  • 3.2.0

  • 全て秋月で入手(USBケーブルも含めて)(PC、モニターは、除く)

  • インターネット接続失敗すると9:00:00からスタートするはず

  • ネット接続時には、70mAぐらい、食うらしい(インターネット情報)

  • 適切に書き換え(アップする時は、気を付けよう)


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

  • 1時間ごとの自動時間合わせ

  //set time
  static int set_1H = 0;
  if ( set_1H > (3600-1) ){ // 1 hour ?
    set_1H = 0; //counter reset
    configTzTime("JST-9", "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");
  set_1H++;

結果

image_original(20).jpg

イメージ

image_original(19).jpg

プログラム




//SSD1306_SNTP_XIAO_ESP32C6_3


//ヘッダーファイル
#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);
const char *ssid = "ご自宅のSSID";
const char *password = "ご自宅のパスワード";


//初期化
void setup() {

  //アンテナの設定
  pinMode(WIFI_ENABLE, OUTPUT); // pinMode(3, OUTPUT);
  digitalWrite(WIFI_ENABLE, LOW); // digitalWrite(3, LOW); // Activate RF switch control
  delay(100);
  pinMode(WIFI_ANT_CONFIG, OUTPUT); // pinMode(14, OUTPUT);
  digitalWrite(WIFI_ANT_CONFIG, LOW); // digitalWrite(14, LOW); // Use IN antenna

  //I2Cの初期化
  Wire.begin(D9,D10); //XIAO ESP32C6+SSD1306
  //Wire.begin(D10,D9); //XIAO ESP32C6+GROVE

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

  //WiFiの初期化
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  delay(3000);//決め打ちなので、おかしかったら調整してね! wifiの待ち0.5*6
  configTzTime("JST-9", "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; //時間の実体とポインター

  t = time(NULL);     //経過秒を取得する
  tm = localtime(&t); //経過秒を時間の構造体に変換する
  display.printf("%02d:%02d:%02d\n", tm->tm_hour, tm->tm_min, tm->tm_sec); //時刻を表示する

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

  //set time
  static int set_1H = 0;
  if ( set_1H > (3600-1) ){ // 1 hour ?
    set_1H = 0; //counter reset
    configTzTime("JST-9", "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");
  }
  set_1H++;

  delay(1000); //1秒待つ

}//loop



1
1
3

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?