{"id":7915,"date":"2023-06-13T10:14:24","date_gmt":"2023-06-13T02:14:24","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=7915"},"modified":"2023-06-13T11:30:45","modified_gmt":"2023-06-13T03:30:45","slug":"wt32-eth01-ethernet","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=7915","title":{"rendered":"WT32-ETH01 - Ethernet"},"content":{"rendered":"<p>--<\/p>\n<ul>\n<li><a class=\"fancy-title\" href=\"https:\/\/community.home-assistant.io\/t\/esphome-ethernet-connectivity-feature-request\/110390\/64\" target=\"_blank\" rel=\"noopener\" data-ember-action=\"\" data-ember-action-60=\"60\"> ESPHome Ethernet connectivity feature request <\/a><\/li>\n<li><a href=\"https:\/\/github.com\/martin-ger\/esp32_nat_router\/files\/6025280\/WT32_ETH01_V2.schematic.pdf\" target=\"_blank\" rel=\"noopener\">WT32_ETH01_V2\u00a0schematic.pdf<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">ethernet:\r\ntype: LAN8720\r\nmdc_pin: GPIO23\r\nmdio_pin: GPIO18\r\nclk_mode: GPIO0_IN\r\nphy_addr: 1\r\npower_pin: GPIO16<\/pre>\n<p>&nbsp;<\/p>\n<p>--<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/espressif\/arduino-esp32\/blob\/master\/libraries\/WiFi\/examples\/ETH_LAN8720_internal_clock\/ETH_LAN8720_internal_clock.ino\" target=\"_blank\" rel=\"noopener\">ETH_LAN8720_internal_clock.ino<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/naisu_kun\/article\/details\/86710990\" target=\"_blank\" rel=\"noopener\">\u4f7f\u7528Arduino\u5f00\u53d1ESP32\uff0806\uff09\uff1aEthernet\u7684\u4f7f\u7528\uff08\u57fa\u4e8eLAN8720\uff09<\/a><\/li>\n<\/ul>\n<pre class=\"lang:arduino decode:true \">\/*\r\n    This sketch shows how to configure different external or internal clock sources for the Ethernet PHY\r\n*\/\r\n\r\n#include &lt;ETH.h&gt;\r\n\r\n\/* \r\n   * ETH_CLOCK_GPIO0_IN   - default: external clock from crystal oscillator\r\n   * ETH_CLOCK_GPIO0_OUT  - 50MHz clock from internal APLL output on GPIO0 - possibly an inverter is needed for LAN8720\r\n   * ETH_CLOCK_GPIO16_OUT - 50MHz clock from internal APLL output on GPIO16 - possibly an inverter is needed for LAN8720\r\n   * ETH_CLOCK_GPIO17_OUT - 50MHz clock from internal APLL inverted output on GPIO17 - tested with LAN8720\r\n*\/\r\n#ifdef ETH_CLK_MODE\r\n#undef ETH_CLK_MODE\r\n#endif\r\n#define ETH_CLK_MODE    ETH_CLOCK_GPIO0_IN\r\n\r\n\/\/ Pin# of the enable signal for the external crystal oscillator (-1 to disable for internal APLL source)\r\n#define ETH_POWER_PIN   16\r\n\r\n\/\/ Type of the Ethernet PHY (LAN8720 or TLK110)\r\n#define ETH_TYPE        ETH_PHY_LAN8720\r\n\r\n\/\/ I\u00b2C-address of Ethernet PHY (0 or 1 for LAN8720, 31 for TLK110)\r\n#define ETH_ADDR        1\r\n\r\n\/\/ Pin# of the I\u00b2C clock signal for the Ethernet PHY\r\n#define ETH_MDC_PIN     23\r\n\r\n\/\/ Pin# of the I\u00b2C IO signal for the Ethernet PHY\r\n#define ETH_MDIO_PIN    18\r\n\r\n\r\nstatic bool eth_connected = false;\r\n\r\nvoid WiFiEvent(WiFiEvent_t event) {\r\n  switch (event) {\r\n    case SYSTEM_EVENT_ETH_START:\r\n      Serial.println(\"ETH Started\");\r\n      \/\/set eth hostname here\r\n      ETH.setHostname(\"esp32-ethernet\");\r\n      break;\r\n    case SYSTEM_EVENT_ETH_CONNECTED:\r\n      Serial.println(\"ETH Connected\");\r\n      break;\r\n    case SYSTEM_EVENT_ETH_GOT_IP:\r\n      Serial.print(\"ETH MAC: \");\r\n      Serial.print(ETH.macAddress());\r\n      Serial.print(\", IPv4: \");\r\n      Serial.print(ETH.localIP());\r\n      if (ETH.fullDuplex()) {\r\n        Serial.print(\", FULL_DUPLEX\");\r\n      }\r\n      Serial.print(\", \");\r\n      Serial.print(ETH.linkSpeed());\r\n      Serial.println(\"Mbps\");\r\n      eth_connected = true;\r\n      break;\r\n    case SYSTEM_EVENT_ETH_DISCONNECTED:\r\n      Serial.println(\"ETH Disconnected\");\r\n      eth_connected = false;\r\n      break;\r\n    case SYSTEM_EVENT_ETH_STOP:\r\n      Serial.println(\"ETH Stopped\");\r\n      eth_connected = false;\r\n      break;\r\n    default:\r\n      break;\r\n  }\r\n}\r\n\r\nvoid testClient(const char * host, uint16_t port) {\r\n  Serial.print(\"\\nconnecting to \");\r\n  Serial.println(host);\r\n\r\n  WiFiClient client;\r\n  if (!client.connect(host, port)) {\r\n    Serial.println(\"connection failed\");\r\n    return;\r\n  }\r\n  client.printf(\"GET \/ HTTP\/1.1\\r\\nHost: %s\\r\\n\\r\\n\", host);\r\n  while (client.connected() &amp;&amp; !client.available());\r\n  while (client.available()) {\r\n    Serial.write(client.read());\r\n  }\r\n\r\n  Serial.println(\"closing connection\\n\");\r\n  client.stop();\r\n}\r\n\r\nvoid setup() {\r\n  Serial.begin(115200);\r\n  WiFi.onEvent(WiFiEvent);\r\n  ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE);\r\n}\r\n\r\n\r\nvoid loop() {\r\n  if (eth_connected) {\r\n    testClient(\"google.com\", 80);\r\n  }\r\n  delay(10000);\r\n}<\/pre>\n<p><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2021\/02\/1614258754.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-7919\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2021\/02\/1614258754-300x204.png\" alt=\"\" width=\"300\" height=\"204\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2021\/02\/1614258754-300x204.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2021\/02\/1614258754-768x523.png 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2021\/02\/1614258754-440x300.png 440w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2021\/02\/1614258754.png 804w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"7915\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;4,100&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- ESPHome Ethe...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"7915\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;4,100&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[272],"tags":[347],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7915"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7915"}],"version-history":[{"count":10,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7915\/revisions"}],"predecessor-version":[{"id":12331,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7915\/revisions\/12331"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}