question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to emit from a GPIO.add_event_detect

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
miguelgrinbergcommented, Oct 23, 2019

Is the GPIO library compatible with asyncio? You can’t use code that was written to be synchronous with asyncio, unfortunately.

0reactions
Souldatcommented, Oct 24, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found