Advertisement
Guest User

Untitled

a guest
Dec 5th, 2013
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Web client
  3. This sketch connects to a website (http://www.google.com)
  4. using an Arduino Wiznet Ethernet shield.
  5. Circuit:
  6. * Ethernet shield attached to pins 10, 11, 12, 13
  7. created 18 Dec 2009
  8. modified 9 Apr 2012
  9. by David A. Mellis
  10. */
  11. //#include <SPI.h> - not needed on Galileo
  12. #include <Ethernet.h>
  13. // Enter a MAC address for your controller below.
  14. // Newer Ethernet shields have a MAC address printed on a sticker on the shield
  15. //byte mac[] = { 0x00, 0x13, 0x20, 0xFF, 0x11, 0xF1 }; // Arduino's artificial mac address - not needed on Galileo
  16. IPAddress server(173,194,33,104); // Google
  17. // Initialize the Ethernet client library
  18. // with the IP address and port of the server
  19. // that you want to connect to (port 80 is default for HTTP):
  20. EthernetClient client;
  21. void setup() {
  22. // Open serial communications and wait for port to open:
  23.   Serial.begin(9600);
  24.    while (!Serial) {
  25.     ; // wait for serial port to connect. Needed for Leonardo only
  26.   }
  27.   delay(3000);
  28.   // start the Ethernet connection: - not needed on Galileo
  29.   ///if (Ethernet.begin(mac) == 0) { - not needed on Galileo
  30.     ///Serial.println("Failed to configure Ethernet using DHCP"); - not needed on Galileo
  31.     // no point in carrying on, so do nothing forevermore: - not needed on Galileo
  32.     ///for(;;) - not needed on Galileo
  33.     ///  ; - not needed on Galileo
  34.   ///} - not needed on Galileo
  35.   // give the Ethernet shield a second to initialize: - not needed on Galileo
  36.   ///delay(1000); - not needed on Galileo
  37.   Serial.println("connecting...");
  38.   // if you get a connection, report back via serial:
  39.   if (client.connect(server, 80)) {
  40.     Serial.println("connected");
  41.     // Make a HTTP request:
  42.     client.println("GET /search?q=arduino HTTP/1.0");
  43.     client.println();
  44.   }
  45.   else {
  46.     // if you didn't get a connection to the server:
  47.     Serial.println("connection failed");
  48.   }
  49. }
  50. void loop()
  51. {
  52.   // if there are incoming bytes available
  53.   // from the server, read them and print them:
  54.   if (client.available()) {
  55.     char c = client.read();
  56.     Serial.print(c);
  57.   }
  58.   // if the server's disconnected, stop the client:
  59.   if (!client.connected()) {
  60.     Serial.println();
  61.     Serial.println("disconnecting.");
  62.     client.stop();
  63.     // do nothing forevermore:
  64.     for(;;)
  65.       ;
  66.   }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement