【问题标题】:How to do a get request in Micropython on ESP32?如何在 ESP32 上的 Micropython 中执行获取请求?
【发布时间】:2021-03-29 04:07:05
【问题描述】:

我正在尝试在 ESP32 上使用 Micropython 执行简单的获取请求。不幸的是,我不断收到OSError: -202

这就是我目前所拥有的:

wlan.py

import network

wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True)       # activate the interface
wlan.connect('Schmittli', 'the key') # connect to an AP
wlan.ifconfig()         # get the interface's IP/netmask/gw/DNS addresses

这是 REPL 输出:

>>> import wlan
import wlan
I (19080) wifi:wifi driver task: 3ffd1058, prio:23, stack:3584, core=0
I (38466) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (38466) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (38506) wifi:wifi firmware version: 44aa95c
I (38506) wifi:config NVS flash: enabled
I (38506) wifi:config nano formating: disabled
I (38506) wifi:Init dynamic tx buffer num: 32
I (38506) wifi:Init data frame dynamic rx buffer num: 32
I (38516) wifi:Init management frame dynamic rx buffer num: 32
I (38516) wifi:Init management short buffer num: 32
I (38526) wifi:Init static rx buffer size: 1600
I (38526) wifi:Init static rx buffer num: 10
I (38536) wifi:Init dynamic rx buffer num: 32
W (38536) phy_init: failed to load RF calibration data (0x1102), falling back to full calibration
I (38676) phy: phy_version: 4180, cb3948e, Sep 12 2019, 16:39:13, 0, 2
I (38696) wifi:mode : sta (30:ae:a4:f6:0f:08)
I (38696) wifi: STA_START
>>> I (38816) wifi:new:<1,0>, old:<1,0>, ap:<255,255>, sta:<1,0>, prof:1
I (39666) wifi:state: init -> auth (b0)
I (39676) wifi:state: auth -> assoc (0)
I (39686) wifi:state: assoc -> run (10)
I (39706) wifi:connected with Schmittli, aid = 28, channel 1, BW20, bssid = a0:b5:49:00:c6:cd
I (39706) wifi:security type: 3, phy: bgn, rssi: -53
I (39706) wifi:pm start, type: 1

I (39716) network: CONNECTED
I (39776) wifi:AP's beacon interval = 102400 us, DTIM period = 3
I (42456) event: sta ip: 192.168.1.118, mask: 255.255.255.0, gw: 192.168.1.1
I (42456) network: GOT_IP


>>> import urequests
import urequests
I (72686) modsocket: Initializing
>>> response = urequests.get('http://jsonplaceholder.typicode.com/albums/1')
response = urequests.get('http://jsonplaceholder.typicode.com/albums/1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "urequests.py", line 108, in get
  File "urequests.py", line 53, in request
OSError: -202

我尝试了两个不同的 ESP32 板,所以我认为这不是硬件问题。是的,该 URL 在我的浏览器中运行良好。

更新

使用其 IP 地址请求相同的服务是可行的。所以这是一个DNS问题。我在同一个 WLAN 上有 10 多个不同的设备。一切正常。所以我真的认为这是一个Mircopython问题。

这是ESP32连接WLAN后的配置:

>>> wlan.ifconfig()
wlan.ifconfig()
('192.168.1.116', '255.255.255.0', '192.168.1.1', '42.2.18.11')

在我的所有其他设备中,DNS 服务器是“192.168.1.1”。我尝试在 ESP32 中手动设置 DNS 服务器,但不起作用:

>>> wlan.ifconfig('192.168.1.116', '255.255.255.0', '192.168.1.1', '192.168.1.1')
wlan.ifconfig('192.168.1.116', '255.255.255.0', '192.168.1.1', '192.168.1.1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function expected at most 2 arguments, got 5
>>> wlan.status()

有什么想法吗?

【问题讨论】:

  • 您尝试过使用stackoverflow.com/questions/64098376/… 的解决方案吗?
  • 我正在手动等待连接,输出状态为I (39716) network: CONNECTED,但我还是尝试了。没有变化,我仍然得到OSError: -202
  • 一切似乎仍然指向您的 WLAN 配置:您的 DHCP 服务器分配了一个不允许的不同 DNS 服务器 (42.2.18.11 = 42-2-18-011.static.netvigator.com)访问端口 53 以获取 DNS 服务。我建议您专注于解决 DHCP / 网络配置,以便您的 MicroPython Board 获得正确的 dns 服务器分配。
  • 尝试设置静态 IP 和 DNS 时使用的 synx 不正确,您缺少 ()。 ifconfig 需要一个元组,而不是 4 个参数。 (我同意错误可能更清楚)wlan.ifconfig(('192.168.1.116', '255.255.255.0', '192.168.1.1', '192.168.1.1'))` 应该做得更好

标签: python python-requests esp32 micropython


【解决方案1】:

您的 DNS 服务器似乎由于某种原因没有将 fqdn 解析为 IP 地址。

  • 运行print(wlan.ifconfig()) 以检查last nibble 中分配的DNS 服务器
  • 检查您的 DNS 服务器是否配置为过滤来自特定客户端/子网的查询
  • 将 FQDN 解析为不同客户端(您的 PC)上的 IP,并在 MicroPython 中使用该 IP 地址。如果这样可行,您的 DNS 就是问题
  • 您还可以使用以下方法测试您的 DNS:
import socket
print(socket.getaddrinfo('micropython.org', 80))
print(socket.getaddrinfo('jsonplaceholder.typicode.com', 80))
# sample return
[(2, 1, 0, 'micropython.org', ('176.58.119.26', 80))]
[(2, 1, 0, 'jsonplaceholder.typicode.com', ('104.27.188.192', 80))]

如果不是您的名称解析,您可能遇到了防火墙。

【讨论】:

  • 你是对的。 DNS 不工作。但为什么?我在同一个 WLAN 上有 iOS、OSX、RaperyPI、Android 和 Windows 设备。它们都可以正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-08
  • 1970-01-01
  • 2012-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
相关资源
最近更新 更多