Pages: [1]   Go Down
Author Topic: Adding token to end of URL  (Read 562 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I have an Ethernet shield and relay board hooked up to my Arduino.  I have a webpage set up and its working fine.  At the moment I have on/off buttons for the relay that, when clicked, append a couple letters with tokens onto the URL.  This is then tokenized using strtok, etc. and the switch is turned on or off...  

Code:
...
...
char *response = strtok(readString, "?");

          response = strtok(NULL, "=");
          if(strcmp (response, "A") == 0){
            val = 1;
          }
          else {
            val = atoi(response);
          }
          response = strtok(NULL, ".");
          tempHr = atoi(response);
          response = strtok(NULL, ".");
          tempMin = atoi(response);
          response = strtok(NULL, ";");
            if(strcmp (response, "T") == 0){
              trueFalse = 'T';
            }
            else
              trueFalse = 'F';
...
...

So now I'm working on a different part of the code where I need to have the user be able to set a weekly schedule for the relays.  I'd like to create text boxes for each day of the week in which the user can input a time like "5:30" and then a GET HTTP request will be sent so that the Arduino can change the time accordingly.
I have it somewhat working, however I'm having problems with tokenizing the requests. The following code sends the request and works fine:
Code:
         client.print("<form action='http://192.168.1.134:8088/' method='GET'>");
          client.print("<input type='text' name='2' maxlength='5' size='5' />");
          client.print("<intput type='submit' value='Submit' /></form><br>");

But I don't know how I can tokenize it when I already have code in the loop that is tokenizing a different type of information for the individual on/off buttons.  For example, when the on/off button is pressed for relay 2 the URL will look like this:

http://192.168.1.134:8088/?2=T;

And when I type in the text box to change the time to "5:30" for relay 2the URL will be:

http://192.168.1.134:8088/?2=5%3A30

I realize the ":" is encoded to %3A so I will have to deal with that as well (although 5.30 works fine but I'd rather not use that);

So as you can see, my problem is that I need to tokenize the URL differently depending on the request.  Is there an easy way of doing this?

Thanks for any help!  I hope this all made sense...

EDIT: I've been thinking of adapting my project using the concepts here http://www.cs.helsinki.fi/u/ljlukkar/iot/#conclusions but I don't know anything about java.  Would it make more sense for me to try and learn to do this?
« Last Edit: December 17, 2012, 07:43:25 pm by ryanmcclure4 » Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Suggest you print out a few examples of the URL that is submitted in the GET request. I would expect to find that the form field values are URL-encoded like http://192.168.1.134:8088/?name1=value1&name2=value2.

In that case you can split the URL at the first ? to separate the parameters assignments, split the parameter assignments at & to get the individual parameter assignments, and split each assignment at the first = to get the name and value. And then you would need to parse and validate the value. It would be user-friendly to validate the field values at the client before you do the submit so that you can warn the user of errors without doing a page turnaround (and save yourself the hassle of preserving the duff values and sending the same form out again for correction) but that's not essential.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In html forms, it seems that the get query string sent is different depending on weather the submit button is used or the enter key.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
          client.print("<input type='text' name='2' maxlength='5' size='5' />");
The name attribute defines the part on the left side of the equal sign. It's hardly a good idea to name everything 2.

Try naming some of your fields 3. Or, maybe, possibly even something meaningful.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In html forms, it seems that the get query string sent is different depending on weather the submit button is used or the enter key.

That is down to the HTML author. The enter key does not implicitly submit the form so if you have a form which is submitted by the enter key (while the focus is not on a submit button) then it implies the author has included some client-side event handling code to recognise the enter key and respond by submitting the form. In that case, it's possible that the author would choose to submit the form differently from the way the submit field submits it, although that would be a strange thing to do.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For simple GET/POST parameter handling you will probably do with the Webuino library

Use unix style timestamps. JavaScript date object will be your friend.

I just wrote an examle how a pins high/low state can be controlled using a web interface but it doesn't have the timer functionality you are after.
https://github.com/lasselukkari/PinToggle/blob/master/pintoggle.ino

Logged

Pages: [1]   Go Up
Jump to: