痞酷網_PIGOO

 找回密碼
 立即註冊
!!! [系統偵測到廣告阻擋軟體] !!!

如果您覺得痞酷網對您有些許幫助,或者您認同痞酷網的理想,

那麼希望您將痞酷網設定為白名單.

並請在上論壇的時候,動動您的手指,用行動支持我們.

謝謝!
查看: 4970|回復: 3

[實做與討論] 網路時鐘 NODEMCU 應用

[複製鏈接]
發表於 2019-4-17 21:53:18 | 顯示全部樓層 |閱讀模式
本文章最後由 antlu 於 2019-4-17 09:53 PM 編輯

這是一個非常簡單的電路,一共只有三個零件組,LGC12002 兩個,NODMCU一個 !!
NODEMCU 網路搜尋一下,一大堆應用,價格 露天拍賣 約120元台幣,他透過家裡的網路分享器向相關網路校時的公司,提出時間需求,這一段資料我是從相關的網站之中COPY下來的,原創是用來驅動 LCM 搭配 溫溼度模組 DHT11來顯示時間溫度溼度,由程式裡面36000秒提出校時一次.希望大家喜歡!!

程式:
  1. //20190414 modify for cs pin control
  2. //201904120 clock from internet & display 2 PT6961
  3. #include <ESP8266WiFi.h>
  4. #include <WiFiUdp.h>
  5. #include <TimeLib.h>
  6. #include <PT6961.h>
  7. #include <DNSServer.h> //20190416
  8. #include <ESP8266WebServer.h> //20190416
  9. #include <WiFiManager.h>//20190416
  10. WiFiManager wifiManager;//20190416
  11. const int DIN1 =5;//d1   
  12. const int CLK1 = 4;//d2
  13. const int CS1 = 0;//d3
  14. const int DIN = 5;//d1
  15. const int CLK = 4;//d2
  16. const int CS = 2; //d4
  17. PT6961 SEG7(DIN,CLK,CS);  
  18. PT6961 SEG8(DIN1,CLK1,CS1);  
  19. boolean colon=false;      
  20. char hrmn_Array[4];
  21. char mondy_Array[4];

  22. const char* ntpServerName = "us.pool.ntp.org";
  23. const int timeZone = 8;
  24. const int BUTTON_PIN = D4;

  25. WiFiUDP Udp;
  26. int localPort = 8888;

  27. unsigned long PreviousClick = 0;
  28. unsigned long PreviousDisplay = 0;

  29. void setup() {
  30.    wifiManager.autoConnect("antlu");
  31.     SEG8.initDisplay();
  32.     SEG7.initDisplay();  
  33.     SEG7.sendCmd(0x88);//bright 1/16
  34.     SEG8.sendCmd(0x88);//bright 4/16
  35.     // PT6961初始化
  36.   Serial.begin(9600);
  37. //  WiFi.begin(ssid,password);
  38.   int pointcount = 0;
  39. while(WiFi.status() != WL_CONNECTED)
  40. {
  41.    delay(500);
  42.    if(pointcount >= 16)
  43.    {
  44.     Serial.println("over 16");
  45.     pointcount = 0;
  46.    }
  47.    Serial.print(".");
  48.    pointcount ++;
  49. }

  50.   Serial.print("IP number assigned by DHCP is ");
  51.   Serial.println(WiFi.localIP());
  52.   Serial.println("Starting UDP");
  53.   Udp.begin(localPort);
  54.   Serial.print("Local port: ");
  55.   Serial.println(Udp.localPort());
  56.   Serial.println("waiting for sync");
  57.   setSyncProvider(getNtpTime); // set the external time provider
  58.   setSyncInterval(36000); // set the number of seconds between re-sync

  59. // attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), IntCallback, RISING);//interrupt routine


  60. }

  61. void loop() {
  62. unsigned long MillisTemp = millis();

  63.   if ((MillisTemp - PreviousDisplay) > 800)
  64.   { // Refresh screen every 800 milliseconds
  65. //     DisplayType2();
  66.       DisplayType3();
  67.       colon=!colon;
  68.       Serial.println("------> ");
  69.     PreviousDisplay = MillisTemp;
  70.   }
  71. }

  72. void IntCallback(){
  73.   unsigned long MillisTemp = millis();  
  74.   if ((MillisTemp - PreviousClick) > 300) { // Debouncing
  75.     Serial.println("Button Click");
  76.     PreviousClick = MillisTemp;
  77.   }
  78. }

  79. void DisplayType2()
  80. {
  81.   // Print YEAR-MONTH-DAY
  82.   Serial.print(year());
  83.   Serial.print("-");
  84.   Serial.print(month());
  85.   Serial.print("-");  
  86.   Serial.println(day());
  87.   Serial.print(hour());
  88.   Serial.print("-");
  89.   Serial.print(minute());
  90.   Serial.print("-");  
  91.   Serial.println(second());
  92.   //
  93.   // Print HOUR:MIN:SEC WEEK
  94.   //

  95. }
  96. void DisplayType3()
  97. {
  98.   int mon_dy1 = month_day_conv();
  99.   int hr_mn1 = hour_minute_conv();
  100.     SEG7.sendNum(mon_dy1,0);
  101.     SEG8.sendNum(hr_mn1,colon);
  102.   delay(10);  
  103. }


  104. int hour_minute_conv(void)
  105. {
  106.   int hr = 100 * hour();
  107.   int mn = minute();
  108.   int hr_mn = (hr+mn);
  109.    Serial.print("hr_mn = ");
  110.    Serial.println(hr_mn);
  111.   return hr_mn;
  112. }

  113. int month_day_conv(void)
  114. {
  115.    int mon = 100 * month();
  116.    int dy = day();
  117.    int mon_dy = mon+dy;
  118.    Serial.print("mon_dy = ");
  119.    Serial.println(mon_dy);
  120.    return mon_dy;
  121. }

  122. const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
  123. byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

  124. time_t getNtpTime()
  125. {
  126.   IPAddress ntpServerIP; // NTP server's ip address
  127.   for (int i = 0; i < 6; i++) { // Retry 6 times if "No NTP Response"
  128.     while (Udp.parsePacket() > 0) ; // discard any previously received packets
  129.     Serial.println("Transmit NTP Request");
  130.     // get a random server from the pool
  131.     WiFi.hostByName(ntpServerName, ntpServerIP);
  132.     Serial.print(ntpServerName);
  133.     Serial.print(": ");
  134.     Serial.println(ntpServerIP);
  135.     sendNTPpacket(ntpServerIP);
  136.     uint32_t beginWait = millis();
  137.     while (millis() - beginWait < 1500) {
  138.       int size = Udp.parsePacket();
  139.       if (size >= NTP_PACKET_SIZE) {
  140.         Serial.println("Receive NTP Response");
  141.         Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
  142.         unsigned long secsSince1900;
  143.         // convert four bytes starting at location 40 to a long integer
  144.         secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
  145.         secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
  146.         secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
  147.         secsSince1900 |= (unsigned long)packetBuffer[43];
  148.         return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
  149.       }
  150.     }
  151.     Serial.println("No NTP Response :-(");
  152.     delay(500);
  153.   }
  154.   return 0; // return 0 if unable to get the time
  155. }

  156. // send an NTP request to the time server at the given address
  157. void sendNTPpacket(IPAddress &address)
  158. {
  159.   // set all bytes in the buffer to 0
  160.   memset(packetBuffer, 0, NTP_PACKET_SIZE);
  161.   // Initialize values needed to form NTP request
  162.   // (see URL above for details on the packets)
  163.   packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  164.   packetBuffer[1] = 0;     // Stratum, or type of clock
  165.   packetBuffer[2] = 6;     // Polling Interval
  166.   packetBuffer[3] = 0xEC;  // Peer Clock Precision
  167.   // 8 bytes of zero for Root Delay & Root Dispersion
  168.   packetBuffer[12] = 49;
  169.   packetBuffer[13] = 0x4E;
  170.   packetBuffer[14] = 49;
  171.   packetBuffer[15] = 52;
  172.   // all NTP fields have been given values, now
  173.   // you can send a packet requesting a timestamp:
  174.   Udp.beginPacket(address, 123); //NTP requests are to port 123
  175.   Udp.write(packetBuffer, NTP_PACKET_SIZE);
  176.   Udp.endPacket();
  177. }
複製代碼


摘路自 臉書粉絲團 Arduino Taipei
感謝 BEN CHENG 大提供的相關資料以及不斷的指導終於達到我的需求,當然也感謝許多同好的建議,甚至,遠在美國的丹丘大也提供寶貴意見... 首先我把問題的解決方法說明(因為我先前看過別人的資料但是不太懂),
1.先把 #include <DNSServer.h> //20190416
#include <ESP8266WebServer.h> //20190416
#include <WiFiManager.h>//20190416
WiFiManager wifiManager;//20190416 定義放到程式的頂端也就是 setup()之前.
2. 把 wifiManager.autoConnect("antlu"); 放在 setup()之內,這個作用就是 當你的作品移到"新的網路點"(先前的設定SSID PASSWORD 收不到的地方)上面時,你可以使用手機去搜尋到一個叫"antlu"的網路點.
3.把以前程式的指定 SSID 和 PASSWORD 去掉,沒有去掉會造成編譯錯誤喔!!
4.程式上傳完成 (可以比較我的先前程式差異.)
5.送電之後 時鐘不會顯示,原因是 程式要上網去抓取時間,但是WIFI 無法上網所以當然沒辦法走下去,其實這時候他正在等待設定...
6.使用 手機的找尋無線網路功能,找到 antlu 這個網點,經過一陣子之後就會顯示 照片1 的畫面.
CKA4.png

7. 選擇第一項 configure WiFi 進入照片2 的畫面.
CKA2.png

8. 在畫面上就有許多 網點可以讓你選擇,選取你可以有密碼的網點,然後輸入密碼,記得按下 SAVE !

9. 這時候會跳出 照片3 的畫面.
CKA3.png

10.沒多久之後 時鐘會顯示了 照片4 ,也就是成功了!!
CKA1.jpg

心得: WiFiManager 功能太強了! 因為習慣領域的關係,我一值把它想的非常複雜,其實,真的很簡單,這是我對網路 和 無線網路的知識極其缺乏的原因... 再次感謝 BEN CHENG 大,別忘了給我訊息 我要把 LGC12002 寄給你!!
新程式內容: https://www.pastiebin.com/5cb68def95419

線路圖:
網路時鐘100.jpg

成品:
CK1.jpg

CK2.jpg

過程:
CK3.jpg

CK4.jpg

CK5.jpg

CK6.jpg

CK7.jpg

盒子製作:
ckdraw.JPG

評分

22

查看全部評分

發表於 2019-4-20 19:08:09 | 顯示全部樓層
厲害的乾哥
雖然我一定不會懂
也是要頂一下
發表於 2019-4-20 19:19:08 | 顯示全部樓層
多謝分享。
 樓主| 發表於 2019-4-21 21:42:10 | 顯示全部樓層
tsai5371 發表於 2019-4-20 07:08 PM
厲害的乾哥
雖然我一定不會懂
也是要頂一下

蔡董:
   您不用懂,只要會用就可以!!
您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

關閉

站長小叮嚀上一條 /1 下一條

禁閉室|手機版|連繫我們|痞酷網電子技術論壇

GMT+8, 2024-4-27 07:36 AM , Processed in 0.064552 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.