Hi all!
I have an Arduino web server that changes the colors of RGB LED according to the input of the user; The problem is whenever the user submits the form for the first time; the color will not be changed in the following submissions unless I upload the code to the board again. AND when I use flush or empty the HTTP header string the LED turns on only for a few seconds then turns off. I want it to have the color that the user has submitted until the next submission.
I appreciate your suggestions and guidance, since I am new to both Arduino and also to programming.
Here is My code:
#include <SPI.h>
#include <Ethernet.h>
#include <string.h>
#define MaxHeaderLength 60
// MAC address from Ethernet shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x0A, 0xC6 };
IPAddress ip(192,168, 15, 4); // IP address
EthernetServer server(80); // create a server at port 80
int LED_status;
int r;
int g;
int b;
String HttpHeader = String(MaxHeaderLength);
char c;
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
pinMode(3, OUTPUT); //red LED
pinMode(5, OUTPUT);//Green LED
pinMode(6, OUTPUT);//Blue LED
HttpHeader="";
}
void loop(){
// Create a client connection
analogWrite(3,r);
analogWrite(5,g);
analogWrite(6,b);
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
c = client.read();
//read MaxHeaderLength number of characters in the HTTP header
if (HttpHeader.length() < MaxHeaderLength)
{
//store characters to string
HttpHeader = HttpHeader + c;
}
}
//if HTTP request has ended
if (c == '\n') {
int andIndex = HttpHeader.indexOf('&');
int andIndexsecond = HttpHeader.indexOf('&',andIndex+1);
int equalIndex = HttpHeader.indexOf('=');
int secequalIndex = HttpHeader.indexOf('=',equalIndex+1);
int thirdequalIndex = HttpHeader.indexOf('=', secequalIndex+1);
int percentIndex = HttpHeader.indexOf('%');
int secondpercentIndex = HttpHeader.indexOf('%', percentIndex+1);
int thirdpercentIndex = HttpHeader.indexOf('%', secondpercentIndex+1);
int cIndex = HttpHeader.indexOf('C');
int secondcIndex = HttpHeader.indexOf('C', cIndex+1);
String LED = HttpHeader.substring(equalIndex+1, andIndex);
String red = HttpHeader.substring(thirdequalIndex+1,secondpercentIndex );
String green = HttpHeader.substring(cIndex+1,thirdpercentIndex);
String blue = HttpHeader.substring(secondcIndex+1);
LED_status = LED.toInt();
r = red.toInt();
g = green.toInt();
b = blue.toInt();
// start of web page
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web pages
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino LED Control</title>");
client.println("<style>");
client.println("body { background: #333; }");
client.println("h1 { text-align: center;");
client.println("font-family: 'Source Sans Pro', sans-serif;");
client.println("font-size: 6em; color:#eee;");
client.println("line-height: 63px;");
client.println("margin: 46px 0;}");
client.println("</style>");
client.println(F("<script type=\"text/javascript\"> function newBackgroundColor(color){ r = parseInt(color.substr(1,2),16); g = parseInt(color.substr(3,2),16); b = parseInt(color.substr(5,2),16);var rgb= +r+','+g+','+b; document.background = color;document.colorForm.selectedcolor.value = rgb;} "));
client.println("</script>");
client.println("</head>");
client.println("<body>");
client.println("<h1>Magic Lamp</h1>");
client.println("<form name=\"colorForm\" method=\"get\" onSubmit=\"http://192.168.15.4\">");
client.print("<input type='radio' name=r value='1'> ON
");
client.print("<input type='radio' name=r value='0' checked> OFF
");
client.println("<p>Choose a color</p>");
client.println("<input name=\"colorpicker\" type=\"color\" onchange=\"newBackgroundColor(colorpicker.value);\"> ");
client.println("<p> Selected Color: </p>");
client.println("<input name=\"selectedcolor\" type=\"text\"> ");
client.println("<input value=\"Change\" type=\"submit\">");
client.println("</form>");
client.println("</body>");
client.println("</html>");
//stopping client
client.stop();
HttpHeader = "";
}
}
}
}