Go Down

Topic: Using Ethernet Shield to read text from a web page (Read 12470 times) previous topic - next topic


I recently picked up the Arduino Ethernet shield and I'm getting started on the first piece of my project.

I want to connect to http://www.nfl.com/liveupdate/scorestrip/scorestrip.json and return the text from that page. Currently it displays
Code: [Select]


Eventually I'll be using the data that is returned, but that part isn't important now.

I got the DNSWebClient Example code working and tried to modify it for my project.  I just changed the URL for my needs.
Code: [Select]

 DNS and DHCP-based Web client

* Ethernet shield attached to pins 10, 11, 12, 13

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

// Enter a MAC address for your controller below.
byte mac[] = {  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
char serverName[] = "www.nfl.com";

String location = "/~liveupdate/scorestrip/scorestrip.json HTTP/1.0";

// Initialize the Ethernet client library
EthernetClient client;

void setup() {
 // start the serial library:
 // start the Ethernet connection:
 if (Ethernet.begin(mac) == 0) {
   Serial.println("Failed to configure Ethernet using DHCP");
   // no point in carrying on, so do nothing forevermore:
 // give the Ethernet shield a second to initialize:

 // if you get a connection, report back via serial:
 if (client.connect(serverName, 80)) {
   // Make a HTTP request:
   client.println("GET www.nfl.com/liveupdate/scorestrip/scorestrip.json HTTP/1.0");
 else {
   // if you didn't get a connection to the server:
   Serial.println("connection failed");

void loop()
 // if there are incoming bytes available
 // from the server, read them and print them:
 if (client.available()) {
   char c = client.read();

 // if the server's disconnected, stop the client:
 if (!client.connected()) {

   // do nothing forevermore:

Here is what is displayed in the serial monitor.
Code: [Select]

HTTP/1.0 400 Bad Request
Server: AkamaiGHost
Mime-Version: 1.0
Content-Type: text/html
Content-Length: 255
Expires: Sun, 22 Jul 2012 20:18:33 GMT
Date: Sun, 22 Jul 2012 20:18:33 GMT
Connection: close

<H1>Invalid URL</H1>
The requested URL "www&#46;nfl&#46;com&#47;liveupdate&#47;scorestrip&#47;scorestrip&#46;json", is invalid.<p>


I'm don't fully understand what HTTP request I should be using. I haven't found a tutorial that explains how that code works and I'm not sure what I search for in order to find more information.

Any assistance is greatly appreciated.  Thanks.

James C4S

Read what is being returned by the web server.  It is telling you the problem.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com


The browser that you typed the http string into parsed it into host name and URL. You are expected to be smart enough to do the same thing. The URL is everything after the host name, including the /.
The art of getting good answers lies in asking good questions.


Thanks guys. I got it working
Code: [Select]
client.println("GET www.nfl.com/liveupdate/scorestrip/scorestrip.json HTTP/1.0");
Code: [Select]
client.println("GET http://www.nfl.com/liveupdate/scorestrip/scorestrip.json HTTP/1.0");

and it returned
Code: [Select]
HTTP/1.0 200 OK
Server: Apache
Last-Modified: Sun, 22 Jul 2012 22:33:50 GMT
ETag: "841a4-48-b9250f80"
Content-Type: text/plain
Cache-Control: max-age=15
Expires: Sun, 22 Jul 2012 22:34:15 GMT
Date: Sun, 22 Jul 2012 22:34:00 GMT
Content-Length: 72
Connection: close
X-Akamai-Edgescape: country_code=US


Now I just need to filter the header text, but that will be easy.



would also like to know....

i can use this in a current project where a page returns raw text

Go Up