Pages: [1]   Go Down
Author Topic: What is the URL of my buttons?  (Read 1199 times)
0 Members and 1 Guest are viewing this topic.
Nijmegen, Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I have Uno R3, W5100 ethernetshield and IDE 1.0.1. I created a webserverpage with some buttons that executes some code in my program. The buttons works fine, but I also want to start these functions through an URL. I thought this was the URL, http://192.168.1.100?name=command&value=aOn but that does not work. Can anyone give me a hint? Below are some parts of my code.
Code:
void toggleLightCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
  if (type == WebServer::POST)
  {
    bool repeat;
    char name[16], value[16];
    do
    {
      repeat = server.readPOSTparam(name, 16, value, 16);

      if (strcmp(name, "command") == 0)
      {
        switchLights(value);
      }
      else if (strcmp(name, "setup") == 0)
      {
        enableReceiver();
      }
    }
    while (repeat);
    server.httpSeeOther(PREFIX);
    return;
  }
  server.httpSuccess();/* for a GET or HEAD, send the standard "it's all OK headers" */
  if (type == WebServer::GET){  /* we don't output the body for a HEAD request */
    P(message) =
      "<html><head><title>Sesam</title></head>"
      "<body><form action='/' method='POST'>ELRO<br>"
      "A<button class='positive' name='command' value='aOn'>Aan</button> <button class='negative' name='command' value='aOff'>Uit</button><br>"
      "B<button class='positive' name='command' value='bOn'>Aan</button> <button class='negative' name='command' value='bOff'>Uit</button><br>"
      "C<button class='positive' name='command' value='cOn'>Aan</button> <button class='negative' name='command' value='cOff'>Uit</button><br>"
      "D<button class='positive' name='command' value='dOn'>Aan</button> <button class='negative' name='command' value='dOff'>Uit</button><HR>"
      "QUIG<br>1<button class='positive' name='command' value='eOn'>Aan</button> <button class='negative' name='command' value='eOff'>Uit</button><br>"
      "2<button class='positive' name='command' value='fOn'>Aan</button> <button class='negative' name='command' value='fOff'>Uit</button><hr>"
      "ALL<br><button class='positive' name='command' value='AllOn'>Aan</button> <button class='negative' name='command' value='AllOff'>Uit</button><hr>"
      "GARAGE<br><button class='positive' name='command' value='Garage'>Aan</button>"
      "<hr><button class='neutral' name='setup' value='setup'>Learn</button>"
     "</form></body></html>";
    server.printP(message);
  }
}
Logged

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

"But that does not work" is too lame for us to provide any help. You'll have to do better than that.

Something happens. You have to tell us what.

It would be far simpler to parse the URL if each of your buttons had its own name. Did your parents name you and all of your siblings the same thing? I doubt it.
Logged

Nijmegen, Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok when I type http://192.168.1.100?name=command&value=aOn in the browser I get the message EPIC FAIL in the browser window. Still pretty lame? Please keep my parent out of this conversation. I appreciate people that actually help.
Logged

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

Not being an American, you probably never saw "The Bob Newhart Show". There was a main character on that show named Darryl. He had two brother - "my brother Darryl" and "my other brother Darryl". It was an endless source of confusion as to which Darryl was being referred to.

You have 4 buttons named command. That is no different from 3 brothers named Darryl.

Quote
in the browser I get the message EPIC FAIL in the browser window. Still pretty lame?
Yes, I'm afraid so. When a browser attempts to fetch a URL, there are standard error codes associated with the success or failure of the GET command that is at the root of the process. For a browser to treat all failures the same way ("EPIC FAIL") instead of telling you exactly why the GET failed (404 - URL not found, 503 - invalid access, etc.) is pretty lame.

I'd recommend trying a different browser. Of course, you need to make sure that the IP address in the URL is correct. That is done with no arguments.

Turning on Serial output on the Arduino, and opening the Serial Monitor will tell you whether a connection to the Arduino is even happening, and, if so, what the Arduino received and what it generated/sent as a result.
Logged

Nijmegen, Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When I type 192.168.1.200 in my browser I get the webpage with buttons that I expected. Please see the webserver GET code above how the webserver outputs HTML to my browser. The webserver  POST code searches for "command" in the name field and is able to pass value "aOn" or "aOff" to the function switchLights(value). I hoped that http://192.168.1.200/?name=command&value=aOn would do the same but I only see the EPIC FAIL message that the webduino webserver outputted. Does anyone have an idea what is the URL of my buttons? Clearly I don't understand yet how to pass parameters through the URL.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Suggest you make sure your sketch prints out trace information each time it receives an HTTP request so that you can see (a) that there is an incoming connection (b) what HTTP request was received (c) what the response was.

I don't know what browser you're using but it's not impossible that the behaviour you're seeing is the result of browser caching rather than a response sent from the Arduino. The trace output would make it clear whether this was the case. Also print out the Arduino's IP address at startup, in case this is changing.
Logged

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

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

I haven\t done any of this programming on arduino but when I look at the code the problem I think you have is that when you type the exact URL for a button in the web browser the web browser (as it should) sends a HTTP GET message. The buttons on the page makes the browser send a HTTP POST message (as it should). The code for switching the light is only run when a HTTP POST message is received.

If you don't understand this about GET and POST I suggest you google for some HTTP protocol description. And if you do, see if the code for the GET message can retrieve the same parameters as in the POST message. So if there are no parameters in the URL it displays the page, if there is any (valid) parameters it executes the requested function.
Logged

Nijmegen, Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Aqualize, thanks for your useful reply. I read a bit  about GET and POST. My HTML page uses POST and then the parameters are not not found in the URL but in the message body. If I want to send parameters through URL then I must use GET. In the HTML I changed the method from POST to GET and then I saw the URL http://192.168.1.200/?command=eOn, that's what I am looking for. Next step is to handle the GET request in the code in my Arduino. Thanks again.
Logged

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

Quote
Next step is to handle the GET request in the code in my Arduino.
I still think that giving each button a different name is going to make your job easier.
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9675
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple button test code that prints what the browser sends to the serial monitor.

Code:
//zoomkat 3-17-12
//simple button GET server code to control servo and arduino pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//Powering a servo from the arduino usually DOES NOT WORK.
//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>

//#include <Servo.h>
//Servo myservo;  // create servo object to control a servo

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
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

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

          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>");

          // DIY buttons
          client.println("<a href=\"/?on1\"\">ON</a>");
          client.println("<a href=\"/?off1\"\">OFF</a><br />");

          // mousedown buttons
          client.println("<br><input type=\"button\" value=\"ON\" onmousedown=\"location.href ('/?on2');\"/>");
          client.println("<input type=\"button\" value=\"OFF\" onmousedown=\"location.href ('/?off2');\"/>");       

          // mousedown radio buttons
          client.println("<br><br><input type=\"radio\" value=\"ON\" onmousedown=\"location.href ('/?on3');\"\">ON</>");
          client.println("<input type=\"radio\" value=\"OFF\" onmousedown=\"location.href ('/?off3');\"\">OFF</>");       

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

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

          ///////////////////// control arduino pin
          if(readString.indexOf("on1") >0) {
            digitalWrite(5, HIGH);
            Serial.println("Led on1");
          }
          if(readString.indexOf("off1") >0) {
            digitalWrite(5, LOW);
            Serial.println("Led off1");
          }

          if(readString.indexOf("on2") >0) {
            digitalWrite(6, HIGH);
            Serial.println("Led on2");
          }
          if(readString.indexOf("off2") >0) {
            digitalWrite(6, LOW);
            Serial.println("Led off2");
          }

          if(readString.indexOf("on3") >0) {
            digitalWrite(7, HIGH);
            Serial.println("Led on3");
          }
          if(readString.indexOf("off3") >0) {
            digitalWrite(7, LOW);
            Serial.println("Led off3");
          }
          //clearing string for next read
          readString="";         
        }
      }
    }
  }
}

Logged

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

Tasmania - Australia
Offline Offline
Sr. Member
****
Karma: 16
Posts: 310
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi jrsikken,

Just waking up with my first café, so if I misunderstand, I apologise.

Firstly, without seeing all your code, it appears to me that you may be using the WebDuino library, is this correct?
Secondly, you are issuing a http GET by supplying the data in your URL line, but looking at your code you have on the third line down the following;

Code:
if (type == WebServer::POST)
Is this not then wanting to see a http POST structure?

If so, I think the Webduino library is then defaulting to give you the lovely 'Epic Fail'.

If your code is in one file, it might be worth your while to show it all.

Tot ziens,
Paul
Logged


Nijmegen, Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks all for your replies. I nolonger need to find the URL with the same parameters as my http POST message.  I figured out how to read the paremeters from a http POST. Thanks anyway!
Logged

Pages: [1]   Go Up
Jump to: