ESP8266 + MAX7219 制作 WiFi 数字时钟

Posted Li-Yongjun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ESP8266 + MAX7219 制作 WiFi 数字时钟相关的知识,希望对你有一定的参考价值。

硬件

ESP8266、MAX7219

软件

Arduino IDE

连线

MAX7219ESP8266
VCC3V/5V
GNDGND
DIND7
CSD4
CLKD5

源码

/*************************连接方法*************************
  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)
*********************************************************/

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

int pinCS = 2; // Attach CS to this pin, DIN to MOSI and CLK to SCK (cf http://arduino.cc/en/Reference/SPI )
int numberOfHorizontalDisplays = 4; // 水平方向有 4 块屏幕
int numberOfVerticalDisplays   = 1; // 垂直方向有 1 块屏幕
#define _DISPLAY_ROTATE 1 // 旋转方向,1:0°,2:90°,3:180°,4:270°

const byte buffer_size = 45;
char time_value[buffer_size];
Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);

const char *ssid      = "xxx";   //输入你的 WiFi 名称
const char *password  = "xxx";   //输入你的 WiFi 密码

void setup() 

  matrix.setIntensity(15); 		// 屏幕亮度
  for (int i = 0; i < numberOfHorizontalDisplays;  i++)
    matrix.setRotation(i, _DISPLAY_ROTATE);

  matrix.fillScreen(LOW);

  matrix.drawChar(2, 0, '1', HIGH, LOW, 1);  // H
  matrix.drawChar(8, 0, '2', HIGH, LOW, 1);  // HH
  matrix.drawChar(14, 0, ':', HIGH, LOW, 1); // HH:
  matrix.drawChar(20, 0, '3', HIGH, LOW, 1); // HH:M
  matrix.drawChar(26, 0, '4', HIGH, LOW, 1); // HH:MM
  matrix.write(); // Send bitmap to display

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  Serial.begin(115200);
  Serial.println();
  Serial.printf("Flash: %d\\n", ESP.getFlashChipRealSize());
  Serial.print("Connecting");

  while (WiFi.status() != WL_CONNECTED) 
    delay(100);
    Serial.print(".");
  

  Serial.println();
  Serial.print("Connected to wifi. My address:");
  IPAddress myAddress = WiFi.localIP();
  Serial.println(myAddress);

  configTime(0 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  setenv("TZ", "CST-8", 1);

  time_t now;
  time(&now);
  while (now < 120) 
    delay(100);
    time(&now);
    Serial.println(now);
  
  WiFi.disconnect();
  WiFi.mode(WIFI_OFF);


void loop() 
  time_t now;
  char time_output[buffer_size];
  String time_str;

  time(&now);
  strftime(time_output, buffer_size, "( %a  %y-%m-%d ) ( %p %T )", localtime(&now));
  time_str = String(time_output);
  time_str.trim();
  time_str.substring(23, 28).toCharArray(time_value, 10);

  Serial.println(time_value);

  matrix.fillScreen(LOW);
  //( Sun  21-07-19 ) ( PM 12:52:12 )
  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

  delay(5000);

组装

没有外壳,买了点小积木自己组装。

完成



以上是关于ESP8266 + MAX7219 制作 WiFi 数字时钟的主要内容,如果未能解决你的问题,请参考以下文章

ESP8266(ESP-12F)案例实操 -- 8x32点阵显示(MAX7219)

ESP8266(ESP-12F) 第三方库使用 -- LedControl

用ESP8266+android,制作自己的WIFI小车(Android 软件)

用ESP8266+android,制作自己的WIFI小车

esp8266wifi模块烧录步骤

使用 ESP8266 制作 WiFi 干扰器 - 无需密码即可使用任何 WiFi