PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

138 lines
4.2KB

  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. by David A. Mellis
  9. modified 9 Apr 2012
  10. by Tom Igoe, based on work by Adrian McEwen
  11. */
  12. #include <SPI.h>
  13. #include <Ethernet.h>
  14. // Enter a MAC address for your controller below.
  15. // Newer Ethernet shields have a MAC address printed on a sticker on the shield
  16. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  17. // if you don't want to use DNS (and reduce your sketch size)
  18. // use the numeric IP instead of the name for the server:
  19. //IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
  20. char server[] = "www.google.com"; // name address for Google (using DNS)
  21. // Set the static IP address to use if the DHCP fails to assign
  22. IPAddress ip(192, 168, 0, 177);
  23. IPAddress myDns(192, 168, 0, 1);
  24. // Initialize the Ethernet client library
  25. // with the IP address and port of the server
  26. // that you want to connect to (port 80 is default for HTTP):
  27. EthernetClient client;
  28. // Variables to measure the speed
  29. unsigned long beginMicros, endMicros;
  30. unsigned long byteCount = 0;
  31. bool printWebData = true; // set to false for better speed measurement
  32. void setup() {
  33. // You can use Ethernet.init(pin) to configure the CS pin
  34. //Ethernet.init(10); // Most Arduino shields
  35. //Ethernet.init(5); // MKR ETH shield
  36. //Ethernet.init(0); // Teensy 2.0
  37. //Ethernet.init(20); // Teensy++ 2.0
  38. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  39. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  40. // Open serial communications and wait for port to open:
  41. Serial.begin(9600);
  42. while (!Serial) {
  43. ; // wait for serial port to connect. Needed for native USB port only
  44. }
  45. // start the Ethernet connection:
  46. Serial.println("Initialize Ethernet with DHCP:");
  47. if (Ethernet.begin(mac) == 0) {
  48. Serial.println("Failed to configure Ethernet using DHCP");
  49. // Check for Ethernet hardware present
  50. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  51. Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  52. while (true) {
  53. delay(1); // do nothing, no point running without Ethernet hardware
  54. }
  55. }
  56. if (Ethernet.linkStatus() == LinkOFF) {
  57. Serial.println("Ethernet cable is not connected.");
  58. }
  59. // try to congifure using IP address instead of DHCP:
  60. Ethernet.begin(mac, ip, myDns);
  61. } else {
  62. Serial.print(" DHCP assigned IP ");
  63. Serial.println(Ethernet.localIP());
  64. }
  65. // give the Ethernet shield a second to initialize:
  66. delay(1000);
  67. Serial.print("connecting to ");
  68. Serial.print(server);
  69. Serial.println("...");
  70. // if you get a connection, report back via serial:
  71. if (client.connect(server, 80)) {
  72. Serial.print("connected to ");
  73. Serial.println(client.remoteIP());
  74. // Make a HTTP request:
  75. client.println("GET /search?q=arduino HTTP/1.1");
  76. client.println("Host: www.google.com");
  77. client.println("Connection: close");
  78. client.println();
  79. } else {
  80. // if you didn't get a connection to the server:
  81. Serial.println("connection failed");
  82. }
  83. beginMicros = micros();
  84. }
  85. void loop() {
  86. // if there are incoming bytes available
  87. // from the server, read them and print them:
  88. int len = client.available();
  89. if (len > 0) {
  90. byte buffer[80];
  91. if (len > 80) len = 80;
  92. client.read(buffer, len);
  93. if (printWebData) {
  94. Serial.write(buffer, len); // show in the serial monitor (slows some boards)
  95. }
  96. byteCount = byteCount + len;
  97. }
  98. // if the server's disconnected, stop the client:
  99. if (!client.connected()) {
  100. endMicros = micros();
  101. Serial.println();
  102. Serial.println("disconnecting.");
  103. client.stop();
  104. Serial.print("Received ");
  105. Serial.print(byteCount);
  106. Serial.print(" bytes in ");
  107. float seconds = (float)(endMicros - beginMicros) / 1000000.0;
  108. Serial.print(seconds, 4);
  109. float rate = (float)byteCount / seconds / 1000.0;
  110. Serial.print(", rate = ");
  111. Serial.print(rate);
  112. Serial.print(" kbytes/second");
  113. Serial.println();
  114. // do nothing forevermore:
  115. while (true) {
  116. delay(1);
  117. }
  118. }
  119. }