Unable to emit from a GPIO.add_event_detect
See original GitHub issueI’m trying to send some data to a client when an LED blinks thats connected to an IO on a raspberry pi.
I know this is a threading issue I’m just not sure how to handle it. the emit needs to be awaited which means the callback function that the GPIO.add_event_detect calls needs to be async, but then that callback function needs to be awaited when called from the GPIO event. I’m not sure how to handle this. The below produces “RuntimeWarning: coroutine ‘my_callback_one’ was never awiated”
sio = socketio.AsyncServer(async_mode='sanic')
app = Sanic()
sio.attach(app)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
async def my_callback_one(channel):
await sio.emit('my_response', {'data': 'test'})
print('========button press LED lighting detected========')
GPIO.add_event_detect(18, GPIO.RISING, callback=my_callback_one)
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
pyQT5 signal doesn't work with GPIO - Raspberry Pi Forums
I want to emit a signal when a physical button is pressed, which is connected to GPIO 17. I have tested the signal...
Read more >running loop in call back function blocks other callbacks
Callbacks are emitted by one thread. Each callback will therefore run to completion before the next is called. This is true for RPi.GPIO...
Read more >RPi.GPIO add_event_detect are sometimes ignored
I already had a Pi with some strange IO behaviour. It was from the early batch of the Pi4. Keep in mind that...
Read more >Interrupt-driven I/O on Raspberry Pi 3 with LEDs and ... - Medium
The basic idea is instead of reading the current input state using GPIO.input(), add a callback function using GPIO.add_event_detect().
Read more >Using a Break Beam Sensor with Python and Raspberry Pi
The emitter: emits a beam of infrared light. ... but required to make it work as a callback for the GPIO module's add_event_detect...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Is the GPIO library compatible with asyncio? You can’t use code that was written to be synchronous with asyncio, unfortunately.
I think the best / easiest solution for me right now is to just write my gpio results to a file and read that file from my other thread on a loop and send that up to the client.