Hi Peter, I have spent quite a few hours trying to make this work with your debounce code, specifically the "Test for the Pushbutton class (coroutines)". I am wanting to provide the ability to register button presses connected to input pins on the ESP32, and that code of yours works perfectly.
The problem arises when I try to combine the button press recognition with a web server that also provides inputs from the user. I am using some pretty basic socket code the run the web server, but I can't seem to combine these 2 things to work together. The web server just waits for input and the button presses don't register, so I presume I have it in the wrong place.
I have tried all kinds of different scenarios with the async coding, but I'll be danged if I can figure out what to do. I'm wondering if you have any suggestions?
I put the shortened version of the socket code below, and the button press code in a separate window below that. I know this should be relatively easy, but as a newbie I'm missing something, I'm sure.
Regards, AB
This is the web socket code:
Code: Select all
# define web server socket parameters
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setblocking(False)
s.bind(('',80))
s.settimeout(None)
# listen for response
while True:
# wait for browser action - either reload or form submission
conn,addr=s.accept()
request=conn.recv(1024)
request=str(request)
# check if it's an actual POST or just a refresh
detect_post = request.find('POST')
# actions
if detect_post > 0:
#do something
else:
# display web form
html_form = form_input(browser_tab_title)
conn.send(html_form)
conn.close()
html_form = ''
this is the button press code I am trying to combine it with (it's your code, expanded to accommodate 4 switches):
Code: Select all
# Quit test by setting var enable_switches
async def kill_switches():
enable_switches = True
while enable_switches:
# this does not affect debounce but must be there
await asyncio.sleep_ms(1)
# actions to perform (coroutine)
async def switch_activated(switch_id, action, delay):
global oled_on
if action == 'pressed' and debug: print('\n---\nSwitch ' + str(switch_id) + ' was pressed')
if action == 'released' and debug: print('Switch ' + str(switch_id) + ' was released')
if action == 'double-click' and debug: print('Switch ' + str(switch_id) + ' was registered')
if action == 'long-press' and debug: print('Switch ' + str(switch_id) + ' was registered')
if action == 'pressed' and oled_on == False and switch_id == 3: # turn on oled display
oled.invert(0) # White text on black background
oled.contrast(255) # Maximum contrast
oled.fill(0)
oled.text("WiFi connected!", 0, 0)
oled.show()
oled_on = True
if debug: print('OLED turned on by user.')
# turn off oled after delay
await asyncio.sleep_ms(10000)
oled.invert(0) # White text on black background
oled.contrast(0) # Minimum contrast
oled.fill(0)
oled.show()
oled_on = False
if debug: print('OLED timed out.')
elif action == 'pressed' and switch_id == 3: # turn off oled display
oled.invert(0) # White text on black background
oled.contrast(0) # Minimum contrast
oled.fill(0)
oled.show()
oled_on = False
if debug: print('OLED turned off by user.')
# this delay has nothing to do with debounce
# await asyncio.sleep_ms(delay)
#if action == 'pressed': print ('pause timer done\n---\n')
# Test for the Pushbutton class (coroutines)
# Pass True to test suppress
def load_switch_async(suppress1, dc1, lp1, suppress2, dc2, lp2,suppress3, dc3, lp3,suppress4, dc4, lp4):
print('Switches are enabled: scheduling coroutines.')
print(helptext)
# CONFIGURE SWITCH 1
pin1 = Pin(36, Pin.IN, None) # pin 36 does not have internal resistor
sw1 = Pushbutton(pin1, suppress1)
# CONFIGURE SWITCH 2
pin2 = Pin(25, Pin.IN, Pin.PULL_DOWN) # pin 25 has internal resistor
sw2 = Pushbutton(pin2, suppress2)
# CONFIGURE SWITCH 3
pin3 = Pin(26, Pin.IN, Pin.PULL_DOWN) # pin 26 has internal resistor
sw3 = Pushbutton(pin3, suppress3)
# CONFIGURE SWITCH 4
pin4 = Pin(39, Pin.IN, None) # pin 39 does not have internal resistor
sw4 = Pushbutton(pin4, suppress4)
delay = 10 #msec
# Switch 1 actions
sw1.press_func(switch_activated, (1, 'pressed', delay))
sw1.release_func(switch_activated, (1, 'released', delay))
if lp1:
sw1.long_func(switch_activated, (1, 'long-press', delay))
if debug: print('Long Press on Switch 1 is enabled')
if dc1:
sw1.double_func(switch_activated, (1, 'double-click', delay))
if debug: print('Double Click on Switch 1 is enabled')
# Switch 2 actions
sw2.press_func(switch_activated, (2, 'pressed', delay))
sw2.release_func(switch_activated, (2, 'released', delay))
if lp2:
sw2.long_func(switch_activated, (2, 'long-press', delay))
if debug: print('Long Press on Switch 2 is enabled')
if dc2:
sw2.double_func(switch_activated, (2, 'double-click', delay))
if debug: print('Double Click on Switch 2 is enabled')
# Switch 3 actions
sw3.press_func(switch_activated, (3, 'pressed', delay))
sw3.release_func(switch_activated, (3, 'released', delay))
if lp3:
sw3.long_func(switch_activated, (3, 'long-press', delay))
if debug: print('Long Press on Switch 3 is enabled')
if dc3:
sw3.double_func(switch_activated, (3, 'double-click', delay))
if debug: print('Double Click on Switch 3 is enabled')
# Switch 4 actions
sw4.press_func(switch_activated, (4, 'pressed', delay))
sw4.release_func(switch_activated, (4, 'released', delay))
if lp4:
sw4.long_func(switch_activated, (4, 'long-press', delay))
if debug: print('Long Press on Switch 4 is enabled')
if dc4:
sw4.double_func(switch_activated, (4, 'double-click', delay))
if debug: print('Double Click on Switch 4 is enabled')
# maintain loop
loop = asyncio.get_event_loop()
#loop.run_forever()
loop.run_until_complete(kill_switches())
# END MODIFIED TEST
# ======================================
# start async switch routine
load_switch_async(suppress1=False, dc1=False, lp1=False, suppress2=False, dc2=False, lp2=False,suppress3=False, dc3=False, lp3=False,suppress4=False, dc4=False, lp4=True)