Supported AutoConnectAux

pull/41/head
Hieromon Ikasamo 6 years ago
parent d5ca170a86
commit 657c6976b3
  1. 134
      examples/Simple/Simple.ino

@ -22,12 +22,101 @@ ESP8266WebServer Server;
#elif defined(ARDUINO_ARCH_ESP32) #elif defined(ARDUINO_ARCH_ESP32)
WebServer Server; WebServer Server;
#endif #endif
static const char AUX_TIMEZONE[] PROGMEM = R"(
{
"title": "TimeZone",
"uri": "/timezone",
"menu": true,
"element": [
{
"name": "caption",
"type": "ACText",
"value": "Sets the time zone to get the current local time.",
"style": "font-family:Arial;font-weight:bold;text-align:center;margin-bottom:10px;color:DarkSlateBlue"
},
{
"name": "timezone",
"type": "ACSelect",
"label": "Select TZ name",
"option": [
"Europe/London",
"Europe/Berlin",
"Europe/Helsinki",
"Europe/Moscow",
"Asia/Dubai",
"Asia/Karachi",
"Asia/Dhaka",
"Asia/Jakarta",
"Asia/Manila",
"Asia/Tokyo",
"Australia/Brisbane",
"Pacific/Noumea",
"Pacific/Auckland",
"Pacific/Tongatapu",
"Pacific/Kiritimati",
"America/Sao_Paulo",
"America/Santiago",
"America/Detroit",
"America/Chicago",
"America/Denver",
"America/LosAngeles",
"America/Anchorage",
"Pacific/Honolulu",
"Pacific/Samoa"
]
},
{
"name": "newline",
"type": "ACElement",
"value": "<br>"
},
{
"name": "start",
"type": "ACSubmit",
"value": "OK",
"uri": "/start"
}
]
}
)";
typedef struct {
char* zone;
char* ntpServer;
int8_t tzoff;
} Timezone_t;
static const Timezone_t TZ[] = {
{ "Europe/London", "europe.pool.ntp.org", 0 },
{ "Europe/Berlin", "europe.pool.ntp.org", 1 },
{ "Europe/Helsinki", "europe.pool.ntp.org", 2 },
{ "Europe/Moscow", "europe.pool.ntp.org", 3 },
{ "Asia/Dubai", "asia.pool.ntp.org", 4 },
{ "Asia/Karachi", "asia.pool.ntp.org", 5 },
{ "Asia/Dhaka", "asia.pool.ntp.org", 6 },
{ "Asia/Jakarta", "asia.pool.ntp.org", 7 },
{ "Asia/Manila", "asia.pool.ntp.org", 8 },
{ "Asia/Tokyo", "asia.pool.ntp.org", 9 },
{ "Australia/Brisbane", "oceania.pool.ntp.org", 10 },
{ "Pacific/Noumea", "oceania.pool.ntp.org", 11 },
{ "Pacific/Auckland", "oceania.pool.ntp.org", 12 },
{ "Atlantic/Azores", "europe.pool.ntp.org", -1 },
{ "America/Noronha", "south-america.pool.ntp.org", -2 },
{ "America/Araguaina", "south-america.pool.ntp.org", -3 },
{ "America/Blanc-Sablon", "north-america.pool.ntp.org", -4},
{ "America/New_York", "north-america.pool.ntp.org", -5 },
{ "America/Chicago", "north-america.pool.ntp.org", -6 },
{ "America/Denver", "north-america.pool.ntp.org", -7 },
{ "America/Los_Angeles", "north-america.pool.ntp.org", -8 },
{ "America/Anchorage", "north-america.pool.ntp.org", -9 },
{ "Pacific/Honolulu", "north - america.pool.ntp.org", -10 },
{ "Pacific/Samoa", "oceania.pool.ntp.org", -11 }
};
AutoConnect Portal(Server); AutoConnect Portal(Server);
AutoConnectConfig Config; // Enable autoReconnect supported on v0.9.4 AutoConnectConfig Config; // Enable autoReconnect supported on v0.9.4
AutoConnectAux Timezone;
#define TIMEZONE (3600 * 9) // Tokyo
#define NTPServer1 "ntp.nict.jp" // NICT japan.
#define NTPServer2 "time1.google.com"
void rootPage() { void rootPage() {
String content = String content =
@ -38,7 +127,8 @@ void rootPage() {
"<body>" "<body>"
"<h2 align=\"center\" style=\"color:blue;margin:20px;\">Hello, world</h2>" "<h2 align=\"center\" style=\"color:blue;margin:20px;\">Hello, world</h2>"
"<h3 align=\"center\" style=\"color:gray;margin:10px;\">{{DateTime}}</h3>" "<h3 align=\"center\" style=\"color:gray;margin:10px;\">{{DateTime}}</h3>"
"<p style=\"padding-top:10px;text-align:center\">" AUTOCONNECT_LINK(COG_32) "</p>" "<p style=\"text-align:center;\">Reload the page to update the time.</p>"
"<p></p><p style=\"padding-top:15px;text-align:center\">" AUTOCONNECT_LINK(COG_24) "</p>"
"</body>" "</body>"
"</html>"; "</html>";
static const char *wd[7] = { "Sun","Mon","Tue","Wed","Thr","Fri","Sat" }; static const char *wd[7] = { "Sun","Mon","Tue","Wed","Thr","Fri","Sat" };
@ -56,23 +146,49 @@ void rootPage() {
Server.send(200, "text/html", content); Server.send(200, "text/html", content);
} }
void startPage() {
// Retrieve the value of AutoConnectElement with arg function of WebServer class.
// Values are accessible with the element name.
String tz = Server.arg("timezone");
for (uint8_t n = 0; n < sizeof(TZ) / sizeof(Timezone_t); n++) {
String tzName = String(TZ[n].zone);
if (tz.equalsIgnoreCase(tzName)) {
configTime(TZ[n].tzoff * 3600, 0, TZ[n].ntpServer);
Serial.println("Time zone: " + tz);
Serial.println("ntp server: " + String(TZ[n].ntpServer));
break;
}
}
// The /start page just constitutes timezone,
// it redirects to the root page without the content response.
Server.sendHeader("Location", String("http://") + Server.client().localIP().toString() + String("/"));
Server.send(302, "text/plain", "");
Server.client().flush();
Server.client().stop();
}
void setup() { void setup() {
delay(1000); delay(1000);
Serial.begin(115200); Serial.begin(115200);
Serial.println(); Serial.println();
// Behavior a root path of ESP8266WebServer.
Server.on("/", rootPage);
// Enable saved past credential by autoReconnect option, // Enable saved past credential by autoReconnect option,
// even once it is disconnected. // even once it is disconnected.
Config.autoReconnect = true; Config.autoReconnect = true;
Portal.config(Config); Portal.config(Config);
Timezone.load(AUX_TIMEZONE); // Load aux. page
Portal.join({ Timezone }); // Register aux. page
// Behavior a root path of ESP8266WebServer.
Server.on("/", rootPage);
Server.on("/start", startPage); // Set NTP server trigger handler
// Establish a connection with an autoReconnect option. // Establish a connection with an autoReconnect option.
if (Portal.begin()) { if (Portal.begin()) {
Serial.println("WiFi connected: " + WiFi.localIP().toString()); Serial.println("WiFi connected: " + WiFi.localIP().toString());
configTime(TIMEZONE, 0, NTPServer1, NTPServer2);
} }
} }

Loading…
Cancel
Save