Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. This sketch connects to a website (http://www.google.com)
  2. using an Arduino Wiznet Ethernet shield.
  3.  
  4. Circuit:
  5. * Ethernet shield attached to pins 10, 11, 12, 13
  6.  
  7. created 18 Dec 2009
  8. by David A. Mellis
  9. modified 9 Apr 2012
  10. by Tom Igoe, based on work by Adrian McEwen
  11.  
  12. */
  13.  
  14. #include <SPI.h>
  15. #include <Ethernet.h>
  16.  
  17. // Enter a MAC address for your controller below.
  18. // Newer Ethernet shields have a MAC address printed on a sticker on the shield
  19. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  20. // if you don't want to use DNS (and reduce your sketch size)
  21. // use the numeric IP instead of the name for the server:
  22. //IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
  23. char server[] = "www.google.com"; // name address for Google (using DNS)
  24.  
  25. // Set the static IP address to use if the DHCP fails to assign
  26. IPAddress ip(192, 168, 0, 177);
  27.  
  28. // Initialize the Ethernet client library
  29. // with the IP address and port of the server
  30. // that you want to connect to (port 80 is default for HTTP):
  31. EthernetClient client;
  32.  
  33. void setup() {
  34. // Open serial communications and wait for port to open:
  35. Serial.begin(9600);
  36. while (!Serial) {
  37. ; // wait for serial port to connect. Needed for native USB port only
  38. }
  39.  
  40. // start the Ethernet connection:
  41. if (Ethernet.begin(mac) == 0) {
  42. Serial.println("Failed to configure Ethernet using DHCP");
  43. // try to congifure using IP address instead of DHCP:
  44. Ethernet.begin(mac, ip);
  45. }
  46. // give the Ethernet shield a second to initialize:
  47. delay(1000);
  48. Serial.println("connecting...");
  49.  
  50. // if you get a connection, report back via serial:
  51. if (client.connect(server, 80)) {
  52. Serial.println("connected");
  53. // Make a HTTP request:
  54. client.print("GET / HTTP/1.1rn");
  55. client.print("Host: www.google.comrn");
  56. client.print("Content-Type: application/x-www-form-urlencodedrn");
  57. client.print("Cache-Control: no-cachern");
  58. client.print("rn");
  59. } else {
  60. // if you didn't get a connection to the server:
  61. Serial.println("connection failed");
  62. }
  63. }
  64.  
  65. void loop() {
  66. // if there are incoming bytes available
  67. // from the server, read them and print them:
  68. while (client.available()) {
  69. char c = client.read();
  70. Serial.print(c);
  71. }
  72.  
  73. // if the server's disconnected, stop the client:
  74. if (!client.connected()) {
  75. Serial.println();
  76. Serial.println("disconnecting.");
  77. client.stop();
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement