简体   繁体   中英

How to do a GET request from Arduino Uno R3 using WiFly?

I looked around and couldn't find anything resembling what I would like to do.

Background: I'm trying to get football information from this API called http://api.football-data.org onto my Arduino via the WiFly shield. Now here's some code I came up with to somehow get this information. However, this code doesn't work yet.

#include <SPI.h>
#include "WiFly.h" //include the WiFly experimental library

char server[] = "api.football-data.org";
WiFlyClient client(server, 80);

void setup(){

  Serial.begin(9600);
  Serial.println("Serial Begun :D");
  WiFly.begin();
  Serial.println("WiFly Begun :D");

  Serial.print("IP: ");
  Serial.println(WiFly.ip()); //print out WiFly IP

}

void loop(){

  if(client){
    while(client.connected()){
      if(client.available()){
        char c = client.read();
        delay(10);
        Serial.print(c);

        if(c == '\n'){
          Serial.println("connected");
          client.print("GET http://api.football-data.org/alpha/soccerseasons/357/leagueTable");
          Serial.print("GET http://api.football-data.org/alpha/soccerseasons/357/leagueTable");
          client.println(" HTTP/1.1");
          Serial.println(" HTTP/1.1");
          client.println("Host: api.football-data.org");
          Serial.println("Host: api.football-data.org");
          client.println("X-Auth-Token: (My Token here)");
          Serial.println("X-Auth-Token: (My Token here)");  
          client.println("Connection: close");
          Serial.println("Connection: close");
          client.println();
        }

      }
    }
    delay(100);
    client.flush();
    client.stop();
  }
}

What changes should I make so that I can get a proper HTTP response back from the api.football-data.org server?

I got it to work based on some examples I found in the WiFly library, particularly the WiFly_WebClient example. Really useful one right there. Here's the fully functioning code:

#include <SPI.h>
#include "WiFly.h" //include the WiFly experimental library
#include "Credentials.h"


WiFlyClient client("api.football-data.org", 80);

void setup(){

  Serial.begin(9600);
  Serial.println("Serial Begun :D");
  WiFly.begin();

  if (!WiFly.join(ssid, passphrase)) {
    Serial.println("Association failed.");
    while (1) {
      // Hang on failure.
    }
  } 

  Serial.print("connecting to server...");
  if(client.connect()){
    Serial.println("connected");
    client.print("GET http://api.football-data.org/alpha/soccerseasons/357/leagueTable");
    Serial.print("GET http://api.football-data.org/alpha/soccerseasons/357/leagueTable");
    client.println(" HTTP/1.1");
    Serial.println(" HTTP/1.1");
    client.println("Host: api.football-data.org");
    Serial.println("Host: api.football-data.org");
    client.println("X-Auth-Token: My Token");
    Serial.println("X-Auth-Token: My Token");  
    client.println("Connection: close");
    Serial.println("Connection: close");
    client.println();
  } else{
    Serial.println("connection failed");
  }


}

void loop(){

  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM