数码之家

 找回密码
 立即注册
搜索
查看: 2811|回复: 7

[综合] 懒癌第三期 ESP266+MAX7219制作3线数码管NTP时钟

[复制链接]
发表于 2020-4-24 08:07:51 | 显示全部楼层 |阅读模式

爱科技、爱创意、爱折腾、爱极致,我们都是技术控

您需要 登录 才可以下载或查看,没有账号?立即注册

x



又是时钟,没办法,别的也不会做。其实已经制作完成3年多了,最近看着打板又便宜了,所以重新做了一个0.56寸的,比之前做的要大一些。

原来使用的是tb上买的max7219数码管模块,便宜,好用,但是偏小,有时候看不清楚。所以这次做个稍微大一些的,岁数大了,眼神不好使了。

tb上买的这种:
led.png

现在的是这样的:

IMG_20200424_074125.jpg

这个是有问题的电路图:

Schematic_ESP8266-MAX7219_CLOCK_2020-04-24_07-44-14.png

为什么说有问题呢?大侠们一看就明白了,数码管的位置错了。自己第一次画图,也不明白,等焊上的时候才发现数字都错位了。只能改代码解决了。

PCB效果图:
pcb1.png
pcb2.png

实物图:
IMG_20200424_074144.jpg IMG_20200424_074153.jpg IMG_20200424_074211.jpg IMG_20200424_074933.jpg

每天22:00到6:00是关屏的,亮度是中等,能看清楚。

以下是代码,大家随便用吧。

开机显示wifi连接密码09876543,wifi名字是esp8266-clock。手机连上以后选择一个wifi热点连接后就开始NTP时钟同步,每分钟的第31-35秒显示当前的日期。



  1. #include <ESP8266WiFi.h>
  2. #include <WiFiUdp.h>
  3. #include <NTPClient.h>
  4. #include <LedControl.h>
  5. #include <WiFiManager.h>
  6. #include <ESP8266WebServer.h>
  7. #include <ESP8266mDNS.h>
  8. #include <DNSServer.h>

  9. //#define _DEBUG_
  10. #define _OTA_

  11. #ifdef _OTA_
  12. #include <ArduinoOTA.h>
  13. #endif
  14. const char* HOSTNAME = "esp8266-clock";
  15. WiFiManager wifiManager;
  16. MDNSResponder mdns;
  17. ESP8266WebServer server(80);
  18. const char pass[] = "09876543";       // your network password

  19. WiFiUDP ntpUDP;
  20. NTPClient timeClient(ntpUDP, "cn.ntp.org.cn", 3600 * 8, 60000 * 60);

  21. String displayMessage = "";   //string on the leds
  22. String dpBits = "";   //dots array like "11111111"
  23. String weekDayDots[] = {
  24.                          "00000010",
  25.                          "10000000",
  26.                          "01000000",
  27.                          "00100000",
  28.                          "00010000",
  29.                          "00001000",
  30.                          "00000100",
  31.                          "00000010" };  //sunday is 0
  32. boolean lcLedOn = false;   //if led is on. it's false before init;
  33. LedControl lc = LedControl(2, 0, 1, 1); // For ESP-01: GPIO2-DIN, GPIO0->Clk, TXD->LOAD

  34. time_t prevDisplay = 0; // when the digital clock was displayed
  35. static long previousMillis = 0;

  36. void writeToLog(String log) {
  37. #ifdef _DEBUG_
  38.   Serial.println(log);
  39. #endif
  40. }

  41. /**
  42. * deep sleep in seconds, can not sleep over 1 hour.
  43. */
  44. void deepSleepInSeconds(float seconds) {
  45.   writeToLog(String("... Sleep for ") + seconds + " seconds");
  46.   delay(seconds * 1000);
  47. }
  48. /**
  49. * deep sleep in miliseconds, can not sleep over 1 hour.
  50. */
  51. void deepSleepInMiliSeconds(float miliseconds) {
  52.   writeToLog(String("... Sleep for ") + miliseconds + " miliseconds");
  53.   delay(miliseconds);
  54. }

  55. int relocation[8] = {4,5,6,7,0,1,2,3};

  56. void ledSetString(String msg, String dp){
  57.   if (msg.length() < 8){
  58.     msg = msg + "        ";
  59.   }
  60.   for(int i=0; i<8; i++) {
  61.     lc.setChar(0,i,msg.charAt(relocation[i]),(dp.charAt(relocation[i]) == '1'));
  62.   }
  63. }
  64. /**
  65. * display on leds
  66. */
  67. void sendToLedDisplay() {
  68.   writeToLog("Display is " + displayMessage + " / Dots are " + dpBits);

  69.   if (lcLedOn) {
  70.     ledSetString(displayMessage, dpBits);
  71.   }
  72. }

  73. /**
  74. * display on leds
  75. */
  76. void sendToLedDisplay(String toDisplay, String toDpBits) {
  77.   if (!displayMessage.equals(toDisplay) || !dpBits.equals(toDpBits)) {
  78.     displayMessage = toDisplay;
  79.     dpBits = toDpBits;
  80.     writeToLog("Display is " + displayMessage + " / Dots are " + dpBits);

  81.     if (lcLedOn) {
  82.       ledSetString(displayMessage, dpBits);
  83.     }
  84.   }
  85. }

  86. void sendToLedDisplayR2L(String toDisplay) {
  87.   String todis = "        " + toDisplay;
  88.   String dis = todis;
  89.   while (dis.length() > 0) {
  90.     dis.remove(0, 1);
  91.     sendToLedDisplay(dis, "00000000");
  92.     delay(200);
  93.   }
  94. }

  95. /**
  96. * turn off leds;
  97. */
  98. void turnOffLcLed() {
  99.   if (lcLedOn) {
  100.     writeToLog("Turning Off LED...");
  101.     lcLedOn = false;
  102.     lc.shutdown(0, true);  // Enable display
  103.     writeToLog("Turning Off LED Done.");
  104.   }
  105. }

  106. /**
  107. * turn on leds;
  108. */
  109. void turnOnLcLed() {
  110.   if (lcLedOn == false) {
  111.     writeToLog("-- Turning On LED...");
  112.     lcLedOn = true;
  113.     lc.shutdown(0, false);  // Enable display
  114.     writeToLog("-- Turning On LED Done.");
  115.   }
  116. }

  117. void testLeds() {
  118.   String chars = " _-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  119.   for (int j = 0; j < chars.length(); j++) {
  120.     for (int i = 0; i < 8; i++) {
  121.       lc.setChar(0, i, chars.charAt(j), (j % 2 == 0));
  122.     }
  123.     delay(200);
  124.   }
  125. }
  126. /**
  127. * add a zero before the digit when digit is less then 10;
  128. */
  129. String printDigits(int digits) {
  130.   String r = String(digits);
  131.   if (digits < 10) {
  132.     r = "0" + r;
  133.   }
  134.   return r;
  135. }

  136. /**
  137. * display date time to the leds
  138. */
  139. void digitalClockDisplay() {
  140.   // digital clock display of the time
  141.   String toDisplay = "";
  142.   String toDpBits = "";
  143.   String delim = "-";
  144.   int second = timeClient.getSeconds();
  145.   if (second > 30 && second <= 35) {
  146.     toDisplay = timeClient.getFormattedDate("");
  147.   } else {
  148.     if (second % 2 == 1) {
  149.       delim = " ";
  150.     }
  151.     toDisplay = printDigits(timeClient.getHours()) + delim + printDigits(timeClient.getMinutes()) + delim
  152.         + printDigits(second);
  153.   }
  154.   toDpBits = weekDayDots[timeClient.getDay()];

  155.   sendToLedDisplay(toDisplay, toDpBits);
  156. }



  157. void handleReset() {
  158.   wifiManager.resetSettings();
  159. }

  160. void handleRoot() {
  161.   server.send(200, "text/html", displayMessage);
  162. }

  163. void handleShow() {
  164.   String s = server.arg("s");
  165.   if (s.length() > 0) {
  166.     sendToLedDisplayR2L(s);
  167.   }
  168.   server.send(200, "text/html", s);
  169. }

  170. void handleNotFound() {
  171.   server.send(404, "text/plain", "404");
  172. }

  173. /**
  174. * init LC 7 digits led
  175. */
  176. void initLED() {
  177.   writeToLog("Initing LED...");
  178.   turnOnLcLed();
  179.   writeToLog("-- LED Set to low brightness");
  180.   lc.setIntensity(0, 3); // Set brightness level (0 is min, 15 is max)
  181.   writeToLog("-- LED clear display");
  182.   lc.clearDisplay(0);
  183.   writeToLog("-- LED testing");
  184.   //testLeds();
  185.   writeToLog("-- LED clear display");
  186.   lc.clearDisplay(0);
  187.   writeToLog("Initing LED Done.");
  188.   writeToLog("");
  189. }

  190. /**
  191. * init wifi
  192. */
  193. void initWifi() {

  194. #ifndef _DEBUG_
  195.   wifiManager.setDebugOutput(false);
  196. #endif

  197.   writeToLog("Initing WIFI...");

  198.   WiFi.hostname(HOSTNAME);
  199.   WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
  200.   sendToLedDisplay(pass, "");

  201.   wifiManager.autoConnect(HOSTNAME, pass);
  202.   if (mdns.begin(HOSTNAME, WiFi.localIP())) {
  203.     writeToLog("MDNS responder started");
  204.   }

  205.   writeToLog(
  206.       String("-- IP number assigned by DHCP is ") + WiFi.localIP().toString());
  207.   writeToLog("Initing WIFI Done.");
  208.   writeToLog("");
  209. }

  210. /**
  211. * init ntp settings
  212. */
  213. void initNtpClient() {
  214.   writeToLog("Initing UDP...");
  215.   timeClient.begin();
  216.   timeClient.update();
  217. }

  218. void initSerial() {
  219. #ifdef _DEBUG_
  220.   Serial.begin(115200);
  221.   while (!Serial) {
  222.     delay(100);
  223.   }
  224.   delay(1000);
  225.   writeToLog("");
  226.   writeToLog("Welcome to NTP Max7219 Clock");
  227. #endif
  228. }

  229. void initWebServer() {
  230.   server.on("/reset", handleReset);
  231.   server.on("/show", handleShow);
  232.   server.on("/", handleRoot);
  233.   server.on("/index.html", handleRoot);
  234.   server.onNotFound(handleNotFound);
  235.   server.begin();
  236.   writeToLog("HTTP server started");
  237. }

  238. void initOTA() {
  239. #ifdef _OTA_
  240. //OTA
  241.   ArduinoOTA.setHostname(HOSTNAME);
  242.   ArduinoOTA.setPort(8266);
  243.   // No authentication by default
  244.   // ArduinoOTA.setPassword((const char *)"irsvr");

  245.   ArduinoOTA.onStart([]() {
  246.     Serial.println("Start");
  247.   });
  248.   ArduinoOTA.onEnd([]() {
  249.     Serial.println("\nEnd");
  250.     ESP.restart();
  251.   });
  252.   ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  253.     Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  254.   });
  255.   ArduinoOTA.onError([](ota_error_t error) {
  256.     Serial.printf("Error[%u]: ", error);
  257.     if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  258.     else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  259.     else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  260.     else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  261.     else if (error == OTA_END_ERROR) Serial.println("End Failed");
  262.   });
  263.   ArduinoOTA.begin();
  264. #endif
  265. }
  266. void setup() {
  267.   initSerial();
  268.   initOTA();
  269.   initLED();
  270.   initWifi();
  271.   initWebServer();
  272.   initNtpClient();
  273. }

  274. void loop() {
  275.   previousMillis = millis();
  276.   server.handleClient();

  277.   mdns.update();
  278. #ifdef _OTA_
  279.   ArduinoOTA.handle();
  280. #endif
  281.   timeClient.update();

  282.   if (lcLedOn) {
  283.     digitalClockDisplay();
  284.   }
  285.   // turn off during night;
  286.   if (timeClient.getHours() >= 22 || timeClient.getHours() < 6) {
  287.     sendToLedDisplayR2L("TURNING OFF LEDS. BYE");
  288.     turnOffLcLed();
  289.   } else {
  290.     turnOnLcLed();
  291.   }
  292. }
