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 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #define PUYA_SUPPORT 1
  2. #define SPIFFS_NO_BLIND_WRITES
  3. #include <FS.h>
  4. #include <Wire.h>
  5. #include "LM1640Display.h"
  6. #include <TimeLib.h>
  7. #include "NtpClientLib.h"
  8. #include <ESP8266WiFi.h>
  9. #include <ESP8266HTTPClient.h>
  10. #include <ArduinoJson.h>
  11. #include <DNSServer.h>
  12. #include <ESP8266WebServer.h>
  13. #include <WiFiManager.h>
  14. #define ONBOARDLED 2 // Built in LED on ESP-12/ESP-07
  15. //////// VARIABLES ////////
  16. // ESP-12
  17. //#define CLK D2
  18. //#define DIO D3
  19. // ESP-01
  20. #define CLK D3
  21. #define DIO D9
  22. // wifi manager stuff
  23. bool wifi_connected = false;
  24. bool ntp_online = false;
  25. char ntp_server[40] = "ru.pool.ntp.org";
  26. char ntp_timezone[3] = "7";
  27. char wifi_hostname[40] = "clock-x16-1";
  28. bool shouldSaveConfig = false;
  29. WiFiManagerParameter custom_ntp_server("server", "NTP server", ntp_server, 40);
  30. WiFiManagerParameter custom_timezone("timezone", "Timezone (default 7)", ntp_timezone, 3);
  31. WiFiManagerParameter custom_hostname("hostname", "Hostname (default clock-vfd)", wifi_hostname, 40);
  32. WiFiManager wifiManager;
  33. // init display
  34. LM1640Display display(CLK, DIO);
  35. boolean syncEventTriggered = false;
  36. NTPSyncEvent_t ntpEvent;
  37. // get temperature from internet
  38. HTTPClient http;
  39. int outside_termo = 0;
  40. //////// CALLBACK FUNCTIONS ////////
  41. //callback config mode
  42. void configModeCallback(WiFiManager *myWiFiManager)
  43. {
  44. Serial.println("Entered config mode");
  45. Serial.println(WiFi.softAPIP());
  46. //if you used auto generated SSID, print it
  47. Serial.println(myWiFiManager->getConfigPortalSSID());
  48. }
  49. //callback notifying us of the need to save config
  50. void saveConfigCallback()
  51. {
  52. Serial.println("Should save config");
  53. shouldSaveConfig = true;
  54. }
  55. void processSyncEvent(NTPSyncEvent_t ntpEvent)
  56. {
  57. if (ntpEvent)
  58. {
  59. ntp_online = false;
  60. Serial.print("Time Sync error: ");
  61. if (ntpEvent == noResponse)
  62. {
  63. Serial.println("NTP server not reachable");
  64. }
  65. else if (ntpEvent == invalidAddress)
  66. {
  67. Serial.println("Invalid NTP server address");
  68. }
  69. }
  70. else
  71. {
  72. ntp_online = true;
  73. Serial.print("Got NTP time: ");
  74. get_outside_termo();
  75. Serial.println(NTP.getTimeDateString(NTP.getLastNTPSync()));
  76. }
  77. }
  78. //////// PRIMITIVES /////////
  79. void reset_device()
  80. {
  81. // connection failed, reset
  82. Serial.println("Fatal error! Resetting device");
  83. display.clear();
  84. display.showString("resetting...");
  85. delay(3000);
  86. ESP.reset();
  87. delay(5000);
  88. }
  89. void hello()
  90. {
  91. display.clear();
  92. for (uint8_t l = 0; l <= 7; l++)
  93. {
  94. display.setBrightness(l); //set the diplay to maximum brightness
  95. display.showString("hello int 21h");
  96. delay(200);
  97. }
  98. }
  99. void config_load() {
  100. if (SPIFFS.begin())
  101. {
  102. Serial.println("mounted file system");
  103. if (SPIFFS.exists("/config.json"))
  104. {
  105. //file exists, reading and loading
  106. Serial.println("reading config file");
  107. File configFile = SPIFFS.open("/config.json", "r");
  108. if (configFile)
  109. {
  110. Serial.println("opened config file");
  111. size_t size = configFile.size();
  112. // Allocate a buffer to store contents of the file.
  113. std::unique_ptr<char[]> buf(new char[size]);
  114. configFile.readBytes(buf.get(), size);
  115. DynamicJsonBuffer jsonBuffer;
  116. JsonObject &json = jsonBuffer.parseObject(buf.get());
  117. json.printTo(Serial);
  118. if (json.success())
  119. {
  120. Serial.println("\nparsed json");
  121. strcpy(ntp_server, json["ntp_server"]);
  122. strcpy(ntp_timezone, json["ntp_timezone"]);
  123. strcpy(wifi_hostname, json["wifi_hostname"]);
  124. }
  125. else
  126. {
  127. Serial.println("failed to load json config");
  128. }
  129. configFile.close();
  130. }
  131. }
  132. }
  133. else
  134. {
  135. Serial.println("failed to mount FS");
  136. }
  137. }
  138. void config_save() {
  139. Serial.println("saving config");
  140. DynamicJsonBuffer jsonBuffer;
  141. JsonObject &json = jsonBuffer.createObject();
  142. json["ntp_server"] = ntp_server;
  143. json["ntp_timezone"] = ntp_timezone;
  144. json["wifi_hostname"] = wifi_hostname;
  145. File configFile = SPIFFS.open("/config.json", "w");
  146. if (!configFile)
  147. {
  148. Serial.println("failed to open config file for writing");
  149. }
  150. json.printTo(Serial);
  151. json.printTo(configFile);
  152. configFile.close();
  153. }
  154. //////// MAIN BODY ////////
  155. void setup()
  156. {
  157. Wire.begin();
  158. pinMode(ONBOARDLED, OUTPUT); // Onboard LED
  159. digitalWrite(ONBOARDLED, HIGH); // Switch off LED
  160. hello();
  161. Serial.println("mounting FS...");
  162. config_load();
  163. Serial.println("Wifi manager configuring...");
  164. // custom wifi parameters
  165. wifiManager.setSaveConfigCallback(saveConfigCallback); //set config save notify callback
  166. wifiManager.setAPCallback(configModeCallback);
  167. wifiManager.addParameter(&custom_ntp_server);
  168. wifiManager.addParameter(&custom_timezone);
  169. wifiManager.addParameter(&custom_hostname);
  170. Serial.println("Wifi connecting...");
  171. if (!wifiManager.autoConnect("clock-x16-1-config"))
  172. {
  173. reset_device();
  174. }
  175. //if you get here you have connected to the WiFi
  176. Serial.println("Wifi connected");
  177. wifi_connected = true;
  178. //read updated parameters
  179. strcpy(ntp_server, custom_ntp_server.getValue());
  180. strcpy(ntp_timezone, custom_timezone.getValue());
  181. strcpy(wifi_hostname, custom_hostname.getValue());
  182. // check hostname, set if needed
  183. if (wifi_hostname != "clock-x16-1")
  184. WiFi.hostname("clock-x16-1");
  185. //save the custom parameters to FS
  186. if (shouldSaveConfig)
  187. {
  188. config_save();
  189. }
  190. Serial.print("local ip ");
  191. Serial.println(WiFi.localIP());
  192. NTP.onNTPSyncEvent([](NTPSyncEvent_t event) {
  193. ntpEvent = event;
  194. syncEventTriggered = true;
  195. });
  196. }
  197. void get_outside_termo()
  198. {
  199. http.begin("http://termo.tomsk.ru/data.txt");
  200. int httpCode = http.GET();
  201. if (httpCode > 0)
  202. {
  203. if (httpCode == HTTP_CODE_OK)
  204. {
  205. String result = http.getString();
  206. outside_termo = round(result.toInt());
  207. }
  208. }
  209. }
  210. void starting()
  211. {
  212. Serial.print("TZ: ");
  213. Serial.println(atoi(ntp_timezone));
  214. display.clear();
  215. //Serial.println("Starting NTP client");
  216. while (ntp_online == false)
  217. {
  218. display.setBrightness(1);
  219. display.showString("connecting...");
  220. NTP.stop();
  221. NTP.begin(ntp_server, atoi(ntp_timezone), true);
  222. NTP.setInterval(3600);
  223. processSyncEvent(ntpEvent);
  224. syncEventTriggered = true;
  225. delay(30000);
  226. }
  227. get_outside_termo();
  228. display.setBrightness(7);
  229. display.clear();
  230. }
  231. char timebuff[17];
  232. void loop()
  233. {
  234. if (WiFi.status() != WL_CONNECTED)
  235. {
  236. wifi_connected = false;
  237. }
  238. // if wifi suddenly disconnected wait, if failed run configurator
  239. if (wifi_connected == true)
  240. {
  241. // if ntp server is down try to reconnect
  242. if (ntp_online == false)
  243. {
  244. starting();
  245. }
  246. else
  247. {
  248. // all fine, working here
  249. if (syncEventTriggered)
  250. {
  251. processSyncEvent(ntpEvent);
  252. syncEventTriggered = false;
  253. }
  254. String timestamp = NTP.getTimeDateString();
  255. // draw temperature
  256. String nozero = "";
  257. String sign;
  258. if (outside_termo < 0)
  259. {
  260. sign = "";
  261. }
  262. else
  263. {
  264. sign = " ";
  265. }
  266. if (outside_termo > -10 && outside_termo < 10)
  267. {
  268. nozero = " ";
  269. }
  270. String result = timestamp + " " + nozero + sign + outside_termo;
  271. result.toCharArray(timebuff, 17);
  272. display.showString(timebuff);
  273. delay(1000);
  274. }
  275. }
  276. else
  277. {
  278. delay(10000);
  279. Serial.println("Wifi connecting...");
  280. if (wifiManager.autoConnect("clock-x16-1-config"))
  281. {
  282. wifi_connected = true;
  283. starting();
  284. };
  285. }
  286. }