Eth Module¶
| Since | Origin / Contributor | Maintainer | Source |
|---|---|---|---|
| 2019-06-25 | Arnim Laeuger | Arnim Laeuger | eth.c |
The eth module provides access to the ethernet PHY chip configuration.
Your board must contain one of the PHY chips that are supported by ESP-IDF:
- IP101
- LAN8720
- TLK110
eth.get_mac()¶
Get MAC address.
Syntax¶
local mac = eth.get_mac()
Parameters¶
None
Returns¶
MAC address as string "aa:bb:cc:dd:ee:dd".
eth.get_speed()¶
Get Ethernet connection speed.
Syntax¶
local speed = eth.get_speed()
Parameters¶
None
Returns¶
Connection speed in Mbit/s, or error if not connected.
- 10
- 100
eth.init()¶
Initialize the PHY chip and set up its tcpip adapter.
Syntax¶
eth.init(cfg)
Parameters¶
cfgtable containing configuration data. All entries are mandatory:addrPHY address, 0 to 31clock_modeexternal/internal clock mode selection, one ofeth.CLOCK_GPIO0_INeth.CLOCK_GPIO0_OUTeth.CLOCK_GPIO16_OUTeth.CLOCK_GPIO17_OUT
mdcMDC pin numbermdioMDIO pin numberphyPHY chip model, one ofeth.PHY_IP101eth.PHY_LAN8720eth.PHY_TLK110
powerpower enable pin, optional
Returns¶
nil
An error is thrown in case of invalid parameters or if the ethernet driver failed.
Example¶
-- Initialize ESP32-GATEWAY
eth.init({phy = eth.PHY_LAN8720,
addr = 0,
clock_mode = eth.CLOCK_GPIO17_OUT,
power = 5,
mdc = 23,
mdio = 18})
-- Initialize wESP32
eth.init({phy = eth.PHY_LAN8720,
addr = 0,
clock_mode = eth.CLOCK_GPIO0_IN,
mdc = 16,
mdio = 17})
eth.on()¶
Register or unregister callback functions for Ethernet events.
Syntax¶
eth.on(event, callback)
Parameters¶
eventEthernet event to register the callback for:- "start"
- "stop"
- "connected"
- "disconnected"
- "got_ip"
callbackcallbackfunction(event, info)to perform when event occurs, ornilto unregister the callback for the event. Theinfoargument given to the callback is a table containing additional information about the event.
Event information provided for each event is as follows:
start: no additional infostop: no additional infoconnected: no additional infodisconnected: no additional infogot_ip: IP network information:ip: the IP address assignednetmask: the IP netmaskgw: the gateway ("0.0.0.0" if no gateway)
Example¶
function ev(event, info)
print("event", event)
if event == "got_ip" then
print("ip:"..info.ip..", nm:"..info.netmask..", gw:"..info.gw)
elseif event == "connected" then
print("speed:", eth.get_speed())
print("mac:", eth.get_mac())
end
end
eth.on("connected", ev)
eth.on("disconnected", ev)
eth.on("start", ev)
eth.on("stop", ev)
eth.on("got_ip", ev)
eth.set_mac()¶
Set MAC address.
Syntax¶
eth.set_mac(mac)
Parameters¶
macMAC address as string "aa:bb:cc:dd:ee:ff"
Returns¶
nil
An error is thrown in case of invalid parameters or if the ethernet driver failed.
eth.set_ip()¶
Sets IP address, netmask, gateway, dns address of the ethernet.
Options set by this function are not saved to flash.
Syntax¶
eth.set_ip(cfg)
Parameters¶
cfgtable to hold configuration:ipdevice ip address.netmasknetwork netmask.gatewaygateway address.dnsname server address.
Returns¶
nil
Example¶
cfg={}
cfg.ip=192.168.0.10
cfg.netmask=255.255.255.0
cfg.gateway=192.168.0.1
cfg.dns=8.8.8.8
eth.set_ip(cfg)