Skip to content

Instantly share code, notes, and snippets.

@nachocodexx
Forked from bbx10/ESPWebForm.ino
Created February 8, 2018 00:02
Show Gist options
  • Save nachocodexx/c65b383c00df2ffd8ce4ba6bfa68fbc1 to your computer and use it in GitHub Desktop.
Save nachocodexx/c65b383c00df2ffd8ce4ba6bfa68fbc1 to your computer and use it in GitHub Desktop.
Demonstrate using an http server and an HTML form to control an LED. The http server runs on the ESP8266.
/*
* Demonstrate using an http server and an HTML form to control an LED.
* The http server runs on the ESP8266.
*
* Connect to "http://esp8266WebForm.local" or "http://<IP address>"
* to bring up an HTML form to control the LED connected GPIO#0. This works
* for the Adafruit ESP8266 HUZZAH but the LED may be on a different pin on
* other breakout boards.
*
* Imperatives to turn the LED on/off using a non-browser http client.
* For example, using wget.
* $ wget http://esp8266webform.local/ledon
* $ wget http://esp8266webform.local/ledoff
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
// Fill in your WiFi router SSID and password
const char* ssid = "xxxxxxxxx";
const char* password = "yyyyyy";
MDNSResponder mdns;
ESP8266WebServer server(80);
const char INDEX_HTML[] =
"<!DOCTYPE HTML>"
"<html>"
"<head>"
"<meta name = \"viewport\" content = \"width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0\">"
"<title>ESP8266 Web Form Demo</title>"
"<style>"
"\"body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }\""
"</style>"
"</head>"
"<body>"
"<h1>ESP8266 Web Form Demo</h1>"
"<FORM action=\"/\" method=\"post\">"
"<P>"
"LED<br>"
"<INPUT type=\"radio\" name=\"LED\" value=\"1\">On<BR>"
"<INPUT type=\"radio\" name=\"LED\" value=\"0\">Off<BR>"
"<INPUT type=\"submit\" value=\"Send\"> <INPUT type=\"reset\">"
"</P>"
"</FORM>"
"</body>"
"</html>";
// GPIO#0 is for Adafruit ESP8266 HUZZAH board. Your board LED might be on 13.
const int LEDPIN = 0;
void handleRoot()
{
if (server.hasArg("LED")) {
handleSubmit();
}
else {
server.send(200, "text/html", INDEX_HTML);
}
}
void returnFail(String msg)
{
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(500, "text/plain", msg + "\r\n");
}
void handleSubmit()
{
String LEDvalue;
if (!server.hasArg("LED")) return returnFail("BAD ARGS");
LEDvalue = server.arg("LED");
if (LEDvalue == "1") {
writeLED(true);
server.send(200, "text/html", INDEX_HTML);
}
else if (LEDvalue == "0") {
writeLED(false);
server.send(200, "text/html", INDEX_HTML);
}
else {
returnFail("Bad LED value");
}
}
void returnOK()
{
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", "OK\r\n");
}
/*
* Imperative to turn the LED on using a non-browser http client.
* For example, using wget.
* $ wget http://esp8266webform/ledon
*/
void handleLEDon()
{
writeLED(true);
returnOK();
}
/*
* Imperative to turn the LED off using a non-browser http client.
* For example, using wget.
* $ wget http://esp8266webform/ledoff
*/
void handleLEDoff()
{
writeLED(false);
returnOK();
}
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 writeLED(bool LEDon)
{
// Note inverted logic for Adafruit HUZZAH board
if (LEDon)
digitalWrite(LEDPIN, 0);
else
digitalWrite(LEDPIN, 1);
}
void setup(void)
{
pinMode(LEDPIN, OUTPUT);
writeLED(false);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (mdns.begin("esp8266WebForm", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/ledon", handleLEDon);
server.on("/ledoff", handleLEDoff);
server.onNotFound(handleNotFound);
server.begin();
Serial.print("Connect to http://esp8266WebForm.local or http://");
Serial.println(WiFi.localIP());
}
void loop(void)
{
server.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment