Im using esp32 and I have an ADC sensor called "Water Sensor" which detects the water level. Basically, I want to connect to a WIFI and then upload the readings to a MQTT Broker. But I found out, that if I connect to a WIFI, the read function doesnt work. Here is the code
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)
This is the error in 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
Is there another way to read the values ? Or anything else I could try to do ?
I've been trying to use time.sleep between connecting to the internet and printing out the values. I also tried to connect to internet, then disconnect and try to get a value but still nothing