// ------------------------------------------------------------ // ライブラリインクルード部 Library include section. // ------------------------------------------------------------ #include // Wifi制御用ライブラリ #include #include // グラフィックライブラリ //#include // テキストライブラリ //結線:[SSD1306 128x64 I2C]-->[Raspberry Pi Pico] // VCC -----------------> 36pin (3v3(OUT)), // GND -----------------> 38pin (GND), // SCL -----------------> 7pin (I2C0 SCL), // SDA -----------------> 6pin (I2C0 SDA) //==================================================================================== // GPIO I2C Setting // マクロでよしなに設定してくれるので、変更する場合以外は不要 //const uint8_t I2C_SCL = 5; // SCL GP5 //const uint8_t I2C_SDA = 4; // SDA GP4 //==================================================================================== // ------------------------------------------------------------ // 定数/変数 定義部 Constant / variable definition section. // ------------------------------------------------------------ const char* ssid = "your-ssid"; // ご自分のWi-FiルータのSSIDを記述します。 "your-ssid" const char* password = "your-password"; // ご自分のWi-Fiルータのパスワードを記述します。"your-password" // 曜日表示文字 const char *weekChar[7] = { "日", "月", "火", "水", "木", "金", "土" }; // U8g2コンストラクタ U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE); // NTPによる時刻同期関数 bool setClock() { NTP.begin("ntp.nict.jp", "time.google.com"); return NTP.waitSet(); } void setup() { // Serial.begin(115200); Wire.begin(); Wire.setClock(400000); u8g2.begin(); // OLED初期化 u8g2.setContrast(1); u8g2.clearBuffer(); u8g2.setFont(u8g2_font_crox1hb_tf); u8g2.drawStr(0, 17, "WiFi connecting..."); u8g2.sendBuffer(); // WiFi接続 WiFi.begin(ssid, password); if (WiFi.status() != WL_CONNECTED) { u8g2.clearBuffer(); u8g2.drawStr(0, 17, "WiFi Connection failed"); u8g2.sendBuffer(); // Serial.println("WiFi Connection failed"); for (;;) yield(); } // NTP時刻同期 if (setClock()) { // 時刻取得失敗時は停止 u8g2.clearBuffer(); u8g2.drawStr(0, 17, "Failed to obtain time"); u8g2.sendBuffer(); // Serial.println("Failed to obtain time"); for (;;) yield(); } // WiFi切断 WiFi.disconnect(true); // 日本標準時をセット setenv("TZ", "JST-9", 1); tzset(); } void loop() { static time_t now; static struct tm *timeinfo; static int lastSec = -1; static char buf[20]; // 現在時を取得 time(&now); timeinfo = localtime(&now); // 時刻が変わっているか判定 if (lastSec != timeinfo->tm_sec) { lastSec = timeinfo->tm_sec; // Serial.print(asctime(timeinfo)); // 時刻が変わった時に描画する u8g2.clearBuffer(); // 時計用フォントをセット u8g2.setFont(u8g2_font_crox5h_tn); // 時分秒の描画 sprintf(buf, "%2d", timeinfo->tm_hour); u8g2.drawStr(0, 62, buf);//0,17 sprintf(buf, "%02d", timeinfo->tm_min); u8g2.drawStr(45, 62, buf);//45,17 sprintf(buf, "%02d", timeinfo->tm_sec); u8g2.drawStr(87, 62, buf);//87,17 // 年月日の描画 sprintf(buf, "%4d", timeinfo->tm_year + 1900); u8g2.drawStr(0, 17, buf);//0,40 sprintf(buf, "%2d", timeinfo->tm_mon + 1); u8g2.drawStr(0, 40, buf);//0,64 sprintf(buf, "%2d", timeinfo->tm_mday); u8g2.drawStr(48, 40, buf);//48,64 // 曜日()の描画 u8g2.setFont(u8g2_font_crox4h_tf); u8g2.drawStr(96, 36, "(");//96,60 u8g2.drawStr(122, 36, ")");//122,60 // 年月日時分秒の描画 // 日本語フォントをセット u8g2.setFont(u8g2_font_b16_b_t_japanese1); u8g2.drawUTF8(28, 62, "時");//28,15 u8g2.drawUTF8(71, 62, "分");//71,15 u8g2.drawUTF8(113, 62, "秒");//113,15 u8g2.drawUTF8(54, 17, "年");//54,38 u8g2.drawUTF8(28, 38, "月");//28,62 u8g2.drawUTF8(76, 38, "日");//76,62 u8g2.drawUTF8(104, 38, weekChar[timeinfo->tm_wday]);//104,62 u8g2.sendBuffer(); } yield(); }