Skip to content

Instantly share code, notes, and snippets.

  • Star 96 You must be signed in to star a gist
  • Fork 50 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
ESP8266 Web Server to storing ap config to EEPROM
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
ESP8266WebServer server(80);
const char* ssid = "test";
const char* passphrase = "test";
String st;
String content;
int statusCode;
void setup() {
Serial.begin(115200);
EEPROM.begin(512);
delay(10);
Serial.println();
Serial.println();
Serial.println("Startup");
// read eeprom for ssid and pass
Serial.println("Reading EEPROM ssid");
String esid;
for (int i = 0; i < 32; ++i)
{
esid += char(EEPROM.read(i));
}
Serial.print("SSID: ");
Serial.println(esid);
Serial.println("Reading EEPROM pass");
String epass = "";
for (int i = 32; i < 96; ++i)
{
epass += char(EEPROM.read(i));
}
Serial.print("PASS: ");
Serial.println(epass);
if ( esid.length() > 1 ) {
WiFi.begin(esid.c_str(), epass.c_str());
if (testWifi()) {
launchWeb(0);
return;
}
}
setupAP();
}
bool testWifi(void) {
int c = 0;
Serial.println("Waiting for Wifi to connect");
while ( c < 20 ) {
if (WiFi.status() == WL_CONNECTED) { return true; }
delay(500);
Serial.print(WiFi.status());
c++;
}
Serial.println("");
Serial.println("Connect timed out, opening AP");
return false;
}
void launchWeb(int webtype) {
Serial.println("");
Serial.println("WiFi connected");
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
Serial.print("SoftAP IP: ");
Serial.println(WiFi.softAPIP());
createWebServer(webtype);
// Start the server
server.begin();
Serial.println("Server started");
}
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");
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
delay(10);
}
}
Serial.println("");
st = "<ol>";
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 += "</ol>";
delay(100);
WiFi.softAP(ssid, passphrase, 6);
Serial.println("softap");
launchWeb(1);
Serial.println("over");
}
void createWebServer(int webtype)
{
if ( webtype == 1 ) {
server.on("/", []() {
IPAddress ip = WiFi.softAPIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
content = "<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 at ";
content += ipStr;
content += "<p>";
content += st;
content += "</p><form method='get' action='setting'><label>SSID: </label><input name='ssid' length=32><input name='pass' length=64><input type='submit'></form>";
content += "</html>";
server.send(200, "text/html", content);
});
server.on("/setting", []() {
String qsid = server.arg("ssid");
String qpass = server.arg("pass");
if (qsid.length() > 0 && qpass.length() > 0) {
Serial.println("clearing eeprom");
for (int i = 0; i < 96; ++i) { EEPROM.write(i, 0); }
Serial.println(qsid);
Serial.println("");
Serial.println(qpass);
Serial.println("");
Serial.println("writing eeprom ssid:");
for (int i = 0; i < qsid.length(); ++i)
{
EEPROM.write(i, qsid[i]);
Serial.print("Wrote: ");
Serial.println(qsid[i]);
}
Serial.println("writing eeprom pass:");
for (int i = 0; i < qpass.length(); ++i)
{
EEPROM.write(32+i, qpass[i]);
Serial.print("Wrote: ");
Serial.println(qpass[i]);
}
EEPROM.commit();
content = "{\"Success\":\"saved to eeprom... reset to boot into new wifi\"}";
statusCode = 200;
} else {
content = "{\"Error\":\"404 not found\"}";
statusCode = 404;
Serial.println("Sending 404");
}
server.send(statusCode, "application/json", content);
});
} else if (webtype == 0) {
server.on("/", []() {
IPAddress ip = WiFi.localIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
server.send(200, "application/json", "{\"IP\":\"" + ipStr + "\"}");
});
server.on("/cleareeprom", []() {
content = "<!DOCTYPE HTML>\r\n<html>";
content += "<p>Clearing the EEPROM</p></html>";
server.send(200, "text/html", content);
Serial.println("clearing eeprom");
for (int i = 0; i < 96; ++i) { EEPROM.write(i, 0); }
EEPROM.commit();
});
}
}
void loop() {
server.handleClient();
}
@ledreppe
Copy link

I get "Not found: /setting" when I enter my ESP's IP address and '/setting' after the address. There's no SSID or Pass stored in it. It connects to WiFi as shown in the screen shot attached. What do I have to do to be able to enter WiFi credentials through the web browser, so I can reboot ESP with WiFi credentials from it's EEPROM?
wifieeprom

@crileone
Copy link

Very useful sketch. It works very well :)
I'm having just a problem with the serial monitor, because I can't see anything it should print during the execution.
Do I need a particular version of the Arduino IDE or to do some setting?
thanks

@diegoiacono
Copy link

I have a litle question, When the code reads the EEPROM it always shows '?' (question marks) within the console Do you know what seems to be the problem?
Thanks in advance.

@feyyazmalkoc
Copy link

@diegoiacono you should select 115200 baud rate.

@feyyazmalkoc
Copy link

code working but need to add "WiFi.disconnect();" after line 181

@feineirz
Copy link

Thanks for your code. It's like paracetamol for me now.

@ipad0214
Copy link

nice work, but i cant see the AP after starting. There is no test ssid or something.

I think i do something wrong, any help?

Thanks in advance.

@Rajkumar181
Copy link

Any idea about FreeRTOS to store the ssid and password into eeprom.

@chetanmahajan850
Copy link

Nice All is working.

change the line 122 " if ( webtype == 0 ) " and line 169 else if (webtype == 1) and
add "WiFi.disconnect();" after line 181

Thank you so much....:)

@AnumSheraz
Copy link

Why does the SoftAP is named as Demo and its insecure (with no password) ?
I was expecting the ssid and password would be test and test !? has anyone experienced this ?

@tiennguyentn
Copy link

very good! It has its own perks. but I've been expecting which way to combine that code and static IP address.

@Shivamgautam1
Copy link

Hi, just tested your code. It will always try to connect with the WiFi irrespective of credentials are stored or not. Checking the length of esid is wrong. It will always return 32. Simply checking if esid != "" works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment