我使用 esp32,并且有一个名为“水传感器”的 ADC 传感器,用于检测水位。基本上,我想连接到 WIFI,然后将读数上传到 MQTT 代理。但我发现,如果我连接到WIFI,读取功能就不起作用了。这是代码
from machine import ADC, Pin import network import time adc_pin = 4 adc = ADC(Pin(adc_pin)) Wi-Fi credentials WIFI_SSID = "XXXXX" WIFI_PASSWORD = "XXXXX" def read_water_sensor(): #Reading the values value = adc.read() #Here is the error return value def connect_wifi(): #Connecting def sta_if = network.WLAN(network.STA_IF) if not sta_if.isconnected(): print("Connecting to Wi-Fi...") sta_if.active(True) sta_if.connect(WIFI_SSID, WIFI_PASSWORD) while not sta_if.isconnected(): pass print("Connected to Wi-Fi") connect_wifi() #connect to the internet while True: water_value = read_water_sensor() print("Water sensor value:", water_value) time.sleep(0.1)
这是 Thonny micropython 中的错误:
Traceback (most recent call last):
File "<stdin>", line 29, in <module>
File "<stdin>", line 14, in read_water_sensor
OSError: [Errno 116] ETIMEDOUT: ESP_ERR_TIMEOUT
还有其他读取值的方法吗?或者还有什么我可以尝试做的吗?
我一直在尝试在连接到互联网和打印值之间使用 time.sleep 。我也尝试连接到互联网,然后断开连接并尝试获取值,但仍然没有
乐鑫文档:
ESP32 集成了两个 12 位 SAR(逐次逼近寄存器)ADC,总共支持 18 个测量通道(模拟使能引脚)。
ADC 驱动程序 API 支持 ADC1(8 个通道,连接到 GPIO 32 - 39)和 ADC2(10 个通道,连接到 GPIO 0、2、4、12 - 15 和 25 - 27)。然而,ADC2 的使用对应用有一些限制:
ADC2 is used by the Wi-Fi driver. Therefore the application can only use ADC2 when the Wi-Fi driver has not started.
也许这有帮助。 祝你好运。