WebClientRepeating.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Repeating Web client
  3. This sketch connects to a a web server and makes a request
  4. using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
  5. the Adafruit Ethernet shield, either one will work, as long as it's got
  6. a Wiznet Ethernet module on board.
  7. This example uses DNS, by assigning the Ethernet client with a MAC address,
  8. IP address, and DNS address.
  9. Circuit:
  10. * Ethernet shield attached to pins 10, 11, 12, 13
  11. created 19 Apr 2012
  12. by Tom Igoe
  13. modified 21 Jan 2014
  14. by Federico Vanzati
  15. http://arduino.cc/en/Tutorial/WebClientRepeating
  16. This code is in the public domain.
  17. */
  18. #include <SPI.h>
  19. #include <Ethernet2.h>
  20. // assign a MAC address for the ethernet controller.
  21. // fill in your address here:
  22. byte mac[] = {
  23. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  24. };
  25. // fill in an available IP address on your network here,
  26. // for manual configuration:
  27. IPAddress ip(192, 168, 1, 177);
  28. // fill in your Domain Name Server address here:
  29. IPAddress myDns(1, 1, 1, 1);
  30. // initialize the library instance:
  31. EthernetClient client;
  32. char server[] = "www.arduino.cc";
  33. //IPAddress server(64,131,82,241);
  34. unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
  35. const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
  36. // the "L" is needed to use long type numbers
  37. void setup() {
  38. // start serial port:
  39. Serial.begin(9600);
  40. while (!Serial) {
  41. ; // wait for serial port to connect. Needed for Leonardo only
  42. }
  43. // give the ethernet module time to boot up:
  44. delay(1000);
  45. // start the Ethernet connection using a fixed IP address and DNS server:
  46. Ethernet.begin(mac, ip, myDns);
  47. // print the Ethernet board/shield's IP address:
  48. Serial.print("My IP address: ");
  49. Serial.println(Ethernet.localIP());
  50. }
  51. void loop() {
  52. // if there's incoming data from the net connection.
  53. // send it out the serial port. This is for debugging
  54. // purposes only:
  55. if (client.available()) {
  56. char c = client.read();
  57. Serial.write(c);
  58. }
  59. // if ten seconds have passed since your last connection,
  60. // then connect again and send data:
  61. if (millis() - lastConnectionTime > postingInterval) {
  62. httpRequest();
  63. }
  64. }
  65. // this method makes a HTTP connection to the server:
  66. void httpRequest() {
  67. // close any connection before send a new request.
  68. // This will free the socket on the WiFi shield
  69. client.stop();
  70. // if there's a successful connection:
  71. if (client.connect(server, 80)) {
  72. Serial.println("connecting...");
  73. // send the HTTP PUT request:
  74. client.println("GET /latest.txt HTTP/1.1");
  75. client.println("Host: www.arduino.cc");
  76. client.println("User-Agent: arduino-ethernet");
  77. client.println("Connection: close");
  78. client.println();
  79. // note the time that the connection was made:
  80. lastConnectionTime = millis();
  81. }
  82. else {
  83. // if you couldn't make a connection:
  84. Serial.println("connection failed");
  85. }
  86. }