0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Arduino IDE + ESP-01 単体 Wi-Fi Clock

Posted at

概要

  • ESP-01 単体で Wi-Fi Clock を作成
  • USBケーブルを刺したままプログラムが書き込める USB-ESP8266 アダプタモジュールを使用

IMG_20250221_185604406.jpg

ESP8266 USB アダプタ モジュール.jpg

ソフトウエア資材

  • Arduino IDE ESP8266 on Windows 11

ハードウエア資材

  • ESP-01S
  • SSD1306(OLED)
  • USB-ESP8266アダプタモジュール

プログラム

ESP-01Clock.ino
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <time.h>

#define WIFI_SSID   "Your SSID"
#define WIFI_PASSWORD "Your Password"

#define TIMEZONE_JPN  (3600 * 9)  // JST is UTC+9hours
#define DAYLIGHTOFFSET_JPN  (0)   // No summer time
#define NTP_SERVER1   "ntp.nict.jp"  // NTP

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void displayDateTime() {
  char bufTime[32], bufDate[32];
  time_t Now = time(NULL);
  struct tm* tm = localtime(&Now);
  strftime(bufTime, sizeof(bufTime), "%T", tm);  
  strftime(bufDate, sizeof(bufDate), "%a %b %d %Y", tm);  
  Serial.printf("%s %s\n", bufTime, bufDate); // Debug
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(0, 0);
  display.println(bufTime);
  display.setTextSize(1);
  display.setCursor(0, 20);
  display.println(bufDate);
  display.display();
}

void setup() {
  Serial.begin(9600); // Serial for debug
  delay(5000);

  Wire.begin(0, 2);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) delay(500);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.println("Initializing");
  display.display();

  configTime(TIMEZONE_JPN, DAYLIGHTOFFSET_JPN, NTP_SERVER1);
}

void loop() {
  displayDateTime();
  delay(1000);
}

まとめ

最も小さなシングルボードコンピュータで WiFi 時計が作れました。
ESP-01 は、なかなか良いですね。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?