I am having trouble with the Ethernet port sporadically returning gibberish when it receives a page call.
I have the Ethernet shield sitting atop an ArduinoMega 2560. I connect to the Arduino with the typical
http://192.168.0.42 page call. I append the data to control the Arduino to the end of the URL and then extract that info in the Arduino, so
http://192.168.0.42/LZR=000/MTR=0/DST=000/TST=0/ would be a typical page request.
The Arduino is supposed to reply to this request with a packet containing "arduino_pkg1<br />" and other strings as can be seen in the code. My problem is that every so often, I get a packet back that is way too long and has statements that have been completely blended, like
arduiarduino_pkg2<br />
so I can't really process the data on the PC since the reply is garbled. I've set up my PC code to only accept packets of the proper length (and therefore ungarbled), but I'd really prefer to fix the problem at the source. Right now I believe it has something to do with the timing of the page calls. The slower I poll the Ethernet port on the Arduino, the fewer errors I get, but I don't see why this would be the case since I don't send the next page call until I receive data back from the previous page call. I find that making page calls any faster than about 2Hz has a high risk of running into sporadic garbled messages being returned. I notice that if I comment out the "myservo.write(deg);" the code runs smoothly without generating errors.
#include <Servo.h>
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0, 42 };
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
Servo myservo;
int laserPos = 90;
int motorControl=0;
int motorDist=0;
int servoPin=9;
boolean testStandOn=false;
int testStandPin=2;
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
//Serial.begin(9600);
//Serial.begin(115200);
myservo.attach(servoPin);
pinMode(testStandPin, OUTPUT);
}
void zeroVars()
{
laserPos = 0;
motorControl=0;
motorDist=0;
//testStandOn=false;//need to retain state
}
void loop()
{
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
zeroVars();
int index=0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
index++;
if(index>=10&&index<=12)
{
int temp=c-48;//char to int
//Serial.println(laserPos);
laserPos+=temp*(index==10?100:index==11?10:1);
}
if(index==18)
{
motorControl=c-48;
}
if(index>=24&&index<=26)
{
int temp=c-48;//char to int
motorDist+=temp*(index==24?100:index==25?10:1);
}
if(index==32)
{
int temp=c-48;//char to int
if(temp==0) testStandOn=false;
if(temp==1) testStandOn=true;
//if some other value, do not change state
}
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
laserServo(laserPos);
setTestStnadState(testStandOn);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("arduino_pkg1<br />");
client.println("arduino_pkg2<br />");
client.println("arduino_end");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
//Ethernet.begin(mac, ip);
server.begin();
/*Serial.print("Laser Position = ");
Serial.println(laserPos);
Serial.print("Motor Control = ");
Serial.println(motorControl);
Serial.print("Distance = ");
Serial.println(motorDist);*/
}
}
void laserServo(int deg)
{
if(deg>=0&°<=180)
{
myservo.write(deg);
delay(1);
}
}
void setTestStnadState(boolean state)
{
digitalWrite(testStandPin, state);
}