1+ #include < chrono>
2+ #include < thread>
3+
4+ #include < sdkconfig.h>
5+
6+ #include " esp32-ethernet-kit.hpp"
7+ #include " logger.hpp"
8+
9+ #if CONFIG_EXAMPLE_ETH_DHCP_SERVER
10+ #include < lwip/ip4_addr.h>
11+ #endif
12+
13+ using namespace std ::chrono_literals;
14+ using DhcpMode = espp::Esp32EthernetKit::DhcpMode;
15+ using EthConfig = espp::Esp32EthernetKit::EthernetConfig;
16+
17+ extern " C" void app_main (void ) {
18+ espp::Logger logger ({.tag = " EthKitExample" , .level = espp::Logger::Verbosity::INFO });
19+ logger.info (" ESP32-Ethernet-Kit A V1.2 example starting" );
20+
21+ // Example 1: get the singleton board instance
22+ // ! [esp32 ethernet kit get instance]
23+ auto &board = espp::Esp32EthernetKit::get ();
24+ // ! [esp32 ethernet kit get instance]
25+
26+ #if CONFIG_EXAMPLE_ETH_DHCP_SERVER
27+ // Example 2: init as DHCP server -- ESP32 assigns IPs to connected hosts.
28+ // ServerConfig::ip_info zero -> default 192.168.4.1/24. Supply Kconfig
29+ // values (EXAMPLE_ETH_SERVER_IP / _NETMASK / _GW) for a custom static IP.
30+ // ! [esp32 ethernet kit dhcp server]
31+ espp::Esp32EthernetKit::ServerConfig srv_cfg;
32+ ip4addr_aton (CONFIG_EXAMPLE_ETH_SERVER_IP , reinterpret_cast <ip4_addr_t *>(&srv_cfg.ip_info .ip ));
33+ ip4addr_aton (CONFIG_EXAMPLE_ETH_SERVER_NETMASK ,
34+ reinterpret_cast <ip4_addr_t *>(&srv_cfg.ip_info .netmask ));
35+ ip4addr_aton (CONFIG_EXAMPLE_ETH_SERVER_GW , reinterpret_cast <ip4_addr_t *>(&srv_cfg.ip_info .gw ));
36+ srv_cfg.on_client_assigned = [&](esp_ip4_addr_t ip, std::array<uint8_t , 6 > mac) {
37+ logger.info (" Client assigned {}.{}.{}.{} (mac {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x})" ,
38+ esp_ip4_addr1_16 (&ip), esp_ip4_addr2_16 (&ip), esp_ip4_addr3_16 (&ip),
39+ esp_ip4_addr4_16 (&ip), mac[0 ], mac[1 ], mac[2 ], mac[3 ], mac[4 ], mac[5 ]);
40+ };
41+ bool eth_ok = board.initialize_ethernet ({
42+ .mode = DhcpMode::SERVER ,
43+ .server_config = srv_cfg,
44+ .on_link_up = [&]() { logger.info (" Ethernet link up" ); },
45+ .on_link_down = [&]() { logger.warn (" Ethernet link down" ); },
46+ .on_got_ip =
47+ [&](esp_ip4_addr_t ip) {
48+ logger.info (" DHCP server up at {}.{}.{}.{}" , esp_ip4_addr1_16 (&ip),
49+ esp_ip4_addr2_16 (&ip), esp_ip4_addr3_16 (&ip), esp_ip4_addr4_16 (&ip));
50+ },
51+ .on_lost_ip = [&]() { logger.warn (" Ethernet lost IP" ); },
52+ });
53+ // ! [esp32 ethernet kit dhcp server]
54+ #else
55+ // Example 3: init as DHCP client -- ESP32 acquires an IP from the network.
56+ // ! [esp32 ethernet kit dhcp client]
57+ bool eth_ok = board.initialize_ethernet ({
58+ .mode = DhcpMode::CLIENT ,
59+ .on_link_up = [&]() { logger.info (" Ethernet link up" ); },
60+ .on_link_down = [&]() { logger.warn (" Ethernet link down" ); },
61+ .on_got_ip =
62+ [&](esp_ip4_addr_t ip) {
63+ logger.info (" DHCP lease acquired: {}.{}.{}.{}" , esp_ip4_addr1_16 (&ip),
64+ esp_ip4_addr2_16 (&ip), esp_ip4_addr3_16 (&ip), esp_ip4_addr4_16 (&ip));
65+ },
66+ .on_lost_ip = [&]() { logger.warn (" Ethernet lost IP" ); },
67+ });
68+ // ! [esp32 ethernet kit dhcp client]
69+ #endif
70+
71+ if (!eth_ok) {
72+ logger.error (" Ethernet initialization failed" );
73+ return ;
74+ }
75+
76+ // SERVER mode: connected as soon as the cable is plugged in (static IP).
77+ // CLIENT mode: connected once the DHCP lease is granted (up to ~30 s).
78+ logger.info (" Waiting for Ethernet..." );
79+ for (int i = 0 ; i < 60 && !board.is_ethernet_connected (); ++i) {
80+ std::this_thread::sleep_for (500ms);
81+ }
82+
83+ if (board.is_ethernet_connected ()) {
84+ auto ip = board.ethernet_ip ();
85+ logger.info (" Connected. IP: {}.{}.{}.{}" , esp_ip4_addr1_16 (&ip), esp_ip4_addr2_16 (&ip),
86+ esp_ip4_addr3_16 (&ip), esp_ip4_addr4_16 (&ip));
87+ } else {
88+ logger.warn (" Ethernet not ready within 30 s" );
89+ }
90+
91+ while (true ) {
92+ std::this_thread::sleep_for (5s);
93+ if (board.is_ethernet_connected ()) {
94+ auto ip = board.ethernet_ip ();
95+ logger.info (" Ethernet up, IP: {}.{}.{}.{}" , esp_ip4_addr1_16 (&ip), esp_ip4_addr2_16 (&ip),
96+ esp_ip4_addr3_16 (&ip), esp_ip4_addr4_16 (&ip));
97+ } else {
98+ logger.warn (" Ethernet not connected" );
99+ }
100+ }
101+ }
0 commit comments