WebClientRepeating.ino 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. Repeating Web client
  3. This sketch connects to 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. https://www.arduino.cc/en/Tutorial/WebClientRepeating
  16. This code is in the public domain.
  17. */
  18. #include <SPI.h>
  19. #include <Ethernet.h>
  20. #include "IP6Address.h"
  21. // assign a MAC address for the Ethernet controller.
  22. // fill in your address here:
  23. byte mac[] = {
  24. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  25. };
  26. byte ip6_lla[] = {
  27. 0xfe, 0x80, 0x00, 0x00,
  28. 0x00, 0x00, 0x00, 0x00,
  29. 0x02, 0x00, 0xdc, 0xff,
  30. 0xfe, 0x57, 0x57, 0x61
  31. };
  32. byte ip6_gua[] = {
  33. 0x00, 0x00, 0x00, 0x00,
  34. 0x00, 0x00, 0x00, 0x00,
  35. 0x00, 0x00, 0x00, 0x00,
  36. 0x00, 0x00, 0x00, 0x00
  37. };
  38. byte ip6_sn6[] = {
  39. 0x00, 0x00, 0x00, 0x00,
  40. 0x00, 0x00, 0x00, 0x00,
  41. 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x00, 0x00, 0x00
  43. };
  44. byte ip6_gw6[] = {
  45. 0x00, 0x00, 0x00, 0x00,
  46. 0x00, 0x00, 0x00, 0x00,
  47. 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00
  49. };
  50. // https://developers.google.com/speed/public-dns/docs/using
  51. // 2001:4860:4860::8888
  52. // 2001:4860:4860::8844
  53. byte ip6_dns6[] = {
  54. 0x20, 0x01, 0x48, 0x60,
  55. 0x48, 0x60, 0x00, 0x00,
  56. 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x88, 0x88
  58. };
  59. // Set the static IP address to use if the DHCP fails to assign
  60. IP6Address ip(192, 168, 0, 177);
  61. IP6Address myDns(192, 168, 0, 1);
  62. IP6Address gateway(192, 168, 0, 1);
  63. IP6Address subnet(255, 255, 0, 0);
  64. IP6Address lla(ip6_lla, 16);
  65. IP6Address gua(ip6_gua, 16);
  66. IP6Address sn6(ip6_sn6, 16);
  67. IP6Address gw6(ip6_gw6, 16);
  68. IP6Address dns6(ip6_dns6, 16);
  69. // initialize the library instance:
  70. EthernetClient client;
  71. #if 1
  72. char server[] = "ipv6.google.com";
  73. #else
  74. char server[] = "www.arduino.cc"; // also change the Host line in httpRequest()
  75. #endif
  76. //IPAddress server(64,131,82,241);
  77. unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
  78. const unsigned long postingInterval = 30*1000; // delay between updates, in milliseconds
  79. void setup() {
  80. // You can use Ethernet.init(pin) to configure the CS pin
  81. //Ethernet.init(10); // Most Arduino shields
  82. //Ethernet.init(5); // MKR ETH Shield
  83. //Ethernet.init(0); // Teensy 2.0
  84. //Ethernet.init(20); // Teensy++ 2.0
  85. //Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet
  86. //Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet
  87. // start serial port:
  88. Serial.begin(9600);
  89. while (!Serial) {
  90. ; // wait for serial port to connect. Needed for native USB port only
  91. }
  92. // start the Ethernet connection:
  93. Serial.println("Initialize Ethernet with DHCP:");
  94. if (Ethernet.begin(mac) == 0) {
  95. Serial.println("Failed to configure Ethernet using DHCP");
  96. // Check for Ethernet hardware present
  97. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  98. Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  99. while (true) {
  100. delay(1); // do nothing, no point running without Ethernet hardware
  101. }
  102. }
  103. if (Ethernet.linkStatus() == LinkOFF) {
  104. Serial.println("Ethernet cable is not connected.");
  105. }
  106. // try to congifure using IP address instead of DHCP:
  107. Ethernet.begin(mac, ip, myDns, gateway, subnet, lla, gua, sn6, gw6);
  108. Serial.print("My IP address: ");
  109. Serial.println(Ethernet.localIP());
  110. } else {
  111. Serial.println(" DHCP assigned IP ");
  112. }
  113. Ethernet.setDnsServerIP(ip6_dns6);
  114. Serial.println("==================================================================");
  115. Serial.println("Network Information");
  116. Serial.println("==================================================================");
  117. Serial.print("IPv4 ADR: "); Serial.println(Ethernet.localIP());
  118. Serial.print("IPv6 LLA: "); Serial.println(Ethernet.linklocalAddress());
  119. Serial.print("IPv6 GUA: "); Serial.println(Ethernet.globalunicastAddress());
  120. Serial.print("IPv6 GAW: "); Serial.println(Ethernet.gateway6());
  121. Serial.print("IPv6 SUB: "); Serial.println(Ethernet.subnetmask6());
  122. Serial.print("IPv6 DNS: "); Serial.println(Ethernet.dnsServerIP());
  123. Serial.println("==================================================================");
  124. // give the Ethernet shield a second to initialize:
  125. delay(1000);
  126. httpRequest();
  127. }
  128. void loop() {
  129. // if there's incoming data from the net connection.
  130. // send it out the serial port. This is for debugging
  131. // purposes only:
  132. if (client.available()) {
  133. char c = client.read();
  134. Serial.write(c);
  135. }
  136. // if ten seconds have passed since your last connection,
  137. // then connect again and send data:
  138. if (millis() - lastConnectionTime > postingInterval) {
  139. httpRequest();
  140. }
  141. }
  142. // this method makes a HTTP connection to the server:
  143. void httpRequest() {
  144. // close any connection before send a new request.
  145. // This will free the socket on the Ethernet shield
  146. client.stop();
  147. // if there's a successful connection:
  148. if (client.connect(server, 80)) {
  149. Serial.println("connecting...");
  150. // send the HTTP GET request:
  151. #if 1
  152. client.println("GET /search?q=arduino HTTP/1.1");
  153. client.println("Host: ipv6.google.com");
  154. client.println("Connection: close");
  155. #else
  156. client.println("GET /latest.txt HTTP/1.1");
  157. client.println("Host: www.arduino.cc");
  158. client.println("User-Agent: arduino-ethernet");
  159. client.println("Connection: close");
  160. #endif
  161. client.println();
  162. // note the time that the connection was made:
  163. lastConnectionTime = millis();
  164. } else {
  165. // if you couldn't make a connection:
  166. Serial.println("connection failed");
  167. }
  168. }