Hello,
I recently bought an ethernet shield for use with my arduino uno and I was trying a project out of jeremy blums book. The project is to create a simple web server that allows you to toggle some LEDS from a webpage. I was successfully able to host up the webpage and connect the LEDS but for some reason when I click the button to toggle an LED from the webpage it is immediately toggling the LED ON then OFF. It should only be toggling it once (ON, or OFF) but it is toggling twice. I will include pictures and my source code, and help would be appreciated!!
/*
Exploring Arduino - Code Listing 14-2: Web Server Code
http://www.exploringarduino.com/content/ch13
Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com )
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 as published by
the Free Software Foundation.
*/
//Arduino Web Server
//Some code adapted under MIT License from
//http://bildr.org/2011/06/arduino-ethernet-pin-control/
#include <Ethernet.h>
#include <SPI.h>
const int BLUE =5;
const int GREEN =6;
const int RED =7;
const int SPEAKER =3;
//For controlling LEDS and the speaker
//If you want to control additional things, add variables to control them here.
int freq = 0;
int pin;
//Set to your MAC address!
//It should be on your sticker. If you can't find it,
//make one up, or use this one.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xFE, 0xED };
//Start the server on port 80
EthernetServer server = EthernetServer(80); //port 80
boolean receiving = false; //To keep track of whether we are getting data.
void setup()
{
Serial.begin(9600);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
//Connect with DHCP
if (!Ethernet.begin(mac))
{
Serial.println("Could not Configure Ethernet with DHCP.");
return;
}
else
{
Serial.println("Ethernet Configured!");
}
//Start the server
server.begin();
Serial.print("Server Started.\nLocal IP: ");
Serial.println(Ethernet.localIP());
}
void loop()
{
EthernetClient client = server.available();
if (client)
{
//An HTTP request ends with a blank line
boolean currentLineIsBlank = true;
boolean sentHeader = false;
while (client.connected())
{
if (client.available())
{
char c = client.read(); //Read from the incoming buffer
if(receiving && c == ' ') receiving = false; //done receiving
if(c == '?') receiving = true; //found arguments
//This looks at the GET requests
if(receiving)
{
//An LED command is specified with an L
if (c == 'L')
{
Serial.print("Toggling Pin ");
pin = client.parseInt();
Serial.println(pin);
digitalWrite(pin, !digitalRead(pin));
break;
}
//Add similarly formatted else if statements here
//TO CONTROL ADDITIONAL THINGS
}
//Print out the response header and the HTML page
if(!sentHeader)
{
//Send a standard HTTP response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html\n");
//Red toggle button
client.println("<form action='' method='get'>");
client.println("<input type='hidden' name='L' value='7' />");
client.println("<input type='submit' value='Toggle Red' />");
client.println("</form>");
//Green toggle button
client.println("<form action='' method='get'>");
client.println("<input type='hidden' name='L' value='6' />");
client.println("<input type='submit' value='Toggle Green' />");
client.println("</form>");
//Blue toggle button
client.println("<form action='' method='get'>");
client.println("<input type='hidden' name='L' value='5' />");
client.println("<input type='submit' value='Toggle Blue' />");
client.println("</form>");
//Add additional forms forms for controlling more things here.
sentHeader = true;
}
if (c == '\n' && currentLineIsBlank) break;
if (c == '\n')
{
currentLineIsBlank = true;
}
else if (c != '\r')
{
currentLineIsBlank = false;
}
}
}
delay(200); //Give the web browser time to receive the data
client.stop(); //Close the connection:
}
}