Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. // Enter a MAC address for your controller below.
  4. // Newer Ethernet shields have a MAC address printed on a sticker on the shield
  5. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  6. // if you don't want to use DNS (and reduce your sketch size)
  7. // use the numeric IP instead of the name for the server:
  8. //IPAddress server(216,58,196,67);  // numeric IP for Google (no DNS)
  9. char server[] = "www.google.com";    // name address for Google (using DNS)
  10. // Set the static IP address to use if the DHCP fails to assign
  11. IPAddress ip(192,168,12,137); //***************ใส่ IP Address ของเรา*******************
  12. // Initialize the Ethernet client library
  13. // with the IP address and port of the server
  14. // that you want to connect to (port 80 is default for HTTP):
  15. EthernetClient client;
  16. void setup() {
  17.   // Open serial communications and wait for port to open:
  18.   Serial.begin(9600);
  19.   while (!Serial) {
  20.     ; // wait for serial port to connect. Needed for native USB port only
  21.   }
  22.   // start the Ethernet connection:
  23.   if (Ethernet.begin(mac) == 0) {
  24.     Serial.println("Failed to configure Ethernet using DHCP");
  25.     // try to congifure using IP address instead of DHCP:
  26.     Ethernet.begin(mac, ip);
  27.   }
  28.   // give the Ethernet shield a second to initialize:
  29.   delay(1000);
  30.   Serial.println("connecting...");
  31.   // if you get a connection, report back via serial:
  32.   if (client.connect(server, 80)) {
  33.     Serial.println("connected");
  34.     // Make a HTTP request:
  35.     client.println("GET /search?q=arduino HTTP/1.1");
  36.     client.println("Host: www.google.com");
  37.     client.println("Connection: close");
  38.     client.println();
  39.   } else {
  40.     // if you didn't get a connection to the server:
  41.     Serial.println("connection failed");
  42.   }
  43. }
  44. void loop() {
  45.   // if there are incoming bytes available
  46.   // from the server, read them and print them:
  47.   if (client.available()) {
  48.     char c = client.read();
  49.     Serial.print(c);
  50.   }
  51.   // if the server's disconnected, stop the client:
  52.   if (!client.connected()) {
  53.     Serial.println();
  54.     Serial.println("disconnecting.");
  55.     client.stop();
  56.     // do nothing forevermore:
  57.     while (true);
  58.   }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement