There are a few different methods I’ve seen for external input of local WiFi credentials to the ESP8266, and having them saved to the EEPROM. However, for me, none of them worked quite as advertised. It also turns out that the ESP8266 saves the most recent SSID and Password internally (even after a reflash), so using EEPROM was actually unnecessary.
Much of the code for setting up a local server / access point was borrowed from Tzapu and chriscook8. Any suggestions for improvement are welcome, as I’m very much a novice at Arduino coding.
//A simplified version of work begun by Tzapu and chriscook8
//for saving local WiFi SSID and Passwords to the ESP8266.
//Uses ESP8266 internal memory instead of the problematic EEPROM.
//To input local SSID and Password, connect to WiFi "ESP8266",
//and point your browser to "192.168.4.1".
#include "ESP8266WiFi.h"
#include <ESP8266mDNS.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp8266.h>
MDNSResponder mdns;
WiFiServer server(80);
char auth[] = "YourAuthToken"; // Blynk token
const char* APssid = "ESP8266"; // Name of access point
String st;
String rsid;
String rpass;
boolean newSSID = false;
void setup() {
Serial.begin(115200);
delay(10);
Serial.println("Startup");
// if the stored SSID and password connected successfully, exit setup
if ( testWifi()) {
Blynk.config(auth);
return;
}
// otherwise, set up an access point to input SSID and password
else
Serial.println("");
Serial.println("Connect timed out, opening AP");
setupAP();
}
int testWifi(void) {
int c = 0;
Serial.println("Waiting for Wifi to connect");
while ( c < 20 ) {
if (WiFi.status() == WL_CONNECTED) {
Serial.println("WiFi connected.");
return(1);
}
delay(500);
Serial.print(WiFi.status());
c++;
}
return(0);
}
void launchWeb(int webtype) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
Serial.println(WiFi.softAPIP());
if (!mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("Error setting up MDNS responder!");
while(1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
// Start the server
server.begin();
Serial.println("Server started");
int b = 20;
int c = 0;
while(b == 20) {
b = mdns1(webtype);
//If a new SSID and Password were sent, close the AP, and connect to local WIFI
if (newSSID == true){
newSSID = false;
//convert SSID and Password sting to char
char ssid[rsid.length()];
rsid.toCharArray(ssid, rsid.length());
char pass[rpass.length()];
rpass.toCharArray(pass, rpass.length());
Serial.println("Connecting to local Wifi");
WiFi.disconnect();
delay(500);
WiFi.begin(ssid,pass);
delay(1000);
if ( testWifi()) {
Blynk.config(auth);
return;
}
else{
Serial.println("");
Serial.println("New SSID or Password failed. Reconnect to server, and try again.");
setupAP();
return;
}
}
}
}
void setupAP(void) {
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0)
Serial.println("no networks found");
else
{
Serial.print(n);
Serial.println(" networks found");
}
Serial.println("");
st = "<ul>";
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
st += "<li>";
st +=i + 1;
st += ": ";
st += WiFi.SSID(i);
st += " (";
st += WiFi.RSSI(i);
st += ")";
st += (WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*";
st += "</li>";
}
st += "</ul>";
delay(100);
WiFi.softAP(APssid);
Serial.println("softAP");
Serial.println("");
launchWeb(1);
}
String urldecode(const char *src){ //fix encoding
String decoded = "";
char a, b;
while (*src) {
if ((*src == '%') && ((a = src[1]) && (b = src[2])) && (isxdigit(a) && isxdigit(b))) {
if (a >= 'a')
a -= 'a'-'A';
if (a >= 'A')
a -= ('A' - 10);
else
a -= '0';
if (b >= 'a')
b -= 'a'-'A';
if (b >= 'A')
b -= ('A' - 10);
else
b -= '0';
decoded += char(16*a+b);
src+=3;
}
else if (*src == '+') {
decoded += ' ';
*src++;
}
else {
decoded += *src;
*src++;
}
}
decoded += '\0';
return decoded;
}
int mdns1(int webtype){
// Check for any mDNS queries and send responses
mdns.update();
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return(20);
}
Serial.println("");
Serial.println("New client");
// Wait for data from client to become available
while(client.connected() && !client.available()){
delay(1);
}
// Read the first line of HTTP request
String req = client.readStringUntil('\r');
// First line of HTTP request looks like "GET /path HTTP/1.1"
// Retrieve the "/path" part by finding the spaces
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return(20);
}
req = req.substring(addr_start + 1, addr_end);
Serial.print("Request: ");
Serial.println(req);
client.flush();
String s;
if ( webtype == 1 ) {
if (req == "/")
{
IPAddress ip = WiFi.softAPIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 at ";
s += ipStr;
s += "<p>";
s += st;
s += "<form method='get' action='a'><label>SSID: </label><input name='ssid' length=32><input name='pass' length=64><input type='submit'></form>";
s += "</html>\r\n\r\n";
Serial.println("Sending 200");
}
else if ( req.startsWith("/a?ssid=") ) {
newSSID = true;
String qsid; //WiFi SSID
qsid = urldecode(req.substring(8,req.indexOf('&')).c_str()); //correct coding for spaces as "+"
Serial.println(qsid);
Serial.println("");
rsid = qsid;
String qpass; //Wifi Password
qpass = urldecode(req.substring(req.lastIndexOf('=')+1).c_str());//correct for coding spaces as "+"
Serial.println(qpass);
Serial.println("");
rpass = qpass;
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 ";
s += "<p> New SSID and Password accepted</html>\r\n\r\n";
}
else
{
s = "HTTP/1.1 404 Not Found\r\n\r\n";
Serial.println("Sending 404");
}
}
else
{
if (req == "/")
{
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266";
s += "<p>";
s += "</html>\r\n\r\n";
Serial.println("Sending 200");
}
else
{
s = "HTTP/1.1 404 Not Found\r\n\r\n";
Serial.println("Sending 404");
}
}
client.print(s);
Serial.println("Done with client");
return(20);
}
void loop() {
// put your main code here, to run repeatedly:
Blynk.run();
}
I’m continuing to tweak this code, and have run into a problem: Is there a command to close the softAP?
When a new SSID and Password are accepted, and the ESP8266 restarts, it continues to broadcast the softAP SSID. I’d like to close the softAP, so that whatever device was used to feed it the local credentials (usually my cell phone) reverts back to the local wifi. Instead, it’s remaining connected to the softAP, so that I have to manually go into my phone’s settings, and disconnect it from the ESP8266.
I’ve tried “WiFi.disconnect()”. Doesn’t seem to have any effect. I’ve tried creating a new softAP wit a different name. My phone remains connected to the original softAP, but now, BOTH softAPs are available.
Any thoughts? How does one shut down a softAP, once created?
Here is some stuff I dug up but haven’t had a chance to check because I don’t need the functionality right now.
To summarize what I think I read, certain flags in the original code became true and don’t get changed.
For instance, if you have called WiFi.softAP() once, the flag will be true, and WiFi.begin() will consider it and set the mode to WIFI_AP_STA (as opposed to WIFI_AP if the flag wasn’t set, which happens to be the default).
From the second link:
Links2004 commented on Aug 9
yes WiFi.mode(WIFI_STA); goes to STA mode only.
what version do you use. this behavior is changed 27 days ago.
see 6233977 and #529
WiFi.softAPdisconnect(true); shut work too, the difference to WiFi.mode(WIFI_STA); is its delete the AP config complete form the espressif config memory.
WiFi.softAPdisconnect(true) seems like a good possible option. If you are using a library from prior to middle of July this functionality won’t work.
Got curious and updating to the staging package in the arduino IDE board manager then redownloading will compile the WiFi.softAPdisconnect function. This function does just what you want. I inserted it where I would have put it in the code below(Copied from above and edited)
//A simplified version of work begun by Tzapu and chriscook8
//for saving local WiFi SSID and Passwords to the ESP8266.
//Uses ESP8266 internal memory instead of the problematic EEPROM.
//To input local SSID and Password, connect to WiFi "ESP8266",
//and point your browser to "192.168.4.1".
#include "ESP8266WiFi.h"
#include <ESP8266mDNS.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp8266.h>
MDNSResponder mdns;
WiFiServer server(80);
char auth[] = "YourAuthToken"; // Blynk token
const char* APssid = "ESP8266"; // Name of access point
String st;
String rsid;
String rpass;
boolean newSSID = false;
void setup() {
Serial.begin(115200);
delay(10);
Serial.println("Startup");
// if the stored SSID and password connected successfully, exit setup
if ( testWifi()) {
Blynk.config(auth);
return;
}
// otherwise, set up an access point to input SSID and password
else
Serial.println("");
Serial.println("Connect timed out, opening AP");
setupAP();
}
int testWifi(void) {
int c = 0;
Serial.println("Waiting for Wifi to connect");
while ( c < 20 ) {
if (WiFi.status() == WL_CONNECTED) {
Serial.println("WiFi connected.");
return(1);
}
delay(500);
Serial.print(WiFi.status());
c++;
}
return(0);
}
void launchWeb(int webtype) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
Serial.println(WiFi.softAPIP());
/* if (!mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("Error setting up MDNS responder!");
while(1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
*/
// Start the server
server.begin();
Serial.println("Server started");
int b = 20;
int c = 0;
while(b == 20) {
b = mdns1(webtype);
//If a new SSID and Password were sent, close the AP, and connect to local WIFI
if (newSSID == true){
newSSID = false;
//convert SSID and Password sting to char
char ssid[rsid.length()];
rsid.toCharArray(ssid, rsid.length());
char pass[rpass.length()];
rpass.toCharArray(pass, rpass.length());
Serial.println("Connecting to local Wifi");
delay(500);
WiFi.softAPdisconnect(true);
delay(500);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,pass);
delay(1000);
if ( testWifi()) {
Blynk.config(auth);
return;
}
else{
Serial.println("");
Serial.println("New SSID or Password failed. Reconnect to server, and try again.");
setupAP();
return;
}
}
}
}
void setupAP(void) {
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0)
Serial.println("no networks found");
else
{
Serial.print(n);
Serial.println(" networks found");
}
Serial.println("");
st = "<ul>";
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
st += "<li>";
st +=i + 1;
st += ": ";
st += WiFi.SSID(i);
st += " (";
st += WiFi.RSSI(i);
st += ")";
st += (WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*";
st += "</li>";
}
st += "</ul>";
delay(100);
WiFi.softAP(APssid);
Serial.println("softAP");
Serial.println("");
launchWeb(1);
}
String urldecode(const char *src){ //fix encoding
String decoded = "";
char a, b;
while (*src) {
if ((*src == '%') && ((a = src[1]) && (b = src[2])) && (isxdigit(a) && isxdigit(b))) {
if (a >= 'a')
a -= 'a'-'A';
if (a >= 'A')
a -= ('A' - 10);
else
a -= '0';
if (b >= 'a')
b -= 'a'-'A';
if (b >= 'A')
b -= ('A' - 10);
else
b -= '0';
decoded += char(16*a+b);
src+=3;
}
else if (*src == '+') {
decoded += ' ';
*src++;
}
else {
decoded += *src;
*src++;
}
}
decoded += '\0';
return decoded;
}
int mdns1(int webtype){
// Check for any mDNS queries and send responses
//mdns.update();
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return(20);
}
Serial.println("");
Serial.println("New client");
// Wait for data from client to become available
while(client.connected() && !client.available()){
delay(1);
}
// Read the first line of HTTP request
String req = client.readStringUntil('\r');
// First line of HTTP request looks like "GET /path HTTP/1.1"
// Retrieve the "/path" part by finding the spaces
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return(20);
}
req = req.substring(addr_start + 1, addr_end);
Serial.print("Request: ");
Serial.println(req);
client.flush();
String s;
if ( webtype == 1 ) {
if (req == "/")
{
IPAddress ip = WiFi.softAPIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 at ";
s += ipStr;
s += "<p>";
s += st;
s += "<form method='get' action='a'><label>SSID: </label><input name='ssid' length=32><input name='pass' length=64><input type='submit'></form>";
s += "</html>\r\n\r\n";
Serial.println("Sending 200");
}
else if ( req.startsWith("/a?ssid=") ) {
newSSID = true;
String qsid; //WiFi SSID
qsid = urldecode(req.substring(8,req.indexOf('&')).c_str()); //correct coding for spaces as "+"
Serial.println(qsid);
Serial.println("");
rsid = qsid;
String qpass; //Wifi Password
qpass = urldecode(req.substring(req.lastIndexOf('=')+1).c_str());//correct for coding spaces as "+"
Serial.println(qpass);
Serial.println("");
rpass = qpass;
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 ";
s += "<p> New SSID and Password accepted</html>\r\n\r\n";
}
else
{
s = "HTTP/1.1 404 Not Found\r\n\r\n";
Serial.println("Sending 404");
}
}
else
{
if (req == "/")
{
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266";
s += "<p>";
s += "</html>\r\n\r\n";
Serial.println("Sending 200");
}
else
{
s = "HTTP/1.1 404 Not Found\r\n\r\n";
Serial.println("Sending 404");
}
}
client.print(s);
Serial.println("Done with client");
return(20);
}
void loop() {
// put your main code here, to run repeatedly:
Blynk.run();
}
You should note that moving the softAPdisconnect function to the following position will help in the case the module doesn’t connect to the Wifi you input.
Completely over looked that. Its likely you will want to change WiFi.mode(WIFI_STA); to WiFi.mode(WIFI_STA_AP); so you don’t get thrown off the AP before the credential are accepted.
//convert SSID and Password sting to char
char ssid[rsid.length()];
rsid.toCharArray(ssid, rsid.length());
char pass[rpass.length()];
rpass.toCharArray(pass, rpass.length());
Serial.println("Connecting to local Wifi");
delay(500);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid,pass);
delay(1000);
if ( testWifi()) {
Blynk.config(auth);
WiFi.softAPdisconnect(true);
delay(500);
return;
}
else{
Serial.println("");
Serial.println("New SSID or Password failed. Reconnect to server, and try again.");
setupAP();
return;
}
Pretty certain the issue I am running into is multiple favicon request being sent to the module before it can handle the main request. Not sure why it was working fine a few days ago.
New client
Request: /favicon.ico
Sending 404
Done with client
I’ve tried client.stop(); and specifically sending a null header for if (req == "/favicon.ico") however neither of these seem to be the silver bullet.
I am sure sending a 404 doesn’t help because it has to do with getting requests while responding at the same time.
I’ve found code Github Weather sensor webserver Which has a busy flag which prevents the esp from receiving a new request until its done responding. I am going to look at it, but not really sure where to start for implementing it in this code. They seem to be using the client.stop(); function but it feels like that function could also terminate the desired connection if client.stop(); is related to the host and not the request.
I’ve made a few more changes, including reformatting the login page. This is working very well for me with the staging package, as suggested by @schmorrison. Also, I don’t know if it’s the Blynk library update or the addition of the yield(); statement in the loop (or a combination of the two), but my ESP8266 boards have been running rock stable for weeks.
/****************************************************************************
* A simplified version of work begun by Tzapu and chriscook8
*for saving local WiFi SSID and Passwords to the ESP8266.
*Uses ESP8266 flash memory instead of the problematic EEPROM.
*To input local SSID and Password, connect to WiFi "ESP8266",
*and point your browser to "192.168.4.1".
*
*Added option to erase current credentials by setting virtual pin 30 high in
*the Blynk app.
*****************************************************************************
*/
#include "ESP8266WiFi.h"
#include <ESP8266mDNS.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp8266.h>
MDNSResponder mdns;
WiFiServer server(80);
char auth[] = "xxxxxxxxxxxxxxxx"; // Blynk token "YourAuthToken"
const char* APssid = "ESP8266"; // Name of access point
String st;
String rsid;
String rpass;
boolean newSSID = false;
void setup() {
Serial.begin(115200);
delay(10);
Serial.println("Startup");
// if the stored SSID and password connected successfully, exit setup
if ( testWifi()) {
Blynk.config(auth);
return;
}
// otherwise, set up an access point to input SSID and password
else
Serial.println("");
Serial.println("Connect timed out, opening AP");
setupAP();
}
int testWifi(void) {
long Timeout = (millis() + 10000);
Serial.print("Waiting for Wifi to connect. ");
while (millis() < Timeout){
delay(500);
Serial.print(".");
if (WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected.");
return(1);
}
}
return(0);
}
void launchWeb(int webtype) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
Serial.println(WiFi.softAPIP());
// Start the server
server.begin();
Serial.println("Server started");
int b = 20;
int c = 0;
while(b == 20) {
b = mdns1(webtype);
//If a new SSID and Password were sent, close the AP, and connect to local WIFI
if (newSSID == true){
newSSID = false;
//convert SSID and Password sting to char
char ssid[rsid.length()];
rsid.toCharArray(ssid, rsid.length());
char pass[rpass.length()];
rpass.toCharArray(pass, rpass.length());
Serial.println("Connecting to local Wifi"); //Close the AP and connect with new SSID and P/W
WiFi.softAPdisconnect(true);
delay(500);
WiFi.begin(ssid,pass);
delay(1000);
if ( testWifi()) {
Blynk.config(auth);
return;
}
else{
Serial.println("");
Serial.println("New SSID or Password failed. Reconnect to server, and try again.");
setupAP();
return;
}
}
}
}
void setupAP(void) {
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0)
Serial.println("no networks found");
else
{
Serial.print(n);
Serial.println(" networks found");
}
Serial.println("");
st = "<ul>";
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
st += "<li>";
st += WiFi.SSID(i);
st += " (";
st += WiFi.RSSI(i);
st += ")";
st += (WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*";
st += "</li>";
}
st += "</ul>";
delay(100);
WiFi.softAP(APssid);
Serial.println("softAP");
Serial.println("");
launchWeb(1);
}
String urldecode(const char *src){ //fix encoding
String decoded = "";
char a, b;
while (*src) {
if ((*src == '%') && ((a = src[1]) && (b = src[2])) && (isxdigit(a) && isxdigit(b))) {
if (a >= 'a')
a -= 'a'-'A';
if (a >= 'A')
a -= ('A' - 10);
else
a -= '0';
if (b >= 'a')
b -= 'a'-'A';
if (b >= 'A')
b -= ('A' - 10);
else
b -= '0';
decoded += char(16*a+b);
src+=3;
}
else if (*src == '+') {
decoded += ' ';
*src++;
}
else {
decoded += *src;
*src++;
}
}
decoded += '\0';
return decoded;
}
int mdns1(int webtype){
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return(20);
}
Serial.println("");
Serial.println("New client");
// Wait for data from client to become available
while(client.connected() && !client.available()){
delay(1);
}
// Read the first line of HTTP request
String req = client.readStringUntil('\r');
// First line of HTTP request looks like "GET /path HTTP/1.1"
// Retrieve the "/path" part by finding the spaces
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return(20);
}
req = req.substring(addr_start + 1, addr_end);
Serial.print("Request: ");
Serial.println(req);
client.flush();
String s;
if ( webtype == 1 ) {
if (req == "/")
{
IPAddress ip = WiFi.softAPIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>";
s += "<font face='arial,helvetica' size='7'>";
s += "<b><label>Hello from ESP8266 at ";
s += ipStr;
s += "</label></b><p>";
s += st;
s += "<form method='get' action='a'><label>SSID: </label><input name='ssid' style='width:200px; height:60px; font-size:50px;'> ";
s += "<label>Password: </label><input name='pass' style='width:200px; height:60px; font-size:50px;'>";
s += "<p><input type='submit' style='font-size:60px'></form>";
s += "</html>\r\n\r\n";
Serial.println("Sending 200");
}
else if ( req.startsWith("/a?ssid=") ) {
newSSID = true;
String qsid; //WiFi SSID
qsid = urldecode(req.substring(8,req.indexOf('&')).c_str()); //correct coding for spaces as "+"
Serial.println(qsid);
Serial.println("");
rsid = qsid;
String qpass; //Wifi Password
qpass = urldecode(req.substring(req.lastIndexOf('=')+1).c_str());//correct for coding spaces as "+"
Serial.println(qpass);
Serial.println("");
rpass = qpass;
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 ";
s += "<p> New SSID and Password accepted</html>\r\n\r\n";
}
else
{
s = "HTTP/1.1 404 Not Found\r\n\r\n";
Serial.println("Sending 404");
}
}
else
{
if (req == "/")
{
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266";
s += "<p>";
s += "</html>\r\n\r\n";
Serial.println("Sending 200");
}
else
{
s = "HTTP/1.1 404 Not Found\r\n\r\n";
Serial.println("Sending 404");
}
}
client.print(s);
Serial.println("Done with client");
return(20);
}
//Use virtual pin 30 to overwrite wifi credentials in flash
BLYNK_WRITE(30){
if (param.asInt()){
Serial.println("Erasing SSID and restarting.");
delay(100);
WiFi.begin("FakeSSID","FakePW"); //replace current WiFi credentials with fake ones
setupAP();
}
}
void loop() {
// put your main code here, to run repeatedly:
Blynk.run();
yield();
}
just wanted to let you know that i ve updated the WiFiManager library quite a lot and it sorts the mdns error now, together with stuff like captive portal to make everything faster to configure, also new webserver code that seems more reliable. should be “drag and drop” with blynk
@chrome1000 really good job. i know how flaky the esp’s can be from the weirdest reasons.
Love the improvements @tzapulica- especially the captive portal .
Just curious… why use the EEPROM, when the ESP already stores SSID and P/W to flash? Also, may I suggest eliminating the reset? WiFiManager’s software reset still fails on my ESP-01. It might be a quirk of that particular board, but it’s the reason I avoided using software resets. Those two minor details aside, this is really awesome work!
i used EEPROM and was planning on moving forward to FS in order to provide other fields, like blynk auth token for instance to be added.
It seems it will be a choice between simplicity and flexibility, and I might go down the route of simplicity for this and drop FS and EEPROM.
I will look into dropping the reset, maybe as a flag. I think I added it mostly because there are some issues between STA and AP modes and switching between them would leave the ESP’s in some noncommunicating state. Maybe that’s fixed.
Yeah, I found some quirkiness with mode switching, too. Thankfully, the way I resolved it doesn’t leave the ESP in a noncommunicating state, but That’s probably more a case of beginner’s luck than brilliant programming skills.
For external config of the esp I also borrowed from Tzapu and chriscook8.
I didn’t want to have to re prog the esp if I changed from local to cloud etc or changed local ports etc.
I added auth code and port entries, using a port add of 1.1.1.1 to indicate cloud use.
The auth code can be copied and pasted into the field for easy use.
I also added a delay at start up, so if port 2 was pulled down just after start up, all data would be cleared and the ap started again.
That way I have the freedom of attaching the esp to where I want to work on it.
I have had no problems at all, and it is very convenient.
As to the use of ‘eeprom’ with the esp, I don’t understand what the issues are. Documentation appears to indicate that the same area in flash memory is used as in the esp config?
Sure, maybe a reset is required after the new config is made, but that is not really a problem?
Several months of use hasn’t given rise to any problems yet.