Go Down

Topic: Having problems reading webserver requests (Read 1 time) previous topic - next topic


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.


#include <SPI.h>
#include <Ethernet.h>

const int bufferMax = 128;
char buffer[bufferMax];
int bufferSize;
String strLine = "";

//General declarations:
boolean reading = false;

byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x24, 0x77 };
IPAddress ip(192,168,0, 177);
String strIPaddress = "";
static char baseurl[]="";
EthernetServer server(80);

*                      VOID setup
void setup(){
  // Open serial communications port:
  Serial.println("--------------Setup routine-------------------");
  Ethernet.begin(mac, ip);

*                      VOID loop
void loop(){

*                      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()) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("<!DOCTYPE html>");
          client.println("<body style=\"background:#4582C7\">");
          client.println("<p><big><b>Arduino Mega (");
          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\">");
          sentHeader = true;
          }                                               //!sentHeader
        char c = client.read();
        if(reading && c == ' ') reading = false;
        if(c == '?') reading = true;                //found the ?, begin reading the info
          //Read in the received character
          if (c == '\n')                            //       \n is : carriage return <CR> - the end of the line will end this
          if (bufferSize < bufferMax) {             // fill up the buffer with this string untill <CR> or untill 128 bits (bufferMax)
              buffer[bufferSize++] = c;
              strLine = strLine + c;
          } else
         if (c == '\n' && currentLineIsBlank)  break;
         if (c == '\n') {
          currentLineIsBlank = true;
        }else if (c != '\r') {
          currentLineIsBlank = false;
    } //connected
     delay(1); // give the web browser time to receive the data
     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........");



Web page multi button test code.

Code: [Select]

//zoomkat 8-04-12
//simple button GET server code to control servo and arduino pins 5, 6, and 7
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use ' instead of " in the html
//address will look like when submited
//for use with W5100 based ethernet shields
///note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString;


void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  pinMode(6, OUTPUT); //pin selected to control
  pinMode(7, OUTPUT); //pin selected to control
  pinMode(8, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  //enable serial data print
  Serial.println("server multi pin button test 1.0"); // so I can keep track of what is loaded

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;

        //if HTTP request has ended
        if (c == '\n') {

          Serial.println(readString); //print to serial monitor for debuging

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");

          client.println("<TITLE>Arduino GET test page</TITLE>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          // For simple testing, pin 5, 6, 7, and 8 are used in buttons
          // DIY buttons
          client.println("<a href=/?on2 >ON</a>");
          client.println("<a href=/?off3 >OFF</a>");
          client.println("&nbsp;<a href=/?off357 >ALL OFF</a><br><br>");

          // mousedown buttons
          client.println("<input type=button value=ON onmousedown=location.href='/?on4'>");
          client.println("<input type=button value=OFF onmousedown=location.href='/?off5'>");       
          client.println("&nbsp;<input type=button value='ALL OFF' onmousedown=location.href='/?off3579'><br><br>");       
          // mousedown radio buttons
          client.println("<input type=radio onmousedown=location.href='/?on6'>ON</>");
          client.println("<input type=radio onmousedown=location.href='/?off7'>OFF</>");
          client.println("&nbsp;<input type=radio onmousedown=location.href='/?off3579'>ALL OFF</><br><br>");   
          // custom buttons
          client.print("<input type=submit value=ON style=width:100px;height:45px onClick=location.href='/?on8'>");
          client.print("<input type=submit value=OFF style=width:100px;height:45px onClick=location.href='/?off9'>");
          client.print("&nbsp;<input type=submit value='ALL OFF' style=width:100px;height:45px onClick=location.href='/?off3579'>");


          //stopping client

          ///////////////////// control arduino pin
          if(readString.indexOf('2') >0)//checks for 2
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led 5 On");
          if(readString.indexOf('3') >0)//checks for 3
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led 5 Off");
          if(readString.indexOf('4') >0)//checks for 4
            digitalWrite(6, HIGH);    // set pin 6 high
            Serial.println("Led 6 On");
          if(readString.indexOf('5') >0)//checks for 5
            digitalWrite(6, LOW);    // set pin 6 low
            Serial.println("Led 6 Off");
           if(readString.indexOf('6') >0)//checks for 6
            digitalWrite(7, HIGH);    // set pin 7 high
            Serial.println("Led 7 On");
          if(readString.indexOf('7') >0)//checks for 7
            digitalWrite(7, LOW);    // set pin 7 low
            Serial.println("Led 7 Off");
            if(readString.indexOf('8') >0)//checks for 8
            digitalWrite(8, HIGH);    // set pin 8 high
            Serial.println("Led 8 On");
          if(readString.indexOf('9') >0)//checks for 9
            digitalWrite(8, LOW);    // set pin 8 low
            Serial.println("Led 8 Off");
          //clearing string for next read


Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0


Thanks zoomkat - problem solved.
Much appreciated.

Go Up