ETH_LAN8720.ino 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. This sketch shows the Ethernet event usage
  3. */
  4. #include <ETH.h>
  5. static bool eth_connected = false;
  6. void WiFiEvent(WiFiEvent_t event)
  7. {
  8. switch (event) {
  9. case ARDUINO_EVENT_ETH_START:
  10. Serial.println("ETH Started");
  11. //set eth hostname here
  12. ETH.setHostname("esp32-ethernet");
  13. break;
  14. case ARDUINO_EVENT_ETH_CONNECTED:
  15. Serial.println("ETH Connected");
  16. break;
  17. case ARDUINO_EVENT_ETH_GOT_IP:
  18. Serial.print("ETH MAC: ");
  19. Serial.print(ETH.macAddress());
  20. Serial.print(", IPv4: ");
  21. Serial.print(ETH.localIP());
  22. if (ETH.fullDuplex()) {
  23. Serial.print(", FULL_DUPLEX");
  24. }
  25. Serial.print(", ");
  26. Serial.print(ETH.linkSpeed());
  27. Serial.println("Mbps");
  28. eth_connected = true;
  29. break;
  30. case ARDUINO_EVENT_ETH_DISCONNECTED:
  31. Serial.println("ETH Disconnected");
  32. eth_connected = false;
  33. break;
  34. case ARDUINO_EVENT_ETH_STOP:
  35. Serial.println("ETH Stopped");
  36. eth_connected = false;
  37. break;
  38. default:
  39. break;
  40. }
  41. }
  42. void testClient(const char * host, uint16_t port)
  43. {
  44. Serial.print("\nconnecting to ");
  45. Serial.println(host);
  46. WiFiClient client;
  47. if (!client.connect(host, port)) {
  48. Serial.println("connection failed");
  49. return;
  50. }
  51. client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
  52. while (client.connected() && !client.available());
  53. while (client.available()) {
  54. Serial.write(client.read());
  55. }
  56. Serial.println("closing connection\n");
  57. client.stop();
  58. }
  59. void setup()
  60. {
  61. Serial.begin(115200);
  62. WiFi.onEvent(WiFiEvent);
  63. ETH.begin();
  64. }
  65. void loop()
  66. {
  67. if (eth_connected) {
  68. testClient("google.com", 80);
  69. }
  70. delay(10000);
  71. }