controlling a LED throw HTML page
Posted: Sun Dec 21, 2014 4:38 pm
I'm trying to move from ethernet shield to ESP8266.
I'm looking for an example of controlling a LED throw HTML page something equal to this example from the ethernet shield:
THANKS.
I'm looking for an example of controlling a LED throw HTML page something equal to this example from the ethernet shield:
Code: Select all
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 150 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
int ledPin = 7;
void setup(){
pinMode(ledPin, OUTPUT); //pin selected to control
// Test LED
digitalWrite(ledPin, HIGH); // set pin high
delay(500);
digitalWrite(ledPin, LOW); // set pin low
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
}
//if HTTP request has ended
if (c == '\n') {
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>Home Automation - Testing a LED</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1 style=\"color:blue;\">Home Automation - Testing a LED</H1>");
client.println("<hr>");
client.println("<br>");
client.println("<H2><a href=\"/?lighton\"\">Turn On Light</a><br></H2>");
client.println("<H2><a href=\"/?lightoff\"\">Turn Off Light</a><br></H2>");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
// control arduino pin
if(readString.indexOf("?lighton") >0) //checks for on
{
digitalWrite(ledPin, HIGH); // set pin high
}
else{
if(readString.indexOf("?lightoff") >0) //checks for off
{
digitalWrite(ledPin, LOW); // set pin low
}
}
//clearing string for next read
readString="";
}
}
}
}
}
THANKS.