Causes of periodic failure in parsing data passed in HTTP GET requests?
i'm doing , http sends /?roundlevel=x arduino ethernet shield. please note, x value hard-coded in there, can simple
code: [select]
if(readstring.indexof("roundlevel=0")>0) tests. arduino running web server sketch. have if statements set in sketch execute code when x = 0, 2 or 3. it's unreliable. when x 0 or 2, arduino 'sees' , executes code in if statement. never suceeds when x = 3. not sure whether problem 1) can't read 3, or 2)this timing issue, or 3) fails read sending in after number of requests. typically (although not always), x=3 on 7th get. there reason might fail after 7th request? requests same client , can happen rapidly within 3 seconds of 1 another. i can't seem debug issue using serial.print() because (for reasons don't understand) serial.begin(9600) interferes arduino-as-server's ability accept client requests. suggestions? code below.
code: [select]
#include <spi.h>
#include <ethernet.h>n
//ethernet shield's mac address
byte mac[] = { 0x90, 0xa2, 0xda, 0x00, 0xf7, 0x99 };
//ip address assigned network
ipaddress ip(10,132,7,115);
// router's gateway address
byte gateway[] = { 10, 132, 7, 254 };
//// subnet:
byte subnet[] = { 255, 255, 254, 0 };
ethernetserver server(80);
string roundopentag = "<game>";
string roundclosetag = "</game>";
long randnumber;
string ledcolor = "";
int seqarray[5] = {3,5,6,7,8};
int loops = 5;
string readstring = string(100);
void setup()
{
pinmode(8, output);
pinmode(7, output);
pinmode(6, output);
pinmode(5, output);
pinmode(3, output);
ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
// listen incoming clients
ethernetclient client = server.available();
if (client)
{
boolean currentlineisblank = true;
while (client.connected())
{
if (client.available())
{
//if there request,
//read request
char c = client.read();
//store characters string
readstring +=c;
if(readstring.indexof("roundlevel=0")>0)
{
roundopentag = "<one>";
roundclosetag = "</one>";
}
if(readstring.indexof("roundlevel=2")>0)
{
roundopentag = "<two>";
roundclosetag = "</two>";
loops = 6;
}
if(readstring.indexof("roundlevel=3")>0)
{
roundopentag = "<three>";
roundclosetag = "</three>";
loops = 7;
}
// if you've gotten end of line (received newline
// character) , line blank, http request has ended,
// can send reply
if (c == '\n' && currentlineisblank)
{
// send standard http response header
client.println("http/1.1 200 ok");
client.println("content-type: text/html");
client.println();
//this delay gives player second phone
//to blinky
delay(1000);
(int = 0; < loops; i++)
{
//pull randomn number between 0 , 4
randomseed(analogread(1));
randnumber = random(0, 5);
if (randnumber == 0)
{
ledcolor = "red";
}
else if (randnumber == 1)
{
ledcolor = "orange";
}
else if (randnumber == 2)
{
ledcolor = "yellow";
}
else if (randnumber == 3)
{
ledcolor = "green";
}
else
{
ledcolor = "blue";
}
digitalwrite(seqarray[randnumber], high);
// set led on 1/4 second
delay(250);
//write xml open tag
client.print(roundopentag);
//write xml text (light color)
client.print(ledcolor + "<br />");
digitalwrite(seqarray[randnumber], low);
//set led off 1/2 second
delay(250);
//write xml closed tag
client.print(roundclosetag);
}
break;
}
if (c == '\n')
{
// you're starting new line
currentlineisblank = true;
}
else if (c != '\r')
{
// you've gotten character on current line
currentlineisblank = false;
}
}
}
// give web browser time receive data
delay(1);
// close connection:
client.stop();
}
}
code: [select]
string readstring = string(100);what think doing?
code: [select]
randomseed(analogread(1));should done once in setup(), not every time need random number.
quote
i can't seem debug issue using serial.print() because (for reasons don't understand) serial.begin(9600) interferes arduino-as-server's ability accept client requests. suggestions? code below.
not me, doesn't.
personally, i'd ditch string class. making far liberal use of class, unnecessarily. creating char pointers pointing "red", "green", etc, , char pointer, ledcolor, points appropriate color name, rather dynamically assigning colors save bunch of memory. ditto open , close tags.
it easy store client request data in array, rather string object, , use pointers point parts of request, strcmp() purposes, instance. doing allow rid of string class , memory fragmenting going on.
Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > Causes of periodic failure in parsing data passed in HTTP GET requests?
arduino
Comments
Post a Comment