Add support for several bootmode

This commit is contained in:
Mathieu Maret 2016-03-03 21:41:12 +01:00
parent 1f9986da85
commit 7292b20d87
1 changed files with 83 additions and 68 deletions

View File

@ -6,15 +6,22 @@
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>
#include <ArduinoOTA.h>
#include <errno.h>
#define EEPROM_SIZE 512
char eeprom[EEPROM_SIZE];
/* Mode could be 0 for Setup, 1 for normal use
* Setup mode is trigger by setting GPIO3 to ground or at first boot
#define BOOTMODE_SETUP 0
#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. */
const char *ssid = "ESPConfigurator";
@ -22,7 +29,7 @@ const char *ssid = "ESPConfigurator";
ESP8266WebServer server(80);
/* 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() {
server.send(200, "text/html", "<h1>You are connected</h1><br/>"
@ -52,10 +59,10 @@ void handleGpio() {
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;
eeprom = "1;"+ssid+";"+password+";"+host+";";
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";";
if (eeprom.length() > EEPROM_SIZE )
return -EMSGSIZE;
@ -82,7 +89,7 @@ void handleSave() {
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");
return;
}
@ -105,14 +112,21 @@ void handleNotFound(){
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;
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;
}
//Read SSID
*ssid = &eeprom[2];
@ -140,9 +154,9 @@ void readEEPROM(int &configured, char **ssid, char **password, char **host){
}
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;
if(!configured || forceSetup ){
if (bootmode == BOOTMODE_SETUP || forceSetup) {
Serial.println("Configuring access point...");
/* You can set a password to the AP here */
WiFi.softAP(ssid);
@ -174,7 +188,7 @@ void setupWifi(int configured, int forceSetup, char *confSsid, char *confPasswor
void setup() {
pinMode(3, OUTPUT);
int configured;
int bootMode;
char *confSsid;
char *confPassword;
char *confHost;
@ -187,12 +201,13 @@ void setup() {
Serial.swap(); //Switch Serial on GPIO 13 & 15
pinMode(3, INPUT_PULLUP);
int txStatus = digitalRead(3);
//Serial.swap(); // Switch back
Serial.swap(); // Switch back
EEPROM.begin(EEPROM_SIZE);
readEEPROM(configured, &confSsid, &confPassword, &confHost);
if(configured){
readEEPROM(bootMode, &confSsid, &confPassword, &confHost);
if (bootMode == BOOTMODE_NORMAL || bootMode == BOOTMODE_OTA) {
Serial.println("Configuration Found !:");
Serial.println(bootMode);
Serial.println(confSsid);
Serial.println(confPassword);
Serial.println(confHost);
@ -205,12 +220,12 @@ void setup() {
Serial.print(txStatus);
Serial.println();
setupWifi(configured, txStatus == 0, confSsid, confPassword, confHost);
setupWifi(bootMode, txStatus == 0, confSsid, confPassword, confHost);
server.on("/", handleRoot);
server.on("/setup", handleSetup);
server.on("/save", handleSave);
if(configured)
if (bootMode == BOOTMODE_NORMAL)
server.on("/gpio", handleGpio);
server.onNotFound(handleNotFound);
server.begin();