[英]Get own IP Address of Esp32 Device
我有一个 Esp32 函数作为 TCP 套接字接收器/服务器。
可能无法始终在设备上设置静态 IP,所以我想找出答案:
如何使用 micropython 从 Esp32 获取本地 IP 地址。
或者,我想应该可以从客户端扫描设备吗? 客户端在 Python 2.7 上运行......所以也许你可以给我一些关于如何解决这个问题的提示。
来自 PybD V 1.13 的 Micropython 文档,但对于大多数(如果不是全部)端口来说都是通用的。
WLAN.ifconfig([(ip, subnet, gateway, dns)])
Get/set IP-level network interface parameters: IP address, subnet mask,
gateway and DNS server.
When called with no arguments, this method returns a 4-tuple with the above
information.
To set the above values, pass a 4-tuple with the required information, for example:
nic.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
正如其他人建议的那样,首先检查文档总是一个好主意。
可能使用主机名也有助于您的客户端连接到设备。 因此,您可以从客户端连接到“mydevice.local”,而不是硬编码“10.1.2.3”
像下面这样的东西应该可以大大缓解这种情况。
import network
from config import homenet, CLIENT_ID
wlan = network.WLAN(network.STA_IF)
if not wlan.active():
wlan.active(True)
if not wlan.isconnected():
wlan.config(dhcp_hostname=CLIENT_ID)
log.info("Activating Wlan {0}".format(homenet['SSID']))
wlan.connect(homenet['SSID'], homenet['password'])
配置文件
from ubinascii import hexlify
# Base SSID to connect to
homenet = {'SSID': 'ThisIsHome', 'password': 'MicroPython'}
CLIENT_ID = b'mydevice' # + hexlify(unique_id())[-6:]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.