I copied this code from a website. It's desined to allow you to turn on and off a LED remotely via the web. The code works fine but I'm at a loss as to how it detects whether you've checked the box and hit submit.
I believe it has something to do with if(readString.indexOf("L=1") >0) but I'm not even sure what that line is doing and where it is getting the data.
Thanks.
#include <SPI.h>
#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
#include <Udp.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = {
192, 168, 1, 110 }; // ip in lan
byte gateway[] = {
192, 168, 1, 1 }; // internet access via router
byte subnet[] = {
255, 255, 255, 0 }; //subnet mask
Server server(80); //server port
byte sampledata=50; //some sample data - outputs 2 (ascii = 50 DEC)
int ledPin = 4; // LED pin
char link[]="http://www.christophercommunication.org"; //link data
String readString = String(30); //string for fetching data from address
boolean LEDON = false; //LED status flag
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
//Set pin 4 to output
pinMode(ledPin, OUTPUT);
//enable serial datada print
Serial.begin(9600);
}
void loop(){
// Create a client connection
Client 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; //replaces readString.append(c);
}
//output chars to serial port
Serial.print(c);
//if HTTP request has ended
if (c == '\n') {
//dirty skip of "GET /favicon.ico HTTP/1.1"
if (readString.indexOf("?") <0)
{
//skip everything
}
else
//lets check if LED should be lighted
if(readString.indexOf("L=1") >0)//replaces if(readString.contains("L=1"))
{
//led has to be turned ON
digitalWrite(ledPin, HIGH); // set the LED on
LEDON = true;
}
else{
//led has to be turned OFF
digitalWrite(ledPin, LOW); // set the LED OFF
LEDON = false;
}
// now output HTML data starting with standard header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
//set background to White
client.print("<body style=background-color:white>");
//send first heading
client.println("<font color='blue'><h1>Web Page based control. </h1><p><h2> over ethernet and Microcontroller</font></p></h2>");
client.println("<hr />");
client.println("<hr />");
//output some sample data to browser
client.println("<font color='black' size='5'>Abstract: This Local webpage controls LED (for representational purposes, otherwise, any electronic equipment).");
client.println("<font color='black' size='4'><p>A microcontroller (Arduino) connected to the internet via the Ethernet shield acts as a middleman to form a communication link between the internet and the microcontroller.</p>");
client.println("<font color='black' size='4'><p>Check / Uncheck the box for the LED to command lighting on or off respectively</p>");
client.println("
");//some space between lines
client.println("<hr />");
//printing some link
client.println("<font color='blue' size='5'>Link: ");
client.print("<a href=");
client.print(link);
client.println(">Visit our site- Chrostopher Communications!</a>");
client.println("
");
client.println("<hr />");
//controlling led via checkbox
client.println("<h1>LED control</h1>");
//address will look like http://192.168.1.110/?L=1 when submited
//Creating the checkbox (a type of form in HTML)
if (LEDON)
client.println("<form method=get name=LED><input type=checkbox name=L value=1 CHECKED>LED
<input type=submit value=submit></form>");
else
client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED
<input type=submit value=submit></form>");
client.println("
");
//printing LED status
client.print("<font size='5'>LED status: ");
if (LEDON)
client.println("<font color='green' size='5'>ON");
else
client.println("<font color='grey' size='5'>OFF");
client.println("<hr />");
client.println("<hr />");
client.println("</body></html>");
//clearing string for next read
readString="";
//stopping client
client.stop();
}
}
}
}
}