Git Product home page Git Product logo

rpi-mybutton-sse's Introduction

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.

rpi-mybutton-sse's People

Contributors

bradleypallen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.