Add possibility to set the GPIO mode

This commit is contained in:
Mathieu Maret 2015-10-01 01:06:19 +02:00
parent 4f76fd6025
commit 146d8b65ce

View File

@ -9,6 +9,7 @@
#include <ESP8266WiFi.h>
#define URL_GPIO "/gpio"
#define URL_MODE "/mode"
const char* ssid = "freebox_sarah";
const char* password = "password";
@ -17,18 +18,35 @@ const char* password = "password";
// specify the port to listen on as an argument
WiFiServer server(80);
enum request_type {INVALID, GET_VALUE, SET_VALUE};
enum requestType {INVALID, GET_VALUE, SET_VALUE, SET_MODE};
int mode[] = {INPUT, OUTPUT, INPUT_PULLUP};
struct request {
int gpio;
int value;
request_type type;
requestType type;
};
int parseRequest(String &raw, struct request &req ) {
int index, end;
String url;
index = raw.indexOf(URL_MODE);
if ( index != -1) {
String subUrl = raw.substring(index + strlen(URL_MODE));
if (parseRequest(subUrl, req) >= 0) {
if ( req.value >= 0 && req.value <= 2) {
req.type = SET_MODE;
req.value = mode[req.value];
return 0;
} else
goto invalid;
} else
goto invalid;
}
index = raw.indexOf(URL_GPIO);
if (index == -1)
@ -152,6 +170,18 @@ void loop() {
s += request.gpio;
s += " is ";
s += digitalRead(request.gpio);
} else if (request.type == SET_MODE) {
Serial.print("Setting mode of GPIO");
Serial.print(request.gpio);
Serial.print(" to mode ");
Serial.print(request.value);
pinMode(request.gpio, request.value);
s += "GPIO";
s += request.gpio;
s += " mode is now ";
s += (request.value);
}
s += "</html>\n";