复制代码


打赏

参与人数 3家元 +38 收起 理由
wushou548 + 15
zidian + 8
人艰不拆了 + 15

查看全部打赏

 楼主| 发表于 2020-4-24 08:09:06 | 显示全部楼层
二楼先站上吧
回复 支持 反对

使用道具 举报

发表于 2020-4-24 19:01:23 | 显示全部楼层
我也做过一个,功能还要多很多,还有收音机。ota不如网页升级固件好。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-24 19:21:15 来自手机浏览器 | 显示全部楼层
kindzhon 发表于 2020-4-24 19:01
我也做过一个,功能还要多很多,还有收音机。ota不如网页升级固件好。

厉害厉害!ota升级至少为了方便测试,现在发现功能越简单越好,太复杂了反而不好玩
回复 支持 反对

使用道具 举报

发表于 2020-4-26 22:15:56 | 显示全部楼层
我也做了一个,不过更简单,使用ESPHOME,还可以接入homeassistant,实现跟多功能
回复 支持 反对

使用道具 举报

发表于 2020-6-12 16:43:22 来自手机浏览器 | 显示全部楼层
再弄个点阵的

更显得高大上~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-12 21:12:59 | 显示全部楼层
shx1971 发表于 2020-6-12 16:43
再弄个点阵的

更显得高大上~

哈哈哈,我有点阵的。也是max7219的。4位的太小了,有个8位的。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-12 21:13:55 | 显示全部楼层
zrh5057 发表于 2020-4-26 22:15
我也做了一个,不过更简单,使用ESPHOME,还可以接入homeassistant,实现跟多功能

我做的那个可以直接连iphone上的家庭,不用装app,直接siri就能开关灯。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2024-4-16 19:44 , Processed in 0.343201 second(s), 14 queries , Redis On.

Powered by Discuz!

© 2006-2023 smzj.net

快速回复 返回顶部 返回列表