ESP8266で4連8×8ドットマトリクスLEDで時計を表示してみる:【ESP8266】2.2インチ ディスプレイILI9341にDHT22で湿温度と時計を表示する

こちらは古い記事で下書き状態だったので公開します。だいぶ前に制作した4連8×8ドットマトリクスLEDとESP8266を使って時計を作った時のメモです。

完成した時計はこちら↓

この記事は複数のページに別れています
  1. ESP8266で4連8x8ドットマトリクスLEDで時計を表示してみる (今このページを見ています)
  2. ESP-01とUSBシリアル変換器FT232ELを接続してみる

作ってから半年以上経ちますが今でも現役で寝室で時計として使ってます。

4連8×8ドットマトリクス LED時計 材料

作るのに必要な機材はESP8266と4連8×8ドットマトリクスLEDのみ。

250個 六角スペーサー ボルトナット 六角ねじナイロン素材 六角スペーサー 六

250個 六角スペーサー ボルトナット 六角ねじナイロン素材 六角スペーサー 六
1,500円前後

できればESP8266と4連8×8ドットマトリクス LEDを接続するのに以下のようなボルトとナットがあったほうがいいです。

HYFJP A r -d u i n oのためのモジュール3pcsのMAX721

HYFJP A r -d u i n oのためのモジュール3pcsのMAX721
HYFJP

10,000円前後

Aceirmc 3ピース ESP8266 シリアルワイヤレスモジュール CH34

Aceirmc 3ピース ESP8266 シリアルワイヤレスモジュール CH34
1,500円前後

How to Control MAX7219 LED Matrix with ESP8266 WiFi Moduleを真似したが失敗

まずはこちらのページの通りに作ってみましたが?なぜかうまくいかなかった。

301 Moved Permanently

ツール > ライブラリを管理で「MD_MAX7219」を検索してMD_MAX72XXをインストールします。

ライブラリを編集する必要があると書いてあるが?MAX72xx.hが見当たらない。MD_MAX72xx.hファイルはある

これは忘れちゃったけどうまくいかなかった。

ESP8266-MAX7219-LED-4x8x8-Matrix-Clockを試す

続いてこちらの↓Youtubeに出ているやり方を試してみました。

こちらに↓説明などもあります。

GitHub - G6EJD/ESP8266-MAX7219-LED-4x8x8-Matrix-Clock
Contribute to G6EJD/ESP8266-MAX7219-LED-4x8x8-Matrix-Clock development by creating an account on GitHub.

ライブラリ

こちらのページからClone or downloadを押してZipファイルをダウンロードして、Arduino IDEのスケッチ > ライブラリをインクルート > .Zip形式のライブラリをインストールを押してライブラリをインストールします。

GitHub - markruys/arduino-Max72xxPanel: Arduino interface for Adafruit-GFX to control a set of 8x8 LEDs with a MAX7219 or MAX7221
Arduino interface for Adafruit-GFX to control a set of 8x8 LEDs with a MAX7219 or MAX7221 - markruys/arduino-Max72xxPanel

スケッチ

スケッチはこちら↓そのまんま使えました。

ESP8266-MAX7219-LED-4x8x8-Matrix-Clock/ESP8266_MAX7219_Time.ino at master · G6EJD/ESP8266-MAX7219-LED-4x8x8-Matrix-Clock
Contribute to G6EJD/ESP8266-MAX7219-LED-4x8x8-Matrix-Clock development by creating an account on GitHub.

Wifiに接続してNTPサーバーで時刻を取得するのでお使いのWifiのSSIDとパスワードを入力する必要があります。

#define WIFI_SSID "xxxxxxxx"
#define WIFI_PASSWORD "xxxxx"

日本時刻に変更

時刻も日本時間に変更します。こちらを参考に↓

レプリケーションおよびイベント・パブリッシング - TZ 環境変数の時間帯コード
TZ 環境変数の値として時間帯コードを使用します。

GMT0BST」を「JST-9」にする。

短いループで年・月・日・曜日が表示される

しょっちゅう年月日が表示されて時計表示が短いのでloop内で最後のほうのdelayを 10分くらいdelay(60000);にするとよさそう。

忘れたけど多分変更

したのでスケッチを書いておきます。

#ifdef ESP32
  #include <WiFi.h>
#else
  #include <ESP8266WiFi.h>
#endif
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
#include <time.h>

int pinCS = 02; // Attach CS to this pin, DIN to MOSI and CLK to SCK (cf http://arduino.cc/en/Reference/SPI )
int numberOfHorizontalDisplays = 4;
int numberOfVerticalDisplays   = 1;
char time_value[20];

// LED Matrix Pin -> ESP8266 Pin
// Vcc            -> 3v  (3V on NodeMCU 3V3 on WEMOS)
// Gnd            -> Gnd (G on NodeMCU)
// DIN            -> D7  (Same Pin for WEMOS)
// CS             -> D4  (Same Pin for WEMOS)
// CLK            -> D5  (Same Pin for WEMOS)

Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);

int wait = 70; // In milliseconds

int spacer = 1;
int width  = 5 + spacer; // The font width is 5 pixels

//Wi-Fi情報
#define WIFI_SSID "Xiaomi_wifi"
#define WIFI_PASSWORD "xxxxxxxxxx"

void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  configTime(0 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  setenv("TZ", "JST-9,M3.5.0/01,M10.5.0/02",1);
  matrix.setIntensity(0); // Use a value between 0 and 15 for brightness
  matrix.setRotation(0, 1);    // The first display is position upside down
  matrix.setRotation(1, 1);    // The first display is position upside down
  matrix.setRotation(2, 1);    // The first display is position upside down
  matrix.setRotation(3, 1);    // The first display is position upside down
}

void loop() {
  matrix.fillScreen(LOW);
  time_t now = time(nullptr);
  String time = String(ctime(&now));
  time.trim();
  Serial.println(time);
  time.substring(11,19).toCharArray(time_value, 10); 
  matrix.drawChar(2,0, time_value[0], HIGH,LOW,1); // H
  matrix.drawChar(8,0, time_value[1], HIGH,LOW,1); // HH
  matrix.drawChar(14,0,time_value[2], HIGH,LOW,1); // HH:
  matrix.drawChar(20,0,time_value[3], HIGH,LOW,1); // HH:M
  matrix.drawChar(26,0,time_value[4], HIGH,LOW,1); // HH:MM
  matrix.write(); // Send bitmap to display
  
  //String timepri = time.substring(11,19);
  //Serial.println(timepri);
  delay(60000);
  display_message(time); // Display time in format 'Wed, Mar 01 16:03:20 2017
}

void display_message(String message){
   for ( int i = 0 ; i < width * message.length() + matrix.width() - spacer; i++ ) {
    //matrix.fillScreen(LOW);
    int letter = i / width;
    int x = (matrix.width() - 1) - i % width;
    int y = (matrix.height() - 8) / 2; // center the text vertically
    while ( x + width - spacer >= 0 && letter >= 0 ) {
      if ( letter < message.length() ) {
        matrix.drawChar(x, y, message[letter], HIGH, LOW, 1); // HIGH LOW means foreground ON, background off, reverse to invert the image
      }
      letter--;
      x -= width;
    }
    matrix.write(); // Send bitmap to display
    delay(wait/2);
  }
}

なかなか使える時計です。

3Dプリンタでケース作ってもいいかもしれません。

コメント

タイトルとURLをコピーしました