2016-03-04 00:44:17 +01:00
|
|
|
/* Generic GPIO Control by HTTP REST interface */
|
|
|
|
/* Modify GPIO by accessing DEVICE_IP/gpio?gpio=[GPIO]&value=[0|1] */
|
|
|
|
/* At first boot it creates a WiFi access point */
|
|
|
|
/* and provide a web server on it, so you can configure Wifi ssid */
|
|
|
|
/* Wifi passwd, and a mDNS hostname */
|
|
|
|
/* In this mode, device will be available at 192.168.4.1 */
|
|
|
|
/* Device can also be put in OTA Mode: In this case, if you have */
|
|
|
|
/* a little flash (512K), it better to disable SPIFFS in */
|
|
|
|
/* "Flash Size" Menu. Use espota.py to upload OTA */
|
|
|
|
/* After passing in OTA mode, next boot will be in setup mode */
|
2015-10-26 01:35:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
#include <ESP8266WiFi.h>
|
2016-03-03 21:41:12 +01:00
|
|
|
#include <WiFiClient.h>
|
2015-10-26 01:35:48 +01:00
|
|
|
#include <ESP8266WebServer.h>
|
|
|
|
#include <ESP8266mDNS.h>
|
|
|
|
#include <EEPROM.h>
|
2016-03-03 21:41:12 +01:00
|
|
|
#include <ArduinoOTA.h>
|
2015-10-26 01:35:48 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#define EEPROM_SIZE 512
|
|
|
|
char eeprom[EEPROM_SIZE];
|
|
|
|
|
2016-03-03 21:41:12 +01:00
|
|
|
#define BOOTMODE_SETUP 0
|
|
|
|
#define BOOTMODE_NORMAL 1
|
|
|
|
#define BOOTMODE_OTA 2
|
|
|
|
|
|
|
|
/* EEPROM LAYOUT
|
2016-03-04 00:44:17 +01:00
|
|
|
"BOOTMODE;SSID;PASSWORD;HOSTNAME;"
|
2016-03-03 21:41:12 +01:00
|
|
|
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
|
2015-10-26 01:35:48 +01:00
|
|
|
*/
|
2016-03-03 21:41:12 +01:00
|
|
|
|
2015-10-26 01:35:48 +01:00
|
|
|
|
|
|
|
/* 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
|
2016-03-03 21:41:12 +01:00
|
|
|
connected to this access point to see it.
|
|
|
|
*/
|
2015-10-26 01:35:48 +01:00
|
|
|
void handleRoot() {
|
2016-03-03 21:41:12 +01:00
|
|
|
server.send(200, "text/html", "<h1>You are connected</h1><br/>"
|
|
|
|
"<a href=\"/setup\">Setup</a><br/>"
|
|
|
|
"<a href=\"/gpio?gpio=2&value=1\">ON</a><br/>"
|
|
|
|
"<a href=\"/gpio?gpio=2&value=0\">OFF</a><br/>"
|
|
|
|
);
|
2015-10-26 01:35:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleSetup() {
|
|
|
|
server.send(200, "text/html", "<form action=\"/save\" method=\"get\">"
|
2016-03-03 21:41:12 +01:00
|
|
|
"<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>");
|
2015-10-26 01:35:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleGpio() {
|
2016-03-03 21:41:12 +01:00
|
|
|
if (!server.hasArg("gpio") || !server.hasArg("value")) {
|
2015-10-26 01:35:48 +01:00
|
|
|
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);
|
2016-03-03 21:41:12 +01:00
|
|
|
server.send(200, "text/html", "<h1>GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "</h1>");
|
2015-10-26 01:35:48 +01:00
|
|
|
}
|
|
|
|
|
2016-03-03 21:41:12 +01:00
|
|
|
int saveConfig(int bootMode, String ssid, String password, String host ) {
|
2015-10-26 01:35:48 +01:00
|
|
|
String eeprom;
|
|
|
|
|
2016-03-03 21:41:12 +01:00
|
|
|
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";";
|
|
|
|
|
|
|
|
if (eeprom.length() > EEPROM_SIZE )
|
2015-10-26 01:35:48 +01:00
|
|
|
return -EMSGSIZE;
|
|
|
|
|
2016-03-03 21:41:12 +01:00
|
|
|
Serial.println("Saving " + eeprom);
|
|
|
|
|
|
|
|
for (int i = 0; i < eeprom.length() && i < EEPROM_SIZE; i++) {
|
|
|
|
EEPROM.write(i, eeprom.charAt(i));
|
2015-10-26 01:35:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EEPROM.commit();
|
|
|
|
delay(100);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-04 00:46:06 +01:00
|
|
|
int saveBootMode(int bootMode){
|
|
|
|
EEPROM.write(0,String(bootMode).charAt(0));
|
|
|
|
EEPROM.commit();
|
|
|
|
delay(100);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-26 01:35:48 +01:00
|
|
|
void handleSave() {
|
|
|
|
String password;
|
|
|
|
String ssid;
|
|
|
|
String hostName;
|
|
|
|
|
2016-03-03 21:41:12 +01:00
|
|
|
if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host")) {
|
2015-10-26 01:35:48 +01:00
|
|
|
server.send(500, "text/plain", "Bad arguments\r\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-03 21:41:12 +01:00
|
|
|
if (saveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"), server.arg("host")) < 0) {
|
2015-10-26 01:35:48 +01:00
|
|
|
server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-03-03 21:41:12 +01:00
|
|
|
|
2015-10-26 01:35:48 +01:00
|
|
|
server.send(200, "text/html", "<h1>Configuration Saved</h1>");
|
|
|
|
}
|
|
|
|
|
2016-03-03 21:41:12 +01:00
|
|
|
void handleNotFound() {
|
2015-10-26 01:35:48 +01:00
|
|
|
String message = "File Not Found\n\n";
|
|
|
|
message += "URI: ";
|
|
|
|
message += server.uri();
|
|
|
|
message += "\nMethod: ";
|
2016-03-03 21:41:12 +01:00
|
|
|
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
2015-10-26 01:35:48 +01:00
|
|
|
message += "\nArguments: ";
|
|
|
|
message += server.args();
|
|
|
|
message += "\n";
|
2016-03-03 21:41:12 +01:00
|
|
|
for (uint8_t i = 0; i < server.args(); i++) {
|
2015-10-26 01:35:48 +01:00
|
|
|
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
|
|
|
}
|
|
|
|
server.send(404, "text/plain", message);
|
|
|
|
}
|
|
|
|
|
2016-03-03 21:41:12 +01:00
|
|
|
void readEEPROM(int &bootMode, char **ssid, char **password, char **host) {
|
2015-10-26 01:35:48 +01:00
|
|
|
|
|
|
|
int i = 2;
|
|
|
|
|
2016-03-03 21:41:12 +01:00
|
|
|
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;
|
2015-10-26 01:35:48 +01:00
|
|
|
return;
|
2016-03-03 21:41:12 +01:00
|
|
|
}
|
2015-10-26 01:35:48 +01:00
|
|
|
|
|
|
|
//Read SSID
|
|
|
|
*ssid = &eeprom[2];
|
|
|
|
do {
|
2016-03-03 21:41:12 +01:00
|
|
|
eeprom[i] = EEPROM.read(i);
|
2015-10-26 01:35:48 +01:00
|
|
|
i++;
|
2016-03-03 21:41:12 +01:00
|
|
|
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
|
|
|
|
eeprom[i - 1] = '\0';
|
2015-10-26 01:35:48 +01:00
|
|
|
|
|
|
|
//Read password
|
|
|
|
*password = &eeprom[i];
|
|
|
|
do {
|
2016-03-03 21:41:12 +01:00
|
|
|
eeprom[i] = EEPROM.read(i);
|
2015-10-26 01:35:48 +01:00
|
|
|
i++;
|
2016-03-03 21:41:12 +01:00
|
|
|
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
|
|
|
|
eeprom[i - 1] = '\0';
|
2015-10-26 01:35:48 +01:00
|
|
|
|
|
|
|
//Read HostName
|
|
|
|
*host = &eeprom[i];
|
|
|
|
do {
|
2016-03-03 21:41:12 +01:00
|
|
|
eeprom[i] = EEPROM.read(i);
|
2015-10-26 01:35:48 +01:00
|
|
|
i++;
|
2016-03-03 21:41:12 +01:00
|
|
|
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
|
|
|
|
eeprom[i - 1] = '\0';
|
2015-10-26 01:35:48 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-03 21:41:12 +01:00
|
|
|
void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) {
|
2015-10-26 01:35:48 +01:00
|
|
|
IPAddress myIP;
|
2016-03-03 21:41:12 +01:00
|
|
|
if (bootmode == BOOTMODE_SETUP || forceSetup) {
|
2015-10-26 01:35:48 +01:00
|
|
|
Serial.println("Configuring access point...");
|
|
|
|
/* You can set a password to the AP here */
|
|
|
|
WiFi.softAP(ssid);
|
|
|
|
myIP = WiFi.softAPIP();
|
2016-03-03 21:41:12 +01:00
|
|
|
} else {
|
2015-10-26 01:35:48 +01:00
|
|
|
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!");
|
2016-03-03 21:41:12 +01:00
|
|
|
while (1) {
|
2015-10-26 01:35:48 +01:00
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Serial.println("mDNS responder started");
|
|
|
|
myIP = WiFi.localIP();
|
|
|
|
}
|
2016-03-03 21:41:12 +01:00
|
|
|
|
2015-10-26 01:35:48 +01:00
|
|
|
Serial.print("My IP address: ");
|
|
|
|
Serial.println(myIP);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
pinMode(3, OUTPUT);
|
2016-03-03 21:41:12 +01:00
|
|
|
|
|
|
|
int bootMode;
|
2015-10-26 01:35:48 +01:00
|
|
|
char *confSsid;
|
|
|
|
char *confPassword;
|
|
|
|
char *confHost;
|
2016-03-03 21:41:12 +01:00
|
|
|
|
|
|
|
delay(1000);
|
|
|
|
Serial.begin(115200);
|
2015-10-26 01:35:48 +01:00
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
// Get GPIO 3 Status
|
2016-03-03 21:41:12 +01:00
|
|
|
Serial.swap(); //Switch Serial on GPIO 13 & 15
|
2015-10-26 01:35:48 +01:00
|
|
|
pinMode(3, INPUT_PULLUP);
|
|
|
|
int txStatus = digitalRead(3);
|
2016-03-03 21:41:12 +01:00
|
|
|
Serial.swap(); // Switch back
|
2015-10-26 01:35:48 +01:00
|
|
|
|
|
|
|
EEPROM.begin(EEPROM_SIZE);
|
2016-03-03 21:41:12 +01:00
|
|
|
readEEPROM(bootMode, &confSsid, &confPassword, &confHost);
|
|
|
|
if (bootMode == BOOTMODE_NORMAL || bootMode == BOOTMODE_OTA) {
|
2015-10-26 01:35:48 +01:00
|
|
|
Serial.println("Configuration Found !:");
|
2016-03-03 21:41:12 +01:00
|
|
|
Serial.println(bootMode);
|
2015-10-26 01:35:48 +01:00
|
|
|
Serial.println(confSsid);
|
|
|
|
Serial.println(confPassword);
|
|
|
|
Serial.println(confHost);
|
|
|
|
Serial.println();
|
2016-03-03 21:41:12 +01:00
|
|
|
} else {
|
2015-10-26 01:35:48 +01:00
|
|
|
Serial.println("No configuration saved");
|
|
|
|
}
|
2016-03-03 21:41:12 +01:00
|
|
|
|
2015-10-26 01:35:48 +01:00
|
|
|
Serial.print("GPIO 3 status :");
|
|
|
|
Serial.print(txStatus);
|
2016-03-03 21:41:12 +01:00
|
|
|
Serial.println();
|
2015-10-26 01:35:48 +01:00
|
|
|
|
2016-03-03 21:41:12 +01:00
|
|
|
setupWifi(bootMode, txStatus == 0, confSsid, confPassword, confHost);
|
2015-10-26 01:35:48 +01:00
|
|
|
|
2016-03-03 21:41:12 +01:00
|
|
|
server.on("/", handleRoot);
|
2015-10-26 01:35:48 +01:00
|
|
|
server.on("/setup", handleSetup);
|
|
|
|
server.on("/save", handleSave);
|
2016-03-03 21:41:12 +01:00
|
|
|
if (bootMode == BOOTMODE_NORMAL)
|
2015-10-26 01:35:48 +01:00
|
|
|
server.on("/gpio", handleGpio);
|
|
|
|
server.onNotFound(handleNotFound);
|
2016-03-03 21:41:12 +01:00
|
|
|
server.begin();
|
|
|
|
Serial.println("HTTP server started");
|
2015-10-26 01:35:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2016-03-03 21:41:12 +01:00
|
|
|
server.handleClient();
|
2015-10-26 01:35:48 +01:00
|
|
|
}
|