ESPHome  2023.5.5
ethernet_component.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
6 
7 #ifdef USE_ESP32
8 
9 #include "esp_eth.h"
10 #include "esp_eth_mac.h"
11 #include "esp_netif.h"
12 
13 namespace esphome {
14 namespace ethernet {
15 
24 };
25 
26 struct ManualIP {
32 };
33 
35  STOPPED,
36  CONNECTING,
37  CONNECTED,
38 };
39 
40 class EthernetComponent : public Component {
41  public:
43  void setup() override;
44  void loop() override;
45  void dump_config() override;
46  float get_setup_priority() const override;
47  bool can_proceed() override;
48  void on_shutdown() override { powerdown(); }
49  bool is_connected();
50 
51  void set_phy_addr(uint8_t phy_addr);
52  void set_power_pin(int power_pin);
53  void set_mdc_pin(uint8_t mdc_pin);
54  void set_mdio_pin(uint8_t mdio_pin);
55  void set_type(EthernetType type);
56  void set_clk_mode(emac_rmii_clock_mode_t clk_mode, emac_rmii_clock_gpio_t clk_gpio);
57  void set_manual_ip(const ManualIP &manual_ip);
58 
60  std::string get_use_address() const;
61  void set_use_address(const std::string &use_address);
62  bool powerdown();
63 
64  protected:
65  static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
66  static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
67 
68  void start_connect_();
69  void dump_connect_params_();
70 
71  std::string use_address_;
72  uint8_t phy_addr_{0};
73  int power_pin_{-1};
74  uint8_t mdc_pin_{23};
75  uint8_t mdio_pin_{18};
77  emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
78  emac_rmii_clock_gpio_t clk_gpio_{EMAC_CLK_IN_GPIO};
79  optional<ManualIP> manual_ip_{};
80 
81  bool started_{false};
82  bool connected_{false};
84  uint32_t connect_begin_;
85  esp_netif_t *eth_netif_{nullptr};
86  esp_eth_handle_t eth_handle_;
87  esp_eth_phy_t *phy_{nullptr};
88 };
89 
90 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
92 extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
93 
94 } // namespace ethernet
95 } // namespace esphome
96 
97 #endif // USE_ESP32
void setup()
void loop()
network::IPAddress dns1
The first DNS server. 0.0.0.0 for default.
esp_eth_phy_t * esp_eth_phy_new_jl1101(const eth_phy_config_t *config)
std::string get_use_address()
Get the active network hostname.
Definition: util.cpp:44
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition: util.cpp:15
EthernetComponent * global_eth_component
network::IPAddress dns2
The second DNS server. 0.0.0.0 for default.
uint8_t type
Definition: a4988.cpp:4
network::IPAddress get_ip_address()
Definition: util.cpp:32