socketio.emit not working with Raspberry Pi GPIO Event Callback
See original GitHub issueHi
I’m trying to add a flowmeter to my Raspberry Pi web app. The flowmeter is attached via GPIO and I’m listening for GPIO Events. When an event occurs the callback method gets called. Inside this method I like to send an update via websocket to the client. Unfortunately I don’t receive an event on client site. Any idea why I don’t get data on the client site?
def callback(channel):
socketio.emit('UPDATE_FLOWMETER', "DATA", namespace='/test')
def initFlowmeter(data):
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
try:
# SETUP GPIO PIN
GPIO.setup(data.gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# ADD GPIO CALLBACK
GPIO.add_event_detect(data.gpio, GPIO.RISING, callback=callback, bouncetime=20)
except Exception as e:
print e
Issue Analytics
- State:
- Created 7 years ago
- Comments:19 (7 by maintainers)
Top Results From Across the Web
socketio emit not working from gpio callback - Stack Overflow
The LEDs, buttons presses, and compressors all work. I just can't seem to get socketio to receive emit messages outside of an on...
Read more >sockio emit from GPIO.add_event_detect - Raspberry Pi Forums
I'm trying to call an Emit when a GPIO event detection fires. And I'm getting errors with not awaiting the socket.io.emit, when I...
Read more >Raspberry Pi Object Counting using Infrared sensor -
This post shows how we can use the Raspberry Pi as an object counter by using the Infrared (IR) sensor in counting objects...
Read more >Add a WebSocket Route to your Flask 2.x Application
With Flask-Socketio, all I needed to do was send a socketio.emit function, and I could do that from anywhere, but it seems to...
Read more >Raspberry Pi & Python Internet 'Thing' pt. 4 with Tony D ...
Live stream to http://twitch.tv/adafruit with the thrilling conclusion of the Pi internet 'thing' series! In this video we use SocketIO to ...
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
@sierrodc I have been working on a similar project which uses the GPIO library to detect when a button is pressed and then update the client side. After trying many different configurations, I ultimately found a solution by forcing the websocket to use gevent with monkey patching instead of eventlet. I’m not sure why this made a difference, but it worked for me. `from gevent import monkey monkey.patch_all()
app = Flask(name) socketio = SocketIO(app, async_mode=‘gevent’)`
I’m happy to share more of my code with you if that doesn’t help.
I’ve this kind of issue too.
If I put “socketio.emit” command elsewhere in my application, it works fine. Inside GPIO callback it doesn’t. Maybe because GPIO.add_event_detect is creating a background thread to control I/O and to invoke the callback and ( maybe) inside the thread there are some issues.
I’m using eventlet, with eventlet.moneky_patch() invocation just after imports.
Workaround: Basically do not use add_event_detect, but create yourself a thread (using eventlet.spawn) that check IO status: