硬件
ESP8266、MAX7219
软件
Arduino IDE
连线
MAX7219 | ESP8266 |
---|
VCC | 3V/5V | GND | GND | DIN | D7 | CS | D4 | CLK | D5 |
源码
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
#include <time.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
int pinCS = 2;
int numberOfHorizontalDisplays = 4;
int numberOfVerticalDisplays = 1;
#define _DISPLAY_ROTATE 1
const byte buffer_size = 45;
char time_value[buffer_size];
Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
const char *ssid = "xxx";
const char *password = "xxx";
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);
matrix.drawChar(8, 0, '2', HIGH, LOW, 1);
matrix.drawChar(14, 0, ':', HIGH, LOW, 1);
matrix.drawChar(20, 0, '3', HIGH, LOW, 1);
matrix.drawChar(26, 0, '4', HIGH, LOW, 1);
matrix.write();
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);
matrix.drawChar(2, 0, time_value[0], HIGH, LOW, 1);
matrix.drawChar(8, 0, time_value[1], HIGH, LOW, 1);
matrix.drawChar(14, 0, time_value[2], HIGH, LOW, 1);
matrix.drawChar(20, 0, time_value[3], HIGH, LOW, 1);
matrix.drawChar(26, 0, time_value[4], HIGH, LOW, 1);
matrix.write();
delay(5000);
}
组装
没有外壳,买了点小积木自己组装。
完成
|