Skip to content

Instantly share code, notes, and snippets.

@bbx10
Created July 21, 2015 03:31
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save bbx10/5a2885a700f30af75fc5 to your computer and use it in GitHub Desktop.
Save bbx10/5a2885a700f30af75fc5 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();
}
@andrewbourhis
Copy link

Does server.handleClient() run handleRoot()? Having trouble following program flow here

@CastleSeven
Copy link

handleClient() is part of the ESP8266WebServer library, it handles some of the background stuff of having a client connect to the server. If handleClient() detects that a client has sent a valid request to the server, and is attempting to access the root of the server ("/"), the function assigned in the server.on("/", handleRoot); line gets called, in this case, "handleRoot".

@GideonVanEeden
Copy link

Great example, thanks. This is the key I was looking for to figure out radio buttons. Still need to spend a lot of time figuring out all the details...

@jigneshk5
Copy link

Very helpful to me

@Olgorofi
Copy link

Good example. I test dozens of examples and only this one had stability and did not crash. I'm using it now for all my home controls. I tried and failed to implement FIXED IP in this example and I could not. If anybody can help me. FIXED IP is essential for my controls. Thank you

@Olgorofi
Copy link

Another thing: I use 2 reles and this example is for only 1. Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment