Pages: [1] 2   Go Down
Author Topic: Help with GET request?  (Read 1292 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey there... Getting pretty frustrated. My code returns that it successfully sent the GET request, however I should get a phone call if it does. I know that it is an programming problem. I have censored out certain URLs in the code. Here is my code:

Code:
/*
 

#include <SPI.h>
#include <Ethernet.h>
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 3;
const int DELAY_VAL = 2;
const int MAX_SHIFT_VAL = 1023;


char keys[ROWS][COLS] = {
   {'1','2','3'}
  ,{'4','5','6'}
  ,{'7','8','9'}
  ,{'*','0','#'}
}; // map out the keys

// map out the pins
byte rowPins[ROWS] = {5,6,7,8};
byte colPins[COLS] = {2,3,4};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

byte mac[] = { 0x90, 0xA2, 0xDA, 0x04, 0x00, 0x8D }; //physical mac address
char serverName[] = "api.tropo.com"; // zoomkat's test web page server

// instantiate a network client
EthernetClient client;
int buttonState = 0; // soft reset button state
String phoneNumber = "";
String clip = "";

void setup()
{
  // start the serial library:
  Serial.begin(9600);
  // set display brightness
  Serial.write(0x7C);
  Serial.write(157);
  // set screen size in case LCD gets derpy...
  Serial.write(0xFE);
  Serial.write(6);
  Serial.write(0xFE);
  Serial.write(4);
  delay(4000);
  // start the Ethernet connection:
  // set up soft reset button

  ////attachInterrupt(0, softReset, CHANGE);
  // clear screen
  clearDisplay();
}

void loop()
{
  ////digitalWrite(softResetPin, LOW);
  // read the state of the reset button value:
 
 
  // check if the pushbutton is pressed (HIGH)
 
 
  delay(70); // delay loop to keep the LCD from redrawing stuff too much
 
  char keyPressed = keypad.getKey(); // need to check press selection on keypad

  if (keyPressed)
  {
    if (keyPressed == '#') // # and * are reveresed for some reason
    {
      clearDisplay();
      selectFirstLine();
      Serial.print("THE GAME");
      delay(500);
    }
    else if (keyPressed == '*')
    {
      clearDisplay();
      selectFirstLine();
      Serial.print("i said wut wut");
      selectSecondLine();
      Serial.print("in the *");
      delay(1000);
    }
    else
    {
      // check for complete number
      if (phoneNumber.length() == 10)
      {
        selectSecondLine();
        // determine which sound clip to play
        switch (keyPressed)
        {
        case '1':
          Serial.print("Rickroll");
          clip = "afdsfads";
          break;
        case '2':
          Serial.print("Nyan Cat");
          clip = "dafsfadfda";
          break;
        case '3':
          Serial.print("Still Alive");
          clip = "adsfads";
          break;
        case '4':
          Serial.print("Hurr Durr");
          clip = "adsfdfas";
          break;
        case '5':
          Serial.print("Heyyeahyeah");
          clip = "adfasfdafads";
          break;
        case '6':
          Serial.print("Friday");
          clip = "adadfddafs";
          break;
        case '7':
          Serial.print("Tiny Tim");
          clip = "adsfdfa";
          break;
        case '8':
          Serial.print("Banana Phone"); // OH GOD WHAT HAVE I DONE
          clip = "dasdfsafsd";
          break;
        case '9':
          Serial.print("Britney Rant");
          clip = "fadsfdsadsf";
          break;
        }
       
        delay(3000);
      }
      else
      {
        phoneNumber = phoneNumber + keyPressed;
      }
    }
  }
  else
  {
    clearDisplay();
    if (phoneNumber.length() == 10)
    {
      if (clip == "")
      {
        selectFirstLine();
        Serial.print("Trolling option:");
      }
      else
      {
        selectFirstLine();
        Serial.print("Connecting...");
        // proceed to lulz
        sendGET();
        delay(10000);
        // close connection to ensure proper re-connect
        client.stop();
        selectSecondLine();
          Serial.print("Great Success!");
          selectFirstLine();
          delay(5000);
          phoneNumber = "";
          clip = "";
      }
    }
    else
    {
      selectFirstLine();
      Serial.print("Phone number:");
     
      if (phoneNumber.length() > 0)
      {
        selectSecondLine();
        Serial.print(phoneNumber);
      }
    }
  }
}

void clearDisplay() {
  Serial.write(0xFE);
  Serial.write(0x01);
}

void selectFirstLine() {
  Serial.write(0xFE);
  Serial.write(128);
   //delay(10);
}

void selectSecondLine() {
  Serial.write(0xFE);
  Serial.write(192);
}

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /1.0/sessions?action=create&token=asdffads&number=asdffdafad HTTP/1.0"); //download text
    client.println("Host: api.tropo.com");
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2220
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Which Aduino do you have?, who is the LCD attached to it?


Mark
Logged

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

Uno R3, the lcd is here: https://www.sparkfun.com/products/9395
Logged

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

Does the "phone call" thing work correctly when you perform the identical GET request from the browser?

You sketch prints out the response to the HTTP GET request. Does that show the same response you get when you used the browser to do the GET?
Logged

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

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2220
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you should be using softSerial on pins other than 0+1, so that you can use Serial for debugging.

Mark
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Please post all of the response you get from the sketch.
Logged

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

Please post all of the response you get from the sketch.

So the response is totally correct. It does not show the output of the GET request. The GET request DOES work in a browser. The response serial is just successful and then it says Great success!
Logged

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

So the response is totally correct. It does not show the output of the GET request. The GET request DOES work in a browser. The response serial is just successful and then it says Great success!

How do you know the response is correct, if you haven't seen it?

This code looks as if it prints the response to the serial monitor:
Code:
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
  }

Why is there no output from that code?
Logged

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

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

See, that's the problem. I have no idea why! It goes through and says "success" or whatever, but never ACTUALLY sends the GET request!
Logged

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

See, that's the problem. I have no idea why! It goes through and says "success" or whatever, but never ACTUALLY sends the GET request!

Well, I don't know whether it's sending it or not, and I don't suppose you do either.

You need to sort out sendGET() so it tells you what actually happened during the process of sending the request and receiving the response. So far you haven't posted any evidence to prove that the function is even being called at all.
Logged

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

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

Its not sending it, but it acts like it did send it successfully. I will try to see what debug I can get.
« Last Edit: February 12, 2013, 07:55:40 pm by TheSpy7 » Logged

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you using a proxy?

If you can get nc (or netcat) working on your system you can echo the messages sent to a local port, e.g.

Code:
nc -l 8000

[edit] Then change your parameters to connect to localhost your system's IP address port 8000 and see what happens.
« Last Edit: February 12, 2013, 09:35:06 pm by spatula » Logged

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

Are you using a proxy?

If you can get nc (or netcat) working on your system you can echo the messages sent to a local port, e.g.

Code:
nc -l 8000

Then change your parameters to connect to localhost port 8000 and see what happens.

Eh, not interested. I just want to know why the current code doesn't work.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5874
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is required that you start the ethernet connection with Ethernet.begin(mac). You mention it with a comment, but never start it. I added it below.
Code:
void setup()
{
  // start the serial library:
  Serial.begin(9600);
  // set display brightness
  Serial.write(0x7C);
  Serial.write(157);
  // set screen size in case LCD gets derpy...
  Serial.write(0xFE);
  Serial.write(6);
  Serial.write(0xFE);
  Serial.write(4);
  delay(4000);

  // start the Ethernet connection:
  if(!Ethernet.begin(mac)) Serial.println("ethernet failed");
  else Serial.println(Ethernet.localIP());

  // set up soft reset button
  ////attachInterrupt(0, softReset, CHANGE);
  // clear screen
  clearDisplay();
}
Logged

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Eh, not interested. I just want to know why the current code doesn't work.

Fine. I took the liberty of modifying my original suggestion because it was wrong.

Perhaps you might be more interested in persistent connections under HTTP/1.0.
Logged

Pages: [1] 2   Go Up
Jump to: