Domotique/WiFiAccessPointConfigurator/WiFiAccessPointConfigurator.ino

219 lines
5.4 KiB
Arduino
Raw Normal View History

2015-10-26 01:35:48 +01:00
/* Create a WiFi access point and provide a web server on it. */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <EEPROM.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
*/
int mode = 0;
/* Set these to your desired credentials. */
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.
*/
void handleRoot() {
server.send(200, "text/html", "<h1>You are connected</h1><br/><a href=\"/setup\">Setup</a>");
}
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;
}
digitalWrite(server.arg("gpio").toInt(), server.arg("value").toInt());
pinMode(server.arg("gpio").toInt(), OUTPUT);
server.send(200, "text/html", "<h1>GPIO" + server.arg("gpio") + " changed to " + server.arg("value") +"</h1>");
}
int saveConfig(String ssid, String password, String host ){
String eeprom;
eeprom = "1;"+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;
}
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(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>");
}
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 &configured, char **ssid, char **password, char **host){
int i = 2;
configured = (EEPROM.read(0) == '1') ;
if (!configured)
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 configured, int forceSetup, char *confSsid, char *confPassword, char *confHost){
IPAddress myIP;
if(!configured || forceSetup ){
Serial.println("Configuring access point...");
/* You can set a password to the AP here */
WiFi.softAP(ssid);
myIP = WiFi.softAPIP();
}else{
Serial.println("Connecting to Wifi...");
WiFi.begin(confSsid, confPassword);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
if (!MDNS.begin(confHost)) {
Serial.println("Error setting up MDNS responder!");
while(1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
myIP = WiFi.localIP();
}
Serial.print("My IP address: ");
Serial.println(myIP);
}
void setup() {
pinMode(3, OUTPUT);
int configured;
char *confSsid;
char *confPassword;
char *confHost;
delay(1000);
Serial.begin(115200);
Serial.println();
// Get GPIO 3 Status
Serial.swap(); //Switch Serial on GPIO 13 & 15
pinMode(3, INPUT_PULLUP);
int txStatus = digitalRead(3);
//Serial.swap(); // Switch back
EEPROM.begin(EEPROM_SIZE);
readEEPROM(configured, &confSsid, &confPassword, &confHost);
if(configured){
Serial.println("Configuration Found !:");
Serial.println(confSsid);
Serial.println(confPassword);
Serial.println(confHost);
Serial.println();
}else{
Serial.println("No configuration saved");
}
Serial.print("GPIO 3 status :");
Serial.print(txStatus);
Serial.println();
setupWifi(configured, txStatus == 0, confSsid, confPassword, confHost);
server.on("/", handleRoot);
server.on("/setup", handleSetup);
server.on("/save", handleSave);
if(configured)
server.on("/gpio", handleGpio);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}