Closed
Description
Faced a strange behavior of WiFi.hostname()
yesterday. Seems that my hostname only get's set after a hard reset.
I did setup a WiFo connection with:
WiFi.hostname(wiFiHostname);
WiFi.begin(ssid, passwd);
After that I try to connect to the network credentials read from EEPROM. If the connection fails I switch the mode to AP with WiFi.mode(WIFI_AP);
and starting a captive portal to get new credentials from the user. My ESP then resets and connects to the new network. Unfortunately even though the ESP connects now in STA mode, it won't apply the hostname.
Only when I hard reset the device and the ESP connects again to the network the hostname is used.
Any ideas, what have gone wrong?
Full code:
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
/* RELAIS SETTINGS */
#define relaisPin D1
/* DEVICE SETTINGS */
const char* cnfSSID = "ESPDevice Config";
const char* wiFiHostname = "espdevice";
/* PROCESSING VARIABLES */
char ssid[30];
char passwd[30];
bool wifiFailure = false;
bool relaisState = LOW;
/* CONFIG WEBPAGE */
String cnfSuccessHtml = "<div class=\"alert\"> <span class=\"closebtn\" onclick=\"this.parentElement.style.display='none';\">×</span> Received network credentials - Restarting now. </div>";
String cnfhtml1 ="<!DOCTYPE html> <html lang=\"en\" dir=\"ltr\"> <head> <meta charset=\"utf-8\"> <title>ESP8266 Configurator</title> <style>html, body{padding: 0; margin: 0; font-family: Helvetica, sans-serif; background-color: #1a2431; color: white;}.wrappper{margin-top: 40px;}main{width: 90%; max-width: 500px; margin: 30px auto;}.alert{padding: 20px; background-color: #28a745; color: white; margin-bottom: 15px; border-radius: .25rem;}.closebtn{margin-left: 15px; color: white; font-weight: bold; float: right; font-size: 22px; line-height: 20px; cursor: pointer; transition: 0.3s;}.closebtn:hover{opacity: .6;}input{width: 100%; margin-bottom: 1.4em; padding: .375rem .75rem; font-size: 1rem; line-height: 1.5; color: #495057; background-color: #fff; background-clip: padding-box; border: 1px solid #ced4da; border-radius: .25rem; box-sizing: border-box;}label{display: inline-block; margin-bottom: .5rem;}.form-actions{display: flex; justify-content: space-between;}button{display: inline-block; font-weight: 400; width: 48%; text-align: center; white-space: nowrap; vertical-align: middle; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; border: 1px solid transparent; padding: .375rem .75rem; font-size: 1rem; line-height: 1.5; border-radius: .25rem; transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;}button[type=\"submit\"]{color: #fff; background-color: #28a745; border-color: #28a745;}button[type=\"submit\"]:hover{color: #fff; background-color: #218838; border-color: #1e7e34;}button[type=\"reset\"]{color: #fff; background-color: #f44336; border-color: #f44336;}button[type=\"reset\"]:hover{color: #fff; background-color: #C83B31; border-color: #C83B31;}footer{position: fixed; bottom: 0; padding: 30px 0; width: 100%; text-align: center; font-size: 0.8em;}a{color: #ff6c40; text-decoration: none;}.colored{color: #ff6c40;}</style> </head> <body> <div class=\"wrappper\"> <main> <h1><span class=\"colored\">ESP8266</span>WiFi Connector</h1>";
String cnfhtml2 = "<form action=\"/\" method=\"post\"> <p> Please enter you WiFi credentials down below. Your WiFi credentials won't be shared with anyone else. </p><label for=\"ssidInput\">Network Name:</label> <input type=\"text\" id=\"ssidInput\" name=\"ssid\" value=\"\" placeholder=\"SSID\"> <label for=\"ssidInput\">Network Password:</label> <input type=\"password\" id=\"passwordInput\" name=\"passwd\" value=\"\" placeholder=\"Password\"> <div class=\"form-actions\"> <button type=\"submit\">Submit</button> <button type=\"reset\">Reset</button> </div> </form> </main> <footer>Copyright by <a href=\"https://github.com/felixgeissler\" target=\"_blank\">Felix Geißler</a>, 2018</footer> </div> </body> </html>";
/* MAIN WEBPAGE */
String html ="<!DOCTYPE html> <html lang=\"en\" dir=\"ltr\"> <head> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> <link rel=\"icon\" href=\"data:,\"> <meta charset=\"utf-8\"> <title>Wohnzimmerlampe Controler</title> <style media=\"screen\"> body{ text-align: center; font-family: \"Trebuchet MS\", Arial; margin-left:auto; margin-right:auto; background-color: #1a2431; color: white; padding: 2em; } button{ font-size: 1.4em; width: 100%; max-width: 400px; height: 3em; border-radius: 2em; margin-top: 3em; color: white; transition: all 800ms; opacity: 1; outline: none; } button:hover{ width: 95%; opacity: .9; } .colored{ color: #ff6c40; } .btn-on{ background-color: transparent; border: 1px solid white; } .btn-off{ border: 1px solid #ff6c40; background-color: #ff6c40; } .btn-err{ border: 1px solid red; background-color: red; } </style> <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script> </head> <body> <h1><span class=\"colored\">Wohnzimmerlampe</span> Controller</h1> <button id=\"toggleButton\" class=\"btn-on\" onchange=\"toggleRelais()\"></button> <script type=\"text/javascript\"> var button = document.getElementById(\"toggleButton\"); $.ajaxSetup({ timeout: 1000 }); window.onload = function(){ $.get(\"/status\", function(data, status){ handleButton(data); }); }; button.addEventListener(\"click\", function(){ $.get(\"/toggle\", function(data, status){ handleButton(data); }); }); function handleButton(data){ if(data == 'on'){ button.setAttribute('class', 'btn-off'); button.innerHTML = 'Off'; }else if(data == 'off'){ button.setAttribute('class', 'btn-on'); button.innerHTML = 'On'; }else{ button.setAttribute('class', 'btn-err'); button.innerHTML = 'ERROR'; } } </script> </body> </html>";
/* NETWORK */
const byte dnsPort = 53;
IPAddress apIP(192, 168, 1, 1);
DNSServer dnsServer;
ESP8266WebServer server(80);
void(* resetFunc) (void) = 0; //declare reset function @ address 0
void setup() {
pinMode(relaisPin, OUTPUT);
Serial.begin(115200);
EEPROM.begin(80);
int EEadress = 0;
EEPROM.get(EEadress, ssid);
EEadress += sizeof(ssid);
EEPROM.get(EEadress, passwd);
Serial.println("\nWIFI credentials from EEPROM: ");
Serial.println(ssid);
Serial.println(passwd);
WiFi.hostname(wiFiHostname);
WiFi.begin(ssid, passwd);
for(int i=0; i<21; i++){
if(WiFi.status() != WL_CONNECTED){
if(i<20){
delay(500);
Serial.println("Waiting to connect…");
wifiFailure = false;
}else{
Serial.println("Connection failed!");
wifiFailure = true;
}
}else{
Serial.println("Connected on IP adress: ");
Serial.println(WiFi.localIP()); //Print the local IP
break;
}
}
if(wifiFailure){
Serial.println("Creating ConfigAP");
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP(cnfSSID);
Serial.print("AP IP address: ");
Serial.println(WiFi.softAPIP());
// if DNSServer is started with "*" for domain name, it will reply with
// provided IP to all DNS request - Captive Portal for WiFi settings
dnsServer.start(dnsPort, "*", apIP);
//Associating the config handler function to all paths
server.onNotFound(handleConf);
}else{
WiFi.mode(WIFI_STA);
//Associating the handler functions to the paths
server.on("/toggle", toggle);
server.on("/status", stat);
server.on("/", root);
}
server.begin();
Serial.println("HTTP server started");
}
void loop() {
if(wifiFailure){
dnsServer.processNextRequest();
}
server.handleClient();
}
/* -------------------- */
/* CONFIG PROGRAM */
/* -------------------- */
void writeCredentials(String newSSID, String newPASSWD){
char SSIDbuff[30];
char PASSWDbuff[30];
newSSID.toCharArray(SSIDbuff, 30);
newPASSWD.toCharArray(PASSWDbuff, 30);
Serial.println("Writing credentials to EEPROM.");
EEPROM.begin(sizeof(SSIDbuff)+sizeof(PASSWDbuff));
int EEaddress = 0;
EEPROM.put(EEaddress, SSIDbuff);
EEaddress += sizeof(SSIDbuff);
EEPROM.put(EEaddress, PASSWDbuff);
EEPROM.commit();
EEPROM.end();
Serial.println("Restarting ESP");
}
void handleConf() {
if ((server.hasArg("ssid")&&server.hasArg("passwd"))) {
Serial.println("Writing WiFi credentials:");
Serial.println(server.arg("ssid"));
Serial.println(server.arg("passwd"));
writeCredentials(server.arg("ssid"), server.arg("passwd"));
server.send(200, "text/html", cnfhtml1+cnfSuccessHtml+cnfhtml2);
delay(1000);
Serial.println("Restarting now.");
resetFunc();
}
else {
server.send(200, "text/html", cnfhtml1+cnfhtml2);
}
}
/* -------------------- */
/* MAIN PROGRAM */
/* -------------------- */
void toggle(){
relaisState = !relaisState;
digitalWrite(relaisPin, relaisState);
String statusMsg = "";
if (relaisState){
statusMsg = "on";
Serial.println("Relais is now ON");
}else{
statusMsg = "off";
Serial.println("Relais is now OFF");
}
server.send(200, "text/plain", statusMsg);
}
void stat(){
String statusMsg = "";
if (relaisState){
statusMsg = "on";
}else{
statusMsg = "off";
}
server.send(200, "text/plain", statusMsg);
Serial.println("Current status has been sent.");
}
void root(){
server.send(200, "text/html", html);
Serial.println("Web interface has been sent.");
}