Add support for several bootmode
This commit is contained in:
parent
1f9986da85
commit
7292b20d87
@ -6,15 +6,22 @@
|
|||||||
#include <ESP8266WebServer.h>
|
#include <ESP8266WebServer.h>
|
||||||
#include <ESP8266mDNS.h>
|
#include <ESP8266mDNS.h>
|
||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
|
#include <ArduinoOTA.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#define EEPROM_SIZE 512
|
#define EEPROM_SIZE 512
|
||||||
char eeprom[EEPROM_SIZE];
|
char eeprom[EEPROM_SIZE];
|
||||||
|
|
||||||
/* Mode could be 0 for Setup, 1 for normal use
|
#define BOOTMODE_SETUP 0
|
||||||
* Setup mode is trigger by setting GPIO3 to ground or at first boot
|
#define BOOTMODE_NORMAL 1
|
||||||
|
#define BOOTMODE_OTA 2
|
||||||
|
|
||||||
|
/* EEPROM LAYOUT
|
||||||
|
"BOOTMODE;SSID;PASSWORD;HOSTNAME;"
|
||||||
|
BOOTMODE could be 0 for Setup, 1 for normal use, 2 for OTA
|
||||||
|
Setup mode is trigger by setting GPIO3 to ground or at first boot
|
||||||
*/
|
*/
|
||||||
int mode = 0;
|
|
||||||
|
|
||||||
/* Set these to your desired credentials. */
|
/* Set these to your desired credentials. */
|
||||||
const char *ssid = "ESPConfigurator";
|
const char *ssid = "ESPConfigurator";
|
||||||
@ -22,8 +29,8 @@ const char *ssid = "ESPConfigurator";
|
|||||||
ESP8266WebServer server(80);
|
ESP8266WebServer server(80);
|
||||||
|
|
||||||
/* Just a little test message. Go to http://192.168.4.1 in a web browser
|
/* Just a little test message. Go to http://192.168.4.1 in a web browser
|
||||||
* connected to this access point to see it.
|
connected to this access point to see it.
|
||||||
*/
|
*/
|
||||||
void handleRoot() {
|
void handleRoot() {
|
||||||
server.send(200, "text/html", "<h1>You are connected</h1><br/>"
|
server.send(200, "text/html", "<h1>You are connected</h1><br/>"
|
||||||
"<a href=\"/setup\">Setup</a><br/>"
|
"<a href=\"/setup\">Setup</a><br/>"
|
||||||
@ -42,27 +49,27 @@ void handleSetup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void handleGpio() {
|
void handleGpio() {
|
||||||
if(!server.hasArg("gpio") || !server.hasArg("value")){
|
if (!server.hasArg("gpio") || !server.hasArg("value")) {
|
||||||
server.send(500, "text/plain", "Bad arguments\r\n");
|
server.send(500, "text/plain", "Bad arguments\r\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
digitalWrite(server.arg("gpio").toInt(), server.arg("value").toInt());
|
digitalWrite(server.arg("gpio").toInt(), server.arg("value").toInt());
|
||||||
pinMode(server.arg("gpio").toInt(), OUTPUT);
|
pinMode(server.arg("gpio").toInt(), OUTPUT);
|
||||||
server.send(200, "text/html", "<h1>GPIO" + server.arg("gpio") + " changed to " + server.arg("value") +"</h1>");
|
server.send(200, "text/html", "<h1>GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "</h1>");
|
||||||
}
|
}
|
||||||
|
|
||||||
int saveConfig(String ssid, String password, String host ){
|
int saveConfig(int bootMode, String ssid, String password, String host ) {
|
||||||
String eeprom;
|
String eeprom;
|
||||||
|
|
||||||
eeprom = "1;"+ssid+";"+password+";"+host+";";
|
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";";
|
||||||
|
|
||||||
if(eeprom.length() > EEPROM_SIZE )
|
if (eeprom.length() > EEPROM_SIZE )
|
||||||
return -EMSGSIZE;
|
return -EMSGSIZE;
|
||||||
|
|
||||||
Serial.println("Saving "+eeprom);
|
Serial.println("Saving " + eeprom);
|
||||||
|
|
||||||
for (int i = 0; i < eeprom.length() && i < EEPROM_SIZE; i++){
|
for (int i = 0; i < eeprom.length() && i < EEPROM_SIZE; i++) {
|
||||||
EEPROM.write(i, eeprom.charAt(i));
|
EEPROM.write(i, eeprom.charAt(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,12 +84,12 @@ void handleSave() {
|
|||||||
String ssid;
|
String ssid;
|
||||||
String hostName;
|
String hostName;
|
||||||
|
|
||||||
if(!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host")){
|
if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host")) {
|
||||||
server.send(500, "text/plain", "Bad arguments\r\n");
|
server.send(500, "text/plain", "Bad arguments\r\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(saveConfig(server.arg("ssid"), server.arg("password"), server.arg("host")) < 0){
|
if (saveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"), server.arg("host")) < 0) {
|
||||||
server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
|
server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -90,64 +97,71 @@ void handleSave() {
|
|||||||
server.send(200, "text/html", "<h1>Configuration Saved</h1>");
|
server.send(200, "text/html", "<h1>Configuration Saved</h1>");
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleNotFound(){
|
void handleNotFound() {
|
||||||
String message = "File Not Found\n\n";
|
String message = "File Not Found\n\n";
|
||||||
message += "URI: ";
|
message += "URI: ";
|
||||||
message += server.uri();
|
message += server.uri();
|
||||||
message += "\nMethod: ";
|
message += "\nMethod: ";
|
||||||
message += (server.method() == HTTP_GET)?"GET":"POST";
|
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
||||||
message += "\nArguments: ";
|
message += "\nArguments: ";
|
||||||
message += server.args();
|
message += server.args();
|
||||||
message += "\n";
|
message += "\n";
|
||||||
for (uint8_t i=0; i<server.args(); i++){
|
for (uint8_t i = 0; i < server.args(); i++) {
|
||||||
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
||||||
}
|
}
|
||||||
server.send(404, "text/plain", message);
|
server.send(404, "text/plain", message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void readEEPROM(int &configured, char **ssid, char **password, char **host){
|
void readEEPROM(int &bootMode, char **ssid, char **password, char **host) {
|
||||||
|
|
||||||
int i = 2;
|
int i = 2;
|
||||||
|
|
||||||
configured = (EEPROM.read(0) == '1') ;
|
int boot = EEPROM.read(0);
|
||||||
|
|
||||||
if (!configured)
|
if (boot == '1') {
|
||||||
|
bootMode = BOOTMODE_NORMAL;
|
||||||
|
} else if (boot == '2') {
|
||||||
|
bootMode = BOOTMODE_OTA;
|
||||||
|
} else {
|
||||||
|
//Do not need to parse EEPROM when not configured
|
||||||
|
bootMode = BOOTMODE_SETUP;
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Read SSID
|
//Read SSID
|
||||||
*ssid = &eeprom[2];
|
*ssid = &eeprom[2];
|
||||||
do {
|
do {
|
||||||
eeprom[i]= EEPROM.read(i);
|
eeprom[i] = EEPROM.read(i);
|
||||||
i++;
|
i++;
|
||||||
} while(i < EEPROM_SIZE && eeprom[i-1] != ';');
|
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
|
||||||
eeprom[i-1] = '\0';
|
eeprom[i - 1] = '\0';
|
||||||
|
|
||||||
//Read password
|
//Read password
|
||||||
*password = &eeprom[i];
|
*password = &eeprom[i];
|
||||||
do {
|
do {
|
||||||
eeprom[i]= EEPROM.read(i);
|
eeprom[i] = EEPROM.read(i);
|
||||||
i++;
|
i++;
|
||||||
} while(i < EEPROM_SIZE && eeprom[i-1] != ';');
|
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
|
||||||
eeprom[i-1] = '\0';
|
eeprom[i - 1] = '\0';
|
||||||
|
|
||||||
//Read HostName
|
//Read HostName
|
||||||
*host = &eeprom[i];
|
*host = &eeprom[i];
|
||||||
do {
|
do {
|
||||||
eeprom[i]= EEPROM.read(i);
|
eeprom[i] = EEPROM.read(i);
|
||||||
i++;
|
i++;
|
||||||
} while(i < EEPROM_SIZE && eeprom[i-1] != ';');
|
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
|
||||||
eeprom[i-1] = '\0';
|
eeprom[i - 1] = '\0';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupWifi(int configured, int forceSetup, char *confSsid, char *confPassword, char *confHost){
|
void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) {
|
||||||
IPAddress myIP;
|
IPAddress myIP;
|
||||||
if(!configured || forceSetup ){
|
if (bootmode == BOOTMODE_SETUP || forceSetup) {
|
||||||
Serial.println("Configuring access point...");
|
Serial.println("Configuring access point...");
|
||||||
/* You can set a password to the AP here */
|
/* You can set a password to the AP here */
|
||||||
WiFi.softAP(ssid);
|
WiFi.softAP(ssid);
|
||||||
myIP = WiFi.softAPIP();
|
myIP = WiFi.softAPIP();
|
||||||
}else{
|
} else {
|
||||||
Serial.println("Connecting to Wifi...");
|
Serial.println("Connecting to Wifi...");
|
||||||
WiFi.begin(confSsid, confPassword);
|
WiFi.begin(confSsid, confPassword);
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
@ -159,7 +173,7 @@ void setupWifi(int configured, int forceSetup, char *confSsid, char *confPasswor
|
|||||||
|
|
||||||
if (!MDNS.begin(confHost)) {
|
if (!MDNS.begin(confHost)) {
|
||||||
Serial.println("Error setting up MDNS responder!");
|
Serial.println("Error setting up MDNS responder!");
|
||||||
while(1) {
|
while (1) {
|
||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -174,7 +188,7 @@ void setupWifi(int configured, int forceSetup, char *confSsid, char *confPasswor
|
|||||||
void setup() {
|
void setup() {
|
||||||
pinMode(3, OUTPUT);
|
pinMode(3, OUTPUT);
|
||||||
|
|
||||||
int configured;
|
int bootMode;
|
||||||
char *confSsid;
|
char *confSsid;
|
||||||
char *confPassword;
|
char *confPassword;
|
||||||
char *confHost;
|
char *confHost;
|
||||||
@ -187,17 +201,18 @@ void setup() {
|
|||||||
Serial.swap(); //Switch Serial on GPIO 13 & 15
|
Serial.swap(); //Switch Serial on GPIO 13 & 15
|
||||||
pinMode(3, INPUT_PULLUP);
|
pinMode(3, INPUT_PULLUP);
|
||||||
int txStatus = digitalRead(3);
|
int txStatus = digitalRead(3);
|
||||||
//Serial.swap(); // Switch back
|
Serial.swap(); // Switch back
|
||||||
|
|
||||||
EEPROM.begin(EEPROM_SIZE);
|
EEPROM.begin(EEPROM_SIZE);
|
||||||
readEEPROM(configured, &confSsid, &confPassword, &confHost);
|
readEEPROM(bootMode, &confSsid, &confPassword, &confHost);
|
||||||
if(configured){
|
if (bootMode == BOOTMODE_NORMAL || bootMode == BOOTMODE_OTA) {
|
||||||
Serial.println("Configuration Found !:");
|
Serial.println("Configuration Found !:");
|
||||||
|
Serial.println(bootMode);
|
||||||
Serial.println(confSsid);
|
Serial.println(confSsid);
|
||||||
Serial.println(confPassword);
|
Serial.println(confPassword);
|
||||||
Serial.println(confHost);
|
Serial.println(confHost);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
}else{
|
} else {
|
||||||
Serial.println("No configuration saved");
|
Serial.println("No configuration saved");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,12 +220,12 @@ void setup() {
|
|||||||
Serial.print(txStatus);
|
Serial.print(txStatus);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
setupWifi(configured, txStatus == 0, confSsid, confPassword, confHost);
|
setupWifi(bootMode, txStatus == 0, confSsid, confPassword, confHost);
|
||||||
|
|
||||||
server.on("/", handleRoot);
|
server.on("/", handleRoot);
|
||||||
server.on("/setup", handleSetup);
|
server.on("/setup", handleSetup);
|
||||||
server.on("/save", handleSave);
|
server.on("/save", handleSave);
|
||||||
if(configured)
|
if (bootMode == BOOTMODE_NORMAL)
|
||||||
server.on("/gpio", handleGpio);
|
server.on("/gpio", handleGpio);
|
||||||
server.onNotFound(handleNotFound);
|
server.onNotFound(handleNotFound);
|
||||||
server.begin();
|
server.begin();
|
||||||
|
Loading…
Reference in New Issue
Block a user