|
又是时钟,没办法,别的也不会做。其实已经制作完成3年多了,最近看着打板又便宜了,所以重新做了一个0.56寸的,比之前做的要大一些。
原来使用的是tb上买的max7219数码管模块,便宜,好用,但是偏小,有时候看不清楚。所以这次做个稍微大一些的,岁数大了,眼神不好使了。
tb上买的这种:
现在的是这样的:
这个是有问题的电路图:
为什么说有问题呢?大侠们一看就明白了,数码管的位置错了。自己第一次画图,也不明白,等焊上的时候才发现数字都错位了。只能改代码解决了。
PCB效果图:
实物图:
每天22:00到6:00是关屏的,亮度是中等,能看清楚。
以下是代码,大家随便用吧。
开机显示wifi连接密码09876543,wifi名字是esp8266-clock。手机连上以后选择一个wifi热点连接后就开始NTP时钟同步,每分钟的第31-35秒显示当前的日期。
- #include <ESP8266WiFi.h>
- #include <WiFiUdp.h>
- #include <NTPClient.h>
- #include <LedControl.h>
- #include <WiFiManager.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <DNSServer.h>
- //#define _DEBUG_
- #define _OTA_
- #ifdef _OTA_
- #include <ArduinoOTA.h>
- #endif
- const char* HOSTNAME = "esp8266-clock";
- WiFiManager wifiManager;
- MDNSResponder mdns;
- ESP8266WebServer server(80);
- const char pass[] = "09876543"; // your network password
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, "cn.ntp.org.cn", 3600 * 8, 60000 * 60);
- String displayMessage = ""; //string on the leds
- String dpBits = ""; //dots array like "11111111"
- String weekDayDots[] = {
- "00000010",
- "10000000",
- "01000000",
- "00100000",
- "00010000",
- "00001000",
- "00000100",
- "00000010" }; //sunday is 0
- boolean lcLedOn = false; //if led is on. it's false before init;
- LedControl lc = LedControl(2, 0, 1, 1); // For ESP-01: GPIO2-DIN, GPIO0->Clk, TXD->LOAD
- time_t prevDisplay = 0; // when the digital clock was displayed
- static long previousMillis = 0;
- void writeToLog(String log) {
- #ifdef _DEBUG_
- Serial.println(log);
- #endif
- }
- /**
- * deep sleep in seconds, can not sleep over 1 hour.
- */
- void deepSleepInSeconds(float seconds) {
- writeToLog(String("... Sleep for ") + seconds + " seconds");
- delay(seconds * 1000);
- }
- /**
- * deep sleep in miliseconds, can not sleep over 1 hour.
- */
- void deepSleepInMiliSeconds(float miliseconds) {
- writeToLog(String("... Sleep for ") + miliseconds + " miliseconds");
- delay(miliseconds);
- }
- int relocation[8] = {4,5,6,7,0,1,2,3};
- void ledSetString(String msg, String dp){
- if (msg.length() < 8){
- msg = msg + " ";
- }
- for(int i=0; i<8; i++) {
- lc.setChar(0,i,msg.charAt(relocation[i]),(dp.charAt(relocation[i]) == '1'));
- }
- }
- /**
- * display on leds
- */
- void sendToLedDisplay() {
- writeToLog("Display is " + displayMessage + " / Dots are " + dpBits);
- if (lcLedOn) {
- ledSetString(displayMessage, dpBits);
- }
- }
- /**
- * display on leds
- */
- void sendToLedDisplay(String toDisplay, String toDpBits) {
- if (!displayMessage.equals(toDisplay) || !dpBits.equals(toDpBits)) {
- displayMessage = toDisplay;
- dpBits = toDpBits;
- writeToLog("Display is " + displayMessage + " / Dots are " + dpBits);
- if (lcLedOn) {
- ledSetString(displayMessage, dpBits);
- }
- }
- }
- void sendToLedDisplayR2L(String toDisplay) {
- String todis = " " + toDisplay;
- String dis = todis;
- while (dis.length() > 0) {
- dis.remove(0, 1);
- sendToLedDisplay(dis, "00000000");
- delay(200);
- }
- }
- /**
- * turn off leds;
- */
- void turnOffLcLed() {
- if (lcLedOn) {
- writeToLog("Turning Off LED...");
- lcLedOn = false;
- lc.shutdown(0, true); // Enable display
- writeToLog("Turning Off LED Done.");
- }
- }
- /**
- * turn on leds;
- */
- void turnOnLcLed() {
- if (lcLedOn == false) {
- writeToLog("-- Turning On LED...");
- lcLedOn = true;
- lc.shutdown(0, false); // Enable display
- writeToLog("-- Turning On LED Done.");
- }
- }
- void testLeds() {
- String chars = " _-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- for (int j = 0; j < chars.length(); j++) {
- for (int i = 0; i < 8; i++) {
- lc.setChar(0, i, chars.charAt(j), (j % 2 == 0));
- }
- delay(200);
- }
- }
- /**
- * add a zero before the digit when digit is less then 10;
- */
- String printDigits(int digits) {
- String r = String(digits);
- if (digits < 10) {
- r = "0" + r;
- }
- return r;
- }
- /**
- * display date time to the leds
- */
- void digitalClockDisplay() {
- // digital clock display of the time
- String toDisplay = "";
- String toDpBits = "";
- String delim = "-";
- int second = timeClient.getSeconds();
- if (second > 30 && second <= 35) {
- toDisplay = timeClient.getFormattedDate("");
- } else {
- if (second % 2 == 1) {
- delim = " ";
- }
- toDisplay = printDigits(timeClient.getHours()) + delim + printDigits(timeClient.getMinutes()) + delim
- + printDigits(second);
- }
- toDpBits = weekDayDots[timeClient.getDay()];
- sendToLedDisplay(toDisplay, toDpBits);
- }
- void handleReset() {
- wifiManager.resetSettings();
- }
- void handleRoot() {
- server.send(200, "text/html", displayMessage);
- }
- void handleShow() {
- String s = server.arg("s");
- if (s.length() > 0) {
- sendToLedDisplayR2L(s);
- }
- server.send(200, "text/html", s);
- }
- void handleNotFound() {
- server.send(404, "text/plain", "404");
- }
- /**
- * init LC 7 digits led
- */
- void initLED() {
- writeToLog("Initing LED...");
- turnOnLcLed();
- writeToLog("-- LED Set to low brightness");
- lc.setIntensity(0, 3); // Set brightness level (0 is min, 15 is max)
- writeToLog("-- LED clear display");
- lc.clearDisplay(0);
- writeToLog("-- LED testing");
- //testLeds();
- writeToLog("-- LED clear display");
- lc.clearDisplay(0);
- writeToLog("Initing LED Done.");
- writeToLog("");
- }
- /**
- * init wifi
- */
- void initWifi() {
- #ifndef _DEBUG_
- wifiManager.setDebugOutput(false);
- #endif
- writeToLog("Initing WIFI...");
- WiFi.hostname(HOSTNAME);
- WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
- sendToLedDisplay(pass, "");
- wifiManager.autoConnect(HOSTNAME, pass);
- if (mdns.begin(HOSTNAME, WiFi.localIP())) {
- writeToLog("MDNS responder started");
- }
- writeToLog(
- String("-- IP number assigned by DHCP is ") + WiFi.localIP().toString());
- writeToLog("Initing WIFI Done.");
- writeToLog("");
- }
- /**
- * init ntp settings
- */
- void initNtpClient() {
- writeToLog("Initing UDP...");
- timeClient.begin();
- timeClient.update();
- }
- void initSerial() {
- #ifdef _DEBUG_
- Serial.begin(115200);
- while (!Serial) {
- delay(100);
- }
- delay(1000);
- writeToLog("");
- writeToLog("Welcome to NTP Max7219 Clock");
- #endif
- }
- void initWebServer() {
- server.on("/reset", handleReset);
- server.on("/show", handleShow);
- server.on("/", handleRoot);
- server.on("/index.html", handleRoot);
- server.onNotFound(handleNotFound);
- server.begin();
- writeToLog("HTTP server started");
- }
- void initOTA() {
- #ifdef _OTA_
- //OTA
- ArduinoOTA.setHostname(HOSTNAME);
- ArduinoOTA.setPort(8266);
- // No authentication by default
- // ArduinoOTA.setPassword((const char *)"irsvr");
- ArduinoOTA.onStart([]() {
- Serial.println("Start");
- });
- ArduinoOTA.onEnd([]() {
- Serial.println("\nEnd");
- ESP.restart();
- });
- ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
- Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
- });
- ArduinoOTA.onError([](ota_error_t error) {
- Serial.printf("Error[%u]: ", error);
- if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
- else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
- else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
- else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
- else if (error == OTA_END_ERROR) Serial.println("End Failed");
- });
- ArduinoOTA.begin();
- #endif
- }
- void setup() {
- initSerial();
- initOTA();
- initLED();
- initWifi();
- initWebServer();
- initNtpClient();
- }
- void loop() {
- previousMillis = millis();
- server.handleClient();
- mdns.update();
- #ifdef _OTA_
- ArduinoOTA.handle();
- #endif
- timeClient.update();
- if (lcLedOn) {
- digitalClockDisplay();
- }
- // turn off during night;
- if (timeClient.getHours() >= 22 || timeClient.getHours() < 6) {
- sendToLedDisplayR2L("TURNING OFF LEDS. BYE");
- turnOffLcLed();
- } else {
- turnOnLcLed();
- }
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
打赏
-
查看全部打赏
|