Can't get web page to run asynchronously on ESP32 with Micropython #18795
Replies: 3 comments 1 reply
-
|
Please see this post and read the paragraph on code formatting. Follow the guidelines to preserve indenting. There is an approved asynchronous web framework: microdot. I've not studied your code in detail, but this function (assuming I've guessed your indentation correctly) will stall the scheduler and prevent other tasks from running: async def run_relay_timer():
while True:
print('run_relay_timer()') # Debugging output |
Beta Was this translation helpful? Give feedback.
-
|
I am still new to Python and Micropython programming. Why would this function stall the scheduler? |
Beta Was this translation helpful? Give feedback.
-
|
I guess it's something else. Please fix the formatting. Here the trick with changing |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to run a web page asynchronously on an ESP32 using MicroPython. Along with the webpage, which will allow me to turn a relay on, on demand, I want to run a timed function which turns the relay on, every morning at 08:00.
I am using an ESP32 Dev board with CP2102 USB chip, DS1307 RTC and 5V relay.
When I run the code in the REPL, I can see the "run_relay_timer(), but none of the other debugging output
The web server debugging output is not showing either, nor can I connect to http://192.168.10.130/ (the DHCP IP assigned to this ESP32)
Looking at the REPL in Thonny, the run_rrelay_timer() runs, but not the run_web_page()
`import asyncio
import esp
esp.osdebug(None)
import time
import urtc
from machine import I2C, Pin
set_hour = 15
set_time = 30
relay = Pin(4, Pin.OUT)
led = Pin(2, Pin.OUT)
relay.value(0) # Start OFF (active low)
def web_page():
if relay.value() == 0:
print("Relay value: ")
print(relay.value)
relay_state = ''
else:
relay_state = 'checked'
html = """<style>
body{font-family:Arial; text-align: center; margin: 0px auto; padding-top:30px;}
.switch{position:relative;display:inline-block;width:120px;height:68px}.switch input{display:none}
.slider{position:absolute;top:0;left:0;right:0;bottom:0;background-color:#ccc;border-radius:34px}
.slider:before{position:absolute;content:"";height:52px;width:52px;left:8px;bottom:8px;background-color:#fff;-webkit-transition:.4s;transition:.4s;border-radius:68px}
input:checked+.slider{background-color:#2196F3}
input:checked+.slider:before{-webkit-transform:translateX(52px);-ms-transform:translateX(52px);transform:translateX(52px)}
</style><script>function toggleCheckbox(element) { var xhr = new XMLHttpRequest(); if(element.checked){ xhr.open("GET", "/?relay=on", true); }
else { xhr.open("GET", "/?relay=off", true); } xhr.send(); }</script>
Sprinkler control
""" % (relay_state) return htmls = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
Define coroutine function
async def run_relay_timer():
while True:
print('run_relay_timer()') # Debugging output
import time
import urtc
from machine import I2C, Pin
async def run_web_page():
while True:
print('run_web_page()') # Debugging output
try:
if gc.mem_free() < 102000:
gc.collect()
conn, addr = s.accept()
conn.settimeout(3.0)
print('Got a connection from %s' % str(addr)) # Debugging output
request = conn.recv(1024)
conn.settimeout(None)
request = str(request)
Define the main function to run the event loop
async def main():
# Create tasks for blinking two LEDs concurrently
asyncio.create_task(run_relay_timer())
asyncio.create_task(run_web_page())
Create and run the event loop
loop = asyncio.get_event_loop()
loop.create_task(main()) # Create a task to run the main function
loop.run_forever() # Run the event loop indefinitely
`
Beta Was this translation helpful? Give feedback.
All reactions