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.

socketio.emit not working with Raspberry Pi GPIO Event Callback

See original GitHub issue

Hi

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:closed
  • Created 7 years ago
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
SkotBotCambocommented, Jun 6, 2017

@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.

1reaction
sierrodccommented, Mar 16, 2017

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:

def checkIO():
  while(True):
    currentIO = GPIO.input(pinNumber)
    if currentIO != previousIO:
      previousIO = currentIO
      if currentIO == 1: # RISING
        socketio.emit(...)
    eventlet.sleep(0.1)
Read more comments on GitHub >

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

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