Split Sketch in several files
This commit is contained in:
parent
1e20d8a397
commit
758c9f8808
75
WiFiAccessPointConfigurator/EEPROM.ino
Normal file
75
WiFiAccessPointConfigurator/EEPROM.ino
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
/* 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 saveConfig(int bootMode, String ssid, String password, String host ) {
|
||||||
|
String eeprom;
|
||||||
|
|
||||||
|
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";";
|
||||||
|
|
||||||
|
if (eeprom.length() > EEPROM_SIZE )
|
||||||
|
return -EMSGSIZE;
|
||||||
|
|
||||||
|
Serial.println("Saving " + eeprom);
|
||||||
|
|
||||||
|
for (int i = 0; i < eeprom.length() && i < EEPROM_SIZE; i++) {
|
||||||
|
EEPROM.write(i, eeprom.charAt(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
EEPROM.commit();
|
||||||
|
delay(100);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int saveBootMode(int bootMode){
|
||||||
|
EEPROM.write(0,String(bootMode).charAt(0));
|
||||||
|
EEPROM.commit();
|
||||||
|
delay(100);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void readEEPROM(int &bootMode, char **ssid, char **password, char **host) {
|
||||||
|
|
||||||
|
int i = 2;
|
||||||
|
|
||||||
|
int boot = EEPROM.read(0);
|
||||||
|
|
||||||
|
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];
|
||||||
|
do {
|
||||||
|
eeprom[i] = EEPROM.read(i);
|
||||||
|
i++;
|
||||||
|
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
|
||||||
|
eeprom[i - 1] = '\0';
|
||||||
|
|
||||||
|
//Read password
|
||||||
|
*password = &eeprom[i];
|
||||||
|
do {
|
||||||
|
eeprom[i] = EEPROM.read(i);
|
||||||
|
i++;
|
||||||
|
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
|
||||||
|
eeprom[i - 1] = '\0';
|
||||||
|
|
||||||
|
//Read HostName
|
||||||
|
*host = &eeprom[i];
|
||||||
|
do {
|
||||||
|
eeprom[i] = EEPROM.read(i);
|
||||||
|
i++;
|
||||||
|
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
|
||||||
|
eeprom[i - 1] = '\0';
|
||||||
|
|
||||||
|
}
|
84
WiFiAccessPointConfigurator/WebServer.ino
Normal file
84
WiFiAccessPointConfigurator/WebServer.ino
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
void handleRoot() {
|
||||||
|
server.send(200, "text/html", "<h1>You are connected</h1><br/>"
|
||||||
|
"<a href=\"/setup\">Setup</a><br/>"
|
||||||
|
"<a href=\"/otamode\">OTA mode</a><br/>"
|
||||||
|
"<a href=\"/gpio?gpio=2&value=1\">ON</a><br/>"
|
||||||
|
"<a href=\"/gpio?gpio=2&value=0\">OFF</a><br/>"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleSetup() {
|
||||||
|
server.send(200, "text/html", "<form action=\"/save\" method=\"get\">"
|
||||||
|
"<div><label for=\"ssid\">Wifi SSID :</label> <input type=\"text\" name=\"ssid\" /></div>"
|
||||||
|
"<div><label for=\"password\">Wifi Password :</label><input type=\"password\" name=\"password\" /> </div>"
|
||||||
|
"<div><label for=\"host\">Hostname :</label><input type=\"text\" name=\"host\" /> </div>"
|
||||||
|
"<div class=\"button\"> <button type=\"submit\">Save</button></div>"
|
||||||
|
"</form>");
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleGpio() {
|
||||||
|
if (!server.hasArg("gpio") || !server.hasArg("value")) {
|
||||||
|
server.send(500, "text/plain", "Bad arguments\r\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pinMode(server.arg("gpio").toInt(), OUTPUT);
|
||||||
|
digitalWrite(server.arg("gpio").toInt(), server.arg("value").toInt());
|
||||||
|
server.send(200, "text/html", "<h1>GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "</h1>");
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleSave() {
|
||||||
|
String password;
|
||||||
|
String ssid;
|
||||||
|
String hostName;
|
||||||
|
|
||||||
|
if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host")) {
|
||||||
|
server.send(500, "text/plain", "Bad arguments\r\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
server.send(200, "text/html", "<h1>Configuration Saved</h1><br/>"
|
||||||
|
"You can reboot now");
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleOTA() {
|
||||||
|
Serial.println("Boot mode Set to OTA");
|
||||||
|
saveBootMode(BOOTMODE_OTA);
|
||||||
|
server.send(200, "text/html", "<h1>OTA Mode set</h1><br/>"
|
||||||
|
"You can reboot now");
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleNotFound() {
|
||||||
|
String message = "File Not Found\n\n";
|
||||||
|
message += "URI: ";
|
||||||
|
message += server.uri();
|
||||||
|
message += "\nMethod: ";
|
||||||
|
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
||||||
|
message += "\nArguments: ";
|
||||||
|
message += server.args();
|
||||||
|
message += "\n";
|
||||||
|
for (uint8_t i = 0; i < server.args(); i++) {
|
||||||
|
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
||||||
|
}
|
||||||
|
server.send(404, "text/plain", message);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setupWebServer(int bootmode) {
|
||||||
|
server.on("/", handleRoot);
|
||||||
|
server.on("/setup", handleSetup);
|
||||||
|
server.on("/save", handleSave);
|
||||||
|
if (bootmode == BOOTMODE_NORMAL){
|
||||||
|
server.on("/gpio", handleGpio);
|
||||||
|
server.on("/otamode", handleOTA);
|
||||||
|
}
|
||||||
|
server.onNotFound(handleNotFound);
|
||||||
|
server.begin();
|
||||||
|
Serial.println("HTTP server started");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -20,7 +20,6 @@
|
|||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
|
||||||
//#define ENABLE_EXTRA_GPIO
|
//#define ENABLE_EXTRA_GPIO
|
||||||
|
|
||||||
#define EEPROM_SIZE 512
|
#define EEPROM_SIZE 512
|
||||||
@ -33,159 +32,24 @@ char eeprom[EEPROM_SIZE];
|
|||||||
|
|
||||||
int mode;
|
int mode;
|
||||||
|
|
||||||
/* 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
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/* Set these to your desired credentials. */
|
/* Set these to your desired credentials. */
|
||||||
const char *ssid = "ESPConfigurator";
|
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
|
/* WebServer decl*/
|
||||||
connected to this access point to see it.
|
void handleRoot();
|
||||||
*/
|
void handleSetup();
|
||||||
void handleRoot() {
|
void handleGpio();
|
||||||
server.send(200, "text/html", "<h1>You are connected</h1><br/>"
|
void handleSave();
|
||||||
"<a href=\"/setup\">Setup</a><br/>"
|
void handleOTA();
|
||||||
"<a href=\"/otamode\">OTA mode</a><br/>"
|
void handleNotFound();
|
||||||
"<a href=\"/gpio?gpio=2&value=1\">ON</a><br/>"
|
void setupWebServer(int bootmode);
|
||||||
"<a href=\"/gpio?gpio=2&value=0\">OFF</a><br/>"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleSetup() {
|
/* EEPROM decl */
|
||||||
server.send(200, "text/html", "<form action=\"/save\" method=\"get\">"
|
int saveConfig(int bootMode, String ssid, String password, String host );
|
||||||
"<div><label for=\"ssid\">Wifi SSID :</label> <input type=\"text\" name=\"ssid\" /></div>"
|
int saveBootMode(int bootMode);
|
||||||
"<div><label for=\"password\">Wifi Password :</label><input type=\"password\" name=\"password\" /> </div>"
|
void readEEPROM(int &bootMode, char **ssid, char **password, char **host);
|
||||||
"<div><label for=\"host\">Hostname :</label><input type=\"text\" name=\"host\" /> </div>"
|
|
||||||
"<div class=\"button\"> <button type=\"submit\">Save</button></div>"
|
|
||||||
"</form>");
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleGpio() {
|
|
||||||
if (!server.hasArg("gpio") || !server.hasArg("value")) {
|
|
||||||
server.send(500, "text/plain", "Bad arguments\r\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
pinMode(server.arg("gpio").toInt(), OUTPUT);
|
|
||||||
digitalWrite(server.arg("gpio").toInt(), server.arg("value").toInt());
|
|
||||||
server.send(200, "text/html", "<h1>GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "</h1>");
|
|
||||||
}
|
|
||||||
|
|
||||||
int saveConfig(int bootMode, String ssid, String password, String host ) {
|
|
||||||
String eeprom;
|
|
||||||
|
|
||||||
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";";
|
|
||||||
|
|
||||||
if (eeprom.length() > EEPROM_SIZE )
|
|
||||||
return -EMSGSIZE;
|
|
||||||
|
|
||||||
Serial.println("Saving " + eeprom);
|
|
||||||
|
|
||||||
for (int i = 0; i < eeprom.length() && i < EEPROM_SIZE; i++) {
|
|
||||||
EEPROM.write(i, eeprom.charAt(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
EEPROM.commit();
|
|
||||||
delay(100);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int saveBootMode(int bootMode){
|
|
||||||
EEPROM.write(0,String(bootMode).charAt(0));
|
|
||||||
EEPROM.commit();
|
|
||||||
delay(100);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleSave() {
|
|
||||||
String password;
|
|
||||||
String ssid;
|
|
||||||
String hostName;
|
|
||||||
|
|
||||||
if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host")) {
|
|
||||||
server.send(500, "text/plain", "Bad arguments\r\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
server.send(200, "text/html", "<h1>Configuration Saved</h1><br/>"
|
|
||||||
"You can reboot now");
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleOTA() {
|
|
||||||
Serial.println("Boot mode Set to OTA");
|
|
||||||
saveBootMode(BOOTMODE_OTA);
|
|
||||||
server.send(200, "text/html", "<h1>OTA Mode set</h1><br/>"
|
|
||||||
"You can reboot now");
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleNotFound() {
|
|
||||||
String message = "File Not Found\n\n";
|
|
||||||
message += "URI: ";
|
|
||||||
message += server.uri();
|
|
||||||
message += "\nMethod: ";
|
|
||||||
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
|
||||||
message += "\nArguments: ";
|
|
||||||
message += server.args();
|
|
||||||
message += "\n";
|
|
||||||
for (uint8_t i = 0; i < server.args(); i++) {
|
|
||||||
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
|
||||||
}
|
|
||||||
server.send(404, "text/plain", message);
|
|
||||||
}
|
|
||||||
|
|
||||||
void readEEPROM(int &bootMode, char **ssid, char **password, char **host) {
|
|
||||||
|
|
||||||
int i = 2;
|
|
||||||
|
|
||||||
int boot = EEPROM.read(0);
|
|
||||||
|
|
||||||
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];
|
|
||||||
do {
|
|
||||||
eeprom[i] = EEPROM.read(i);
|
|
||||||
i++;
|
|
||||||
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
|
|
||||||
eeprom[i - 1] = '\0';
|
|
||||||
|
|
||||||
//Read password
|
|
||||||
*password = &eeprom[i];
|
|
||||||
do {
|
|
||||||
eeprom[i] = EEPROM.read(i);
|
|
||||||
i++;
|
|
||||||
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
|
|
||||||
eeprom[i - 1] = '\0';
|
|
||||||
|
|
||||||
//Read HostName
|
|
||||||
*host = &eeprom[i];
|
|
||||||
do {
|
|
||||||
eeprom[i] = EEPROM.read(i);
|
|
||||||
i++;
|
|
||||||
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
|
|
||||||
eeprom[i - 1] = '\0';
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) {
|
void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) {
|
||||||
IPAddress myIP;
|
IPAddress myIP;
|
||||||
@ -257,19 +121,6 @@ void setupOTA() {
|
|||||||
Serial.println(ESP.getFreeSketchSpace());
|
Serial.println(ESP.getFreeSketchSpace());
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupWebServer() {
|
|
||||||
server.on("/", handleRoot);
|
|
||||||
server.on("/setup", handleSetup);
|
|
||||||
server.on("/save", handleSave);
|
|
||||||
if (mode == BOOTMODE_NORMAL){
|
|
||||||
server.on("/gpio", handleGpio);
|
|
||||||
server.on("/otamode", handleOTA);
|
|
||||||
}
|
|
||||||
server.onNotFound(handleNotFound);
|
|
||||||
server.begin();
|
|
||||||
Serial.println("HTTP server started");
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(3, OUTPUT);
|
pinMode(3, OUTPUT);
|
||||||
|
|
||||||
@ -311,7 +162,7 @@ void setup() {
|
|||||||
if (mode == BOOTMODE_OTA) {
|
if (mode == BOOTMODE_OTA) {
|
||||||
setupOTA();
|
setupOTA();
|
||||||
} else {
|
} else {
|
||||||
setupWebServer();
|
setupWebServer(mode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user