NTP clock made with ESP-12 (ESP8266) and 16-char TM1640 (JY-LM1640)
mcu
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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. HTTPClient http;
  19. boolean syncEventTriggered = false;
  20. NTPSyncEvent_t ntpEvent;
  21. //set timezone
  22. static const int8_t timeZone = 7;
  23. bool wifiFirstConnected = false;
  24. bool ntp_online = false;
  25. int outside_termo = 0;
  26. void onSTAConnected (WiFiEventStationModeConnected ipInfo) {
  27. Serial.printf ("Connected to %s\r\n", ipInfo.ssid.c_str ());
  28. }
  29. // Start NTP only after IP network is connected
  30. void onSTAGotIP(WiFiEventStationModeGotIP ipInfo) {
  31. Serial.printf("Got IP: %s\r\n", ipInfo.ip.toString().c_str());
  32. wifiFirstConnected = true;
  33. //digitalWrite(ONBOARDLED, LOW); // Turn on LED
  34. }
  35. // Manage network disconnection
  36. void onSTADisconnected(WiFiEventStationModeDisconnected event_info) {
  37. Serial.printf("Disconnected from SSID: %s\n", event_info.ssid.c_str());
  38. Serial.printf("Reason: %d\n", event_info.reason);
  39. digitalWrite(ONBOARDLED, HIGH); // Turn off LED
  40. //NTP.stop(); // NTP sync can be disabled to avoid sync errors
  41. ntp_online = false;
  42. }
  43. void processSyncEvent(NTPSyncEvent_t ntpEvent) {
  44. if (ntpEvent) {
  45. Serial.print("Time Sync error: ");
  46. if (ntpEvent == noResponse) {
  47. Serial.println("NTP server not reachable");
  48. ntp_online = false;
  49. }
  50. else if (ntpEvent == invalidAddress) {
  51. Serial.println("Invalid NTP server address");
  52. ntp_online = false;
  53. }
  54. }
  55. else {
  56. Serial.print("Got NTP time: ");
  57. ntp_online = true;
  58. get_outside_termo();
  59. Serial.println(NTP.getTimeDateString(NTP.getLastNTPSync()));
  60. }
  61. }
  62. void setup() {
  63. static WiFiEventHandler e1, e2, e3;
  64. Wire.begin();
  65. pinMode(ONBOARDLED, OUTPUT); // Onboard LED
  66. digitalWrite(ONBOARDLED, LOW);
  67. delay(1000);
  68. digitalWrite(ONBOARDLED, HIGH);
  69. //Serial.begin(115200);
  70. //WiFi.mode(WIFI_STA);
  71. WiFi.hostname("clock-x16-01");
  72. //WiFi.begin(YOUR_WIFI_SSID, YOUR_WIFI_PASSWD);
  73. NTP.onNTPSyncEvent([](NTPSyncEvent_t event) {
  74. ntpEvent = event;
  75. syncEventTriggered = true;
  76. });
  77. e1 = WiFi.onStationModeGotIP(onSTAGotIP);// As soon WiFi is connected, start NTP Client
  78. e2 = WiFi.onStationModeDisconnected(onSTADisconnected);
  79. e3 = WiFi.onStationModeConnected (onSTAConnected);
  80. digitalWrite(ONBOARDLED, HIGH); // Switch off LED
  81. WiFiManager wifiManager;
  82. wifiManager.autoConnect("clock-x16-01");
  83. }
  84. void get_outside_termo() {
  85. http.begin("http://termo.tomsk.ru/data.txt");
  86. int httpCode = http.GET();
  87. if (httpCode > 0) {
  88. if (httpCode == HTTP_CODE_OK) {
  89. String result = http.getString();
  90. outside_termo = round(result.toInt());
  91. }
  92. }
  93. }
  94. void starting() {
  95. NTP.begin("ntp.tomtel.ru", timeZone, true);
  96. NTP.setInterval(10, 600);
  97. display.clear();
  98. //Serial.println("Starting NTP client");
  99. while (ntp_online == false) {
  100. if (syncEventTriggered) {
  101. processSyncEvent (ntpEvent);
  102. syncEventTriggered = true;
  103. }
  104. display.setBrightness(7);
  105. display.showString("connecting...");
  106. //Serial.println("Connecting");
  107. delay(1000);
  108. }
  109. get_outside_termo();
  110. display.clear();
  111. for (uint8_t l = 0; l <= 7; l++) {
  112. display.setBrightness(l); //set the diplay to maximum brightness
  113. display.showString("hello int 21h");
  114. delay(200);
  115. }
  116. display.clear();
  117. }
  118. char timebuff[17];
  119. void loop() {
  120. if (ntp_online == true) {
  121. // sync time
  122. if (syncEventTriggered) {
  123. processSyncEvent (ntpEvent);
  124. syncEventTriggered = false;
  125. }
  126. String timestamp = NTP.getTimeDateString();
  127. // draw temperature
  128. String nozero = "";
  129. String sign;
  130. if (outside_termo < 0) {
  131. sign = "";
  132. } else
  133. {
  134. sign = " ";
  135. }
  136. if (outside_termo > -10 && outside_termo < 10) {
  137. nozero = " ";
  138. }
  139. String result = timestamp + " " + nozero + sign + outside_termo;
  140. //Serial.print(timestamp + "\n");
  141. result.toCharArray(timebuff, 17);
  142. display.showString( timebuff );
  143. delay(1000);
  144. } else {
  145. starting();
  146. }
  147. }