A demo showing how the Raspberry Pi can send server-side events in response to input from the GPIO interface.
Python
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
LICENSE
README.md
server.py

README.md

rpi-mybutton-sse: Server-sent events from a Raspberry Pi GPIO-interfaced device

Overview

rpi-mybutton-sse is a simple experimental application that mashes up the introductory GPIO project described in The MagPi Issue 2 (June 2012), p.10 with a Python Flask server running on the Raspberry Pi that pushes server-sent events to web browsers across the network whenever the button in the project's circuit is pressed.

This YouTube video walks through the implementation and demonstrates the running application.

Requirements

Hardware

  • A circuit as described in the MagPi article, connected to the appropriate Raspberry Pi GPIO pins.

Software

Installation

Assuming Git is available on your Raspberry Pi, do the following:

$ git clone https://github.com/bradleypallen/rpi-mybutton-sse.git
$ cd rpi-mybutton-sse/
$ sudo easy_install flask
$ sudo easy_install RPi.GPIO

Execution

First, execute the following command in a shell on the Raspberry Pi:

$ sudo python server.py

Then, using a web browser that supports server-side events:

  • Go to http://<ip-address-of-your-Raspberry-Pi>:5000/.
  • Press the button on the circuit and watch data describing the event appear in a list on the page.

Running multiple browser sessions

The above will work for a single browser session, but because there is only a single synchronous worker serving up server-side events it won't support concurrent sessions from multiple browser. To get the demo to work for multiple browser sessions, you'll need to use asynchronous workers to service the requests.

For example, you can install greenlet, gevent and gnuicorn and do the following:

$ sudo gunicorn -w 4 -k gevent -b 0.0.0.0:5000 -t 99999 server:app

This works in my informal tests to around 10 concurrent browser sessions before the server starts ignore further sessions.

Issues

While gunicorn works on the Raspberry Pi for this application, it seems brittle; request handling errors are thrown when browser sessions are terminated, and better keep-alive and timeout settings need to be determined. Suggestions for improvement are welcome.

Acknowledgements

License

This software is provided under terms of an MIT License.