Run two funktions at the same Time on ESP32

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
BenjaminBaehrle
Posts: 1
Joined: Thu Aug 13, 2020 9:12 am

Run two funktions at the same Time on ESP32

Post by BenjaminBaehrle » Thu Aug 13, 2020 9:30 am

Hi,
im trying to start two diffrent functions at the same time.
The first one is a webserver, the second one is a script to check the temperature of an DTH11.

Webserver:
def webserverThread():
print('Webserver_Started')
while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
print('Content = %s' % str(request))
sensor_readings = read_sensor()
print(sensor_readings)
response = web_page()
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(response)
conn.close()

def tempCheckerThread():
print('TempThread_Started')
while True:
if float(temp) > 30.0:
print('Try to send Mail')
send_mail_warn()

now i want to run both to the same time. I've tested _thread but when the Webserver function is running the thempcheck starts only after reloading the website..

Any Ideas for a newbie?

THX

Benny

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Run two funktions at the same Time on ESP32

Post by pythoncoder » Fri Aug 14, 2020 10:30 am

I recommend uasyncio rather than threading. It's more efficient. Threading has the potential for writing code with bugs which are extremely hard to find.

See this tutorial.
Peter Hinch
Index to my micropython libraries.

User avatar
rcolistete
Posts: 352
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Re: Run two funktions at the same Time on ESP32

Post by rcolistete » Fri Aug 14, 2020 3:01 pm

Another hardware, K210 boards with Lobo firmware has the option :
Running two independent Micropython instances on two K210 cores is supported.
Rich set of functions for data exchange between instances is provided.
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

Post Reply