Hello!
i read a lot of closed and solved issues but i didn't understand well how to solve 2 main problems that i have with AutoConnect.
I state that i think this is a great library and permits to have an easier life using esp modules! But i still have some question for Hieromon and the comunity...
In my project (code below) i started from the Simple example from Hieromon with connection to NTP server and made some tests...
problem1: I need to save the timezone setting made on the timezone page to reload the same setting after shutting down my esp32 and repower it. Now i found that the timezone setting is lost every time the esp is shutted down. (the dates and time that i see in my main page of the browser shows a date and time around 1970 after every restart. I made some tests to create a txt file saved with spiffs with the timezone configuration and try to read it at every restart but with no success( see code below)
problem2: I have problems with the access point mode. When i first power on ESP32 after programming it, it's correctly configured as access point and with a mobile device a set the correct information (settings) of my wireless network. Then when the network is not available anymore i need to get the access point mode working again after a set timeout but i can't get this working. I tried also to change the autoReconnect option from true to false but didn't understood well how this function works.
I hope to solve this! Thank you in advance!
below the code!
void startPage() {
// Retrieve the value of AutoConnectElement with arg function of WebServer class.
// Values are accessible with the element name.
tz = Server.arg("timezone");
Serial.println("tz");
Serial.println(tz);
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;
}
Serial.println ("tzName");
Serial.println (tzName);
}
// 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() {
delay(1000);
Serial.begin(115200);
Serial.println();
delay (500);
File file33 = SPIFFS.open("/dir/tz.txt", "r");
while (file33.available()){
tz1 = file33.read();
}
Serial.println ("time zone read from flash");
Serial.println("valore letto"+ tz1);
tz=tz1;
file33.close();
// Enable saved past credential by autoReconnect option,
// even once it is disconnected.
Config.autoReconnect = false;
Config.hostName = "Portal";
// Set access point name
Config.apid = "Setup";
// Set menu title
Config.title = "Cunfiguration";
Portal.config(Config);
// Load aux. page
Timezone.load(AUX_TIMEZONE);
// Retrieve the select element that holds the time zone code and
// register the zone mnemonic in advance.
AutoConnectSelect& tz = Timezone["timezone"].as();
for (uint8_t n = 0; n < sizeof(TZ) / sizeof(Timezone_t); n++) {
tz.add(String(TZ[n].zone));
}
Portal.join({ Timezone }); // Register aux. page
// Behavior a root path of ESP8266WebServer.
Server.on("/", rootPage);
Server.on("/start", startPage); // Set NTP server trigger handler
Serial.println("Creating portal and trying to connect...");
// Establish a connection with an autoReconnect option.
if (Portal.begin()) {
Serial.println("WiFi connected: " + WiFi.localIP().toString());
Serial.println(WiFi.getHostname());
}
}
void loop() {
Portal.handleClient();
if (tz!=tz1){
// salva file con impostazioni timezone
File file22 = SPIFFS.open("/dir/tz.txt", "w");
if (!file22) Serial.println("file not found, creating new file");
while (file22.available()) file22.print(tz);
tz1=tz;
Serial.println ("file printed with tz" );
Serial.println (tz1 + tz);
file22.close();
Hello! i read a lot of closed and solved issues but i didn't understand well how to solve 2 main problems that i have with AutoConnect. I state that i think this is a great library and permits to have an easier life using esp modules! But i still have some question for Hieromon and the comunity... In my project (code below) i started from the Simple example from Hieromon with connection to NTP server and made some tests...
problem1: I need to save the timezone setting made on the timezone page to reload the same setting after shutting down my esp32 and repower it. Now i found that the timezone setting is lost every time the esp is shutted down. (the dates and time that i see in my main page of the browser shows a date and time around 1970 after every restart. I made some tests to create a txt file saved with spiffs with the timezone configuration and try to read it at every restart but with no success( see code below)
problem2: I have problems with the access point mode. When i first power on ESP32 after programming it, it's correctly configured as access point and with a mobile device a set the correct information (settings) of my wireless network. Then when the network is not available anymore i need to get the access point mode working again after a set timeout but i can't get this working. I tried also to change the autoReconnect option from true to false but didn't understood well how this function works.
I hope to solve this! Thank you in advance! below the code!
if defined(ARDUINO_ARCH_ESP8266)
include
include
elif defined(ARDUINO_ARCH_ESP32)
include
include
endif
include
include
include
String tz; String tz1;
static const char AUX_TIMEZONE[] PROGMEM = R"( { "title": "Data e Ora", "uri": "/timezone", "menu": true, "element": [ { "name": "caption", "type": "ACText", "value": "Impostazioni Data e Ora locali", "style": "font-family:Arial;font-weight:bold;text-align:center;margin-bottom:10px;color:DarkSlateBlue" }, { "name": "timezone", "type": "ACSelect", "label": "Selezionare Zona", "option": [], "selected": 10 }, { "name": "newline", "type": "ACElement", "value": "
" }, { "name": "start", "type": "ACSubmit", "value": "OK", "uri": "/start" } ] } )";
typedef struct { const char zone; const 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 } };
if defined(ARDUINO_ARCH_ESP8266)
ESP8266WebServer Server;
elif defined(ARDUINO_ARCH_ESP32)
WebServer Server;
endif
AutoConnect Portal(Server); AutoConnectConfig Config; // Enable autoReconnect supported on v0.9.4 AutoConnectAux Timezone; ////////////////////////////////////////////////////////////////////////////////////////////ROOT PAGE_NTP_VISUALIZZA_DATI_MACCHINA void rootPage() { String content = "" "
" "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" "<script type=\"text/javascript\">" "setTimeout(\"location.reload()\", 1000);" "" "" "" "<h2 align=\"center\" style=\"color:black;margin:20px;\">H228 Industry 4.0" "<h3 align=\"center\" style=\"color:gray;margin:10px;\">{{DateTime}}" "<p style=\"text-align:center;\">Reload the page to update the time." "<p style=\"padding-top:15px;text-align:center\">" AUTOCONNECT_LINK(COG_24) "" "" ""; static const char wd[7] = { "Dom","Lun","Mar","Mer","Gio","Ven","Sab" }; struct tm tm; time_t t; char dateTime[26];t = time(NULL); tm = localtime(&t); sprintf(dateTime, "%04d/%02d/%02d(%s) %02d:%02d:%02d.", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, wd[tm->tm_wday], tm->tm_hour, tm->tm_min, tm->tm_sec); content.replace("{{DateTime}}", String(dateTime)); Server.send(200, "text/html", content); Serial.println ("dateTime"); Serial.println (dateTime);
}
void startPage() { // Retrieve the value of AutoConnectElement with arg function of WebServer class. // Values are accessible with the element name. tz = Server.arg("timezone");
Serial.println("tz"); Serial.println(tz);
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;
} Serial.println ("tzName"); Serial.println (tzName);
}
// 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() { delay(1000); Serial.begin(115200); Serial.println(); delay (500); File file33 = SPIFFS.open("/dir/tz.txt", "r"); while (file33.available()){ tz1 = file33.read(); } Serial.println ("time zone read from flash"); Serial.println("valore letto"+ tz1); tz=tz1; file33.close();
// Enable saved past credential by autoReconnect option, // even once it is disconnected.
Config.autoReconnect = false; Config.hostName = "Portal"; // Set access point name Config.apid = "Setup"; // Set menu title Config.title = "Cunfiguration";
Portal.config(Config);
// Load aux. page Timezone.load(AUX_TIMEZONE); // Retrieve the select element that holds the time zone code and // register the zone mnemonic in advance. AutoConnectSelect& tz = Timezone["timezone"].as();
for (uint8_t n = 0; n < sizeof(TZ) / sizeof(Timezone_t); n++) {
tz.add(String(TZ[n].zone));
}
Portal.join({ Timezone }); // Register aux. page
// Behavior a root path of ESP8266WebServer. Server.on("/", rootPage); Server.on("/start", startPage); // Set NTP server trigger handler
Serial.println("Creating portal and trying to connect..."); // Establish a connection with an autoReconnect option. if (Portal.begin()) { Serial.println("WiFi connected: " + WiFi.localIP().toString()); Serial.println(WiFi.getHostname()); }
}
void loop() { Portal.handleClient();
if (tz!=tz1){ // salva file con impostazioni timezone File file22 = SPIFFS.open("/dir/tz.txt", "w"); if (!file22) Serial.println("file not found, creating new file");
while (file22.available()) file22.print(tz); tz1=tz; Serial.println ("file printed with tz" ); Serial.println (tz1 + tz); file22.close();
}
}