NTP clock made with ESP-12 (ESP8266) and 16-char TM1640 (JY-LM1640)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tm1640-ntp.ino 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <Wire.h>
  2. #include "LM1640Display.h"
  3. #include <TimeLib.h>
  4. #include "NtpClientLib.h"
  5. #include <ESP8266WiFi.h>
  6. #include <ESP8266HTTPClient.h>
  7. #include <DNSServer.h>
  8. #include <ESP8266WebServer.h>
  9. #include <WiFiManager.h>
  10. #define ONBOARDLED 2 // Built in LED on ESP-12/ESP-07
  11. // ESP-12
  12. //#define CLK D2
  13. //#define DIO D3
  14. // ESP-01
  15. #define CLK D3
  16. #define DIO D9
  17. LM1640Display display(CLK, DIO);
  18. //set timezone
  19. static const int8_t timeZone = 7;
  20. bool wifiFirstConnected = false;
  21. bool ntp_online = false;
  22. void onSTAConnected (WiFiEventStationModeConnected ipInfo) {
  23. Serial.printf ("Connected to %s\r\n", ipInfo.ssid.c_str ());
  24. }
  25. // Start NTP only after IP network is connected
  26. void onSTAGotIP(WiFiEventStationModeGotIP ipInfo) {
  27. Serial.printf("Got IP: %s\r\n", ipInfo.ip.toString().c_str());
  28. wifiFirstConnected = true;
  29. //digitalWrite(ONBOARDLED, LOW); // Turn on LED
  30. }
  31. // Manage network disconnection
  32. void onSTADisconnected(WiFiEventStationModeDisconnected event_info) {
  33. Serial.printf("Disconnected from SSID: %s\n", event_info.ssid.c_str());
  34. Serial.printf("Reason: %d\n", event_info.reason);
  35. digitalWrite(ONBOARDLED, HIGH); // Turn off LED
  36. //NTP.stop(); // NTP sync can be disabled to avoid sync errors
  37. ntp_online = false;
  38. }
  39. void processSyncEvent(NTPSyncEvent_t ntpEvent) {
  40. if (ntpEvent) {
  41. Serial.print("Time Sync error: ");
  42. if (ntpEvent == noResponse) {
  43. Serial.println("NTP server not reachable");
  44. ntp_online = false;
  45. }
  46. else if (ntpEvent == invalidAddress) {
  47. Serial.println("Invalid NTP server address");
  48. ntp_online = false;
  49. }
  50. }
  51. else {
  52. Serial.print("Got NTP time: ");
  53. ntp_online = true;
  54. Serial.println(NTP.getTimeDateString(NTP.getLastNTPSync()));
  55. }
  56. }
  57. boolean syncEventTriggered = false;
  58. NTPSyncEvent_t ntpEvent;
  59. void setup() {
  60. static WiFiEventHandler e1, e2, e3;
  61. Wire.begin();
  62. pinMode(ONBOARDLED, OUTPUT); // Onboard LED
  63. digitalWrite(ONBOARDLED, LOW);
  64. delay(1000);
  65. digitalWrite(ONBOARDLED, HIGH);
  66. //Serial.begin(115200);
  67. //WiFi.mode(WIFI_STA);
  68. WiFi.hostname("clock-x16-01");
  69. //WiFi.begin(YOUR_WIFI_SSID, YOUR_WIFI_PASSWD);
  70. pinMode(ONBOARDLED, OUTPUT); // Onboard LED
  71. digitalWrite(ONBOARDLED, HIGH); // Switch off LED
  72. NTP.onNTPSyncEvent([](NTPSyncEvent_t event) {
  73. ntpEvent = event;
  74. syncEventTriggered = true;
  75. });
  76. e1 = WiFi.onStationModeGotIP(onSTAGotIP);// As soon WiFi is connected, start NTP Client
  77. e2 = WiFi.onStationModeDisconnected(onSTADisconnected);
  78. e3 = WiFi.onStationModeConnected (onSTAConnected);
  79. digitalWrite(ONBOARDLED, HIGH); // Switch off LED
  80. WiFiManager wifiManager;
  81. wifiManager.autoConnect("clock-x16-01");
  82. }
  83. void starting() {
  84. NTP.begin("ntp.tomtel.ru", timeZone, true);
  85. NTP.setInterval(10, 600);
  86. display.clear();
  87. //Serial.println("Starting NTP client");
  88. while (ntp_online == false) {
  89. if (syncEventTriggered) {
  90. processSyncEvent (ntpEvent);
  91. syncEventTriggered = true;
  92. }
  93. display.setBrightness(7);
  94. display.showString("connecting...");
  95. //Serial.println("Connecting");
  96. delay(1000);
  97. }
  98. display.clear();
  99. for (uint8_t l = 0; l <= 7; l++) {
  100. display.setBrightness(l); //set the diplay to maximum brightness
  101. display.showString("hello int 21h");
  102. delay(200);
  103. }
  104. display.clear();
  105. }
  106. char timebuff[17];
  107. void loop() {
  108. if (ntp_online == true) {
  109. if (syncEventTriggered) {
  110. processSyncEvent (ntpEvent);
  111. syncEventTriggered = false;
  112. }
  113. String timestamp = NTP.getTimeDateString();
  114. //Serial.print(timestamp + "\n");
  115. timestamp.toCharArray(timebuff, 17);
  116. display.showString( timebuff );
  117. delay(1000);
  118. } else {
  119. starting();
  120. }
  121. }