0

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

2
  • Could you remove the comment > character? If you want to display code, use the code ``` formatting only. Also, please fix your indentation. Commented Dec 10, 2023 at 21:13
  • This is why indentation is very import for python code. You had a timeout error, so to what level of indentation your time.sleep(0.1) belong to? Commented Dec 11, 2023 at 7:35

1 Answer 1

1

Espressif documentation:

The ESP32 integrates two 12-bit SAR (Successive Approximation Register) ADCs, supporting a total of 18 measurement channels (analog enabled pins).

The ADC driver API supports ADC1 (8 channels, attached to GPIOs 32 - 39), and ADC2 (10 channels, attached to GPIOs 0, 2, 4, 12 - 15 and 25 - 27). However, the usage of ADC2 has some restrictions for the application:

ADC2 is used by the Wi-Fi driver. Therefore the application can only use ADC2 when the Wi-Fi driver has not started.

Maybe this helps. Good luck.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.