WebServer example and querystring parsing

Is there a simple way to implement querystring parameter parsing in the WebServer example?

For example I would like to call ?key=value and read the value in the lines:

EthernetClient client = server.available(); if (client) { ....

For example I would like to call ?key=value and read the value in the lines:

What do you mean by "call ?key=value"? That is not a function that can be called.

You can collect the response from the server into a string (a NULL terminated array of chars) or a String (ugh!). Then, you can use strtok(), for strings, or some indexOf() for Strings, to find the ?, = and value terminator or the key and value tokens.

I mean that I want to retrieve the value of the querystring parameter. For example:

If I call http://192.168.2.22/?temperature=23

I would like to set the variable temp in the code to 23.

Do you have some example code for me?

Some simple server test code that checks what is contained in the query string. With the serial monitor open you can see what is being received by the server. Note the difference in what is received when the enter key us used to send vs the change button.

//zoomkat 4-05-12
//web LED code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html (or use ') 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//turns pin 5 on/off

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

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

String readString; 

//////////////////////

void setup(){

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

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("servertest1"); // 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; 
          Serial.print(c); //print what server receives to serial monitor
        } 

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

          ///////////////
          Serial.println(readString);

          //now output HTML data header

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

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

          client.println("<H1>HTML form GET example</H1>");

          client.println("<FORM ACTION=\"http://192.168.1.102:84\" method=get >");

          client.println("Pin 5 \"on\" or \"off\": <INPUT TYPE=TEXT NAME=\"LED\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\">
");

          client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Change Pin 5!\">");

          client.println("</FORM>");

          client.println("
");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          /////////////////////
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Thanx! This was exactly what I needed :slight_smile: It’s working now. I have only changed the input type to a select type:

client.println("Heat \"on\" or \"off\": <select name=\"heat\"><option value=\"heaton\">Heat ON</option><option value=\"heatoff\">Heat OFF</option></select>
");

Hi, I've been searching the forum for this topic and I finally found what I was looking for. But my inquiry is how to obtain only the value from "192.168.1.101/?input=20". My values ranges from 20 to 100 based on user input on client side.... ??

Please help.. thank you.... :confused:

Raymond_Ram: Hi, I've been searching the forum for this topic and I finally found what I was looking for. But my inquiry is how to obtain only the value from "192.168.1.101/?input=20". My values ranges from 20 to 100 based on user input on client side.... ??

Please help.. thank you.... :confused:

One approach would be to find the index of the = and then capture every thing beyond it.

ZOOMCAT there may be a problem with your code above as follows:

Web page shows correctly but when I enter "on" without quotes the inputted string is recognized OK and as shown on Monitor as follows:

servertest1 GET / HTTP/1.1 GET / HTTP/1.1

GET /favicon.ico HTTP/1.1 GET /favicon.ico HTTP/1.1

Led On GET / HTTP/1.1 GET / HTTP/1.1

then Firefox browser returns: Unable to Connect - Can't establish a connection to the server 192.168.1.1:177

Explorer and Chrome browsers display similar errors.

Have we missed something ?

Have we missed something ?

Below is simple server code that is a little more recent. This code looks for “on1” instead of just “on”, as browsers now make a second request to servers for a favorite icon, favicon.ico, and favicon contains “on”. You might try the below code to see if it works better.

//zoomkat 10-6-13
//simple button GET with iframe code
//open serial monitor to see what the arduino receives
//use the ' instead of " in html ilnes 
//address will look like http://192.168.1.102:84/ when submited
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //ethernet shield mac address
byte ip[] = { 192, 168, 1, 102 }; // arduino 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(4, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("servertest1"); // 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; 
          //Serial.print(c);
        } 

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

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

          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat");
               client.println();
               client.println();  
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

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

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          client.println("<a href='/?on1' target='inlineframe'>ON</a>"); 
          client.println("<a href='/?off' target='inlineframe'>OFF</a>"); 

          client.println("<IFRAME name=inlineframe style='display:none'>");          
          client.println("</IFRAME>");

          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on1") >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}
1 Like

Works fine zoomkat..... love your work. HNY

hi,
this code doesnt work in arduino nano. what am i missing?
thnx

this code doesn't work

? What do you mean, specifically, by "doesn't work".