Simple.ino 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Simple.ino, Example for the AutoConnect library.
  3. Copyright (c) 2018, Hieromon Ikasamo
  4. https://github.com/Hieromon/AutoConnect
  5. This software is released under the MIT License.
  6. https://opensource.org/licenses/MIT
  7. */
  8. #if defined(ARDUINO_ARCH_ESP8266)
  9. #include <ESP8266WiFi.h>
  10. #include <ESP8266WebServer.h>
  11. #elif defined(ARDUINO_ARCH_ESP32)
  12. #include <WiFi.h>
  13. #include <WebServer.h>
  14. #endif
  15. #include <time.h>
  16. #include <AutoConnect.h>
  17. static const char AUX_TIMEZONE[] PROGMEM = R"(
  18. {
  19. "title": "TimeZone",
  20. "uri": "/timezone",
  21. "menu": true,
  22. "element": [
  23. {
  24. "name": "caption",
  25. "type": "ACText",
  26. "value": "Sets the time zone to get the current local time.",
  27. "style": "font-family:Arial;font-weight:bold;text-align:center;margin-bottom:10px;color:DarkSlateBlue"
  28. },
  29. {
  30. "name": "timezone",
  31. "type": "ACSelect",
  32. "label": "Select TZ name",
  33. "option": [],
  34. "selected": 10
  35. },
  36. {
  37. "name": "newline",
  38. "type": "ACElement",
  39. "value": "<br>"
  40. },
  41. {
  42. "name": "start",
  43. "type": "ACSubmit",
  44. "value": "OK",
  45. "uri": "/start"
  46. }
  47. ]
  48. }
  49. )";
  50. typedef struct {
  51. const char* zone;
  52. const char* ntpServer;
  53. int8_t tzoff;
  54. } Timezone_t;
  55. static const Timezone_t TZ[] = {
  56. { "Europe/London", "europe.pool.ntp.org", 0 },
  57. { "Europe/Berlin", "europe.pool.ntp.org", 1 },
  58. { "Europe/Helsinki", "europe.pool.ntp.org", 2 },
  59. { "Europe/Moscow", "europe.pool.ntp.org", 3 },
  60. { "Asia/Dubai", "asia.pool.ntp.org", 4 },
  61. { "Asia/Karachi", "asia.pool.ntp.org", 5 },
  62. { "Asia/Dhaka", "asia.pool.ntp.org", 6 },
  63. { "Asia/Jakarta", "asia.pool.ntp.org", 7 },
  64. { "Asia/Manila", "asia.pool.ntp.org", 8 },
  65. { "Asia/Tokyo", "asia.pool.ntp.org", 9 },
  66. { "Australia/Brisbane", "oceania.pool.ntp.org", 10 },
  67. { "Pacific/Noumea", "oceania.pool.ntp.org", 11 },
  68. { "Pacific/Auckland", "oceania.pool.ntp.org", 12 },
  69. { "Atlantic/Azores", "europe.pool.ntp.org", -1 },
  70. { "America/Noronha", "south-america.pool.ntp.org", -2 },
  71. { "America/Araguaina", "south-america.pool.ntp.org", -3 },
  72. { "America/Blanc-Sablon", "north-america.pool.ntp.org", -4},
  73. { "America/New_York", "north-america.pool.ntp.org", -5 },
  74. { "America/Chicago", "north-america.pool.ntp.org", -6 },
  75. { "America/Denver", "north-america.pool.ntp.org", -7 },
  76. { "America/Los_Angeles", "north-america.pool.ntp.org", -8 },
  77. { "America/Anchorage", "north-america.pool.ntp.org", -9 },
  78. { "Pacific/Honolulu", "north-america.pool.ntp.org", -10 },
  79. { "Pacific/Samoa", "oceania.pool.ntp.org", -11 }
  80. };
  81. #if defined(ARDUINO_ARCH_ESP8266)
  82. ESP8266WebServer Server;
  83. #elif defined(ARDUINO_ARCH_ESP32)
  84. WebServer Server;
  85. #endif
  86. AutoConnect Portal(Server);
  87. AutoConnectConfig Config; // Enable autoReconnect supported on v0.9.4
  88. AutoConnectAux Timezone;
  89. void rootPage() {
  90. String content =
  91. "<html>"
  92. "<head>"
  93. "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
  94. "<script type=\"text/javascript\">"
  95. "setTimeout(\"location.reload()\", 1000);"
  96. "</script>"
  97. "</head>"
  98. "<body>"
  99. "<h2 align=\"center\" style=\"color:blue;margin:20px;\">Hello, world</h2>"
  100. "<h3 align=\"center\" style=\"color:gray;margin:10px;\">{{DateTime}}</h3>"
  101. "<p style=\"text-align:center;\">Reload the page to update the time.</p>"
  102. "<p></p><p style=\"padding-top:15px;text-align:center\">" AUTOCONNECT_LINK(COG_24) "</p>"
  103. "</body>"
  104. "</html>";
  105. static const char *wd[7] = { "Sun","Mon","Tue","Wed","Thr","Fri","Sat" };
  106. struct tm *tm;
  107. time_t t;
  108. char dateTime[26];
  109. t = time(NULL);
  110. tm = localtime(&t);
  111. sprintf(dateTime, "%04d/%02d/%02d(%s) %02d:%02d:%02d.",
  112. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  113. wd[tm->tm_wday],
  114. tm->tm_hour, tm->tm_min, tm->tm_sec);
  115. content.replace("{{DateTime}}", String(dateTime));
  116. Server.send(200, "text/html", content);
  117. }
  118. void startPage() {
  119. // Retrieve the value of AutoConnectElement with arg function of WebServer class.
  120. // Values are accessible with the element name.
  121. String tz = Server.arg("timezone");
  122. for (uint8_t n = 0; n < sizeof(TZ) / sizeof(Timezone_t); n++) {
  123. String tzName = String(TZ[n].zone);
  124. if (tz.equalsIgnoreCase(tzName)) {
  125. configTime(TZ[n].tzoff * 3600, 0, TZ[n].ntpServer);
  126. Serial.println("Time zone: " + tz);
  127. Serial.println("ntp server: " + String(TZ[n].ntpServer));
  128. break;
  129. }
  130. }
  131. // The /start page just constitutes timezone,
  132. // it redirects to the root page without the content response.
  133. Server.sendHeader("Location", String("http://") + Server.client().localIP().toString() + String("/"));
  134. Server.send(302, "text/plain", "");
  135. Server.client().flush();
  136. Server.client().stop();
  137. }
  138. void setup() {
  139. delay(1000);
  140. Serial.begin(115200);
  141. Serial.println();
  142. // Enable saved past credential by autoReconnect option,
  143. // even once it is disconnected.
  144. Config.autoReconnect = true;
  145. Portal.config(Config);
  146. // Load aux. page
  147. Timezone.load(AUX_TIMEZONE);
  148. // Retrieve the select element that holds the time zone code and
  149. // register the zone mnemonic in advance.
  150. AutoConnectSelect& tz = Timezone["timezone"].as<AutoConnectSelect>();
  151. for (uint8_t n = 0; n < sizeof(TZ) / sizeof(Timezone_t); n++) {
  152. tz.add(String(TZ[n].zone));
  153. }
  154. Portal.join({ Timezone }); // Register aux. page
  155. // Behavior a root path of ESP8266WebServer.
  156. Server.on("/", rootPage);
  157. Server.on("/start", startPage); // Set NTP server trigger handler
  158. // Establish a connection with an autoReconnect option.
  159. if (Portal.begin()) {
  160. Serial.println("WiFi connected: " + WiFi.localIP().toString());
  161. }
  162. }
  163. void loop() {
  164. Portal.handleClient();
  165. }