I would be very appreciative of any assistance or advice that is given.
My setup is Arduino Mega2560 plus Ethernet shield using the Ethernet.h library
A simple web server with a single html page containing a single button using the html GET method to send the data back to the Mega's ip address for reading. In future there will be more buttons and text fields that will need parsing and further processing.
I am getting stuck on reading the data with client.read().
On the first click of the button it works fine: the buffer contains "?btn=001" and strLine == "?btn001" but, subsequent to that, I get strange results.
On each, subsequent click, the data seems to append to strLine
It seems to me that there is a buffer or cache that is not being cleared somewhere.
All help is appreciated.
Code:
//Libraries:
#include <SPI.h>
#include <Ethernet.h>
const int bufferMax = 128;
char buffer[bufferMax];
int bufferSize;
String strLine = "";
//General declarations:
boolean reading = false;
//Ethernet
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x24, 0x77 };
IPAddress ip(192,168,0, 177);
String strIPaddress = "192.168.0.177";
static char baseurl[]="http://192.168.0.177/";
EthernetServer server(80);
/***********************************************************************************************
- VOID setup
************************************************************************************************/
void setup(){
// Open serial communications port:
Serial.begin(9600);
Serial.println("--------------Setup routine-------------------");
Ethernet.begin(mac, ip);
server.begin();
}
/***********************************************************************************************
- VOID loop
************************************************************************************************/
void loop(){
checkForClient();
}
/***********************************************************************************************
- VOID checkForClient
************************************************************************************************/
void checkForClient(){
// listen for incoming clients
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()) {
if(!sentHeader){
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("");
client.println("");
client.println("");
client.println("<body style="background:#4582C7">");
client.println("");
client.println("
Arduino Mega (");
client.println(")
client.println("<form METHOD=GET action="" + String(baseurl) + "">");
client.println("<INPUT type=hidden name=btn value="001"><INPUT type="submit" value="Button" style="background:#FF0303">");
client.println("");
client.println("");
sentHeader = true;
} //!sentHeader
char c = client.read();
if(reading && c == ' ') reading = false;
if(c == '?') reading = true; //found the ?, begin reading the info
if(reading){
//Read in the received character
if (c == '\n') // \n is : carriage return - the end of the line will end this
break;
else
if (bufferSize < bufferMax) { // fill up the buffer with this string untill or untill 128 bits (bufferMax)
buffer[bufferSize++] = c;
strLine = strLine + c;
} else
break;
}
if (c == '\n' && currentLineIsBlank) break;
if (c == '\n') {
currentLineIsBlank = true;
}else if (c != '\r') {
currentLineIsBlank = false;
}
}//available
} //connected
delay(1); // give the web browser time to receive the data
client.println(strLine);
client.stop(); // close the connection:
parseReceivedRequest(strLine); //parse and process the GET return
strLine = ""; //clear the returned value
} //client
} //void checkForClient
/***********************************************************************************************
- VOID ParseReceivedRequest
************************************************************************************************/
void parseReceivedRequest(String strValue){
Serial.println("in ParseReceivedRequest........");
Serial.println(strValue);
}