Overview

Playing with my Raspberry Pi and its GPIO over the weekend, it occurred to me I could control it in real time from a browser using Socket.IO and pi-gpio.

Source Code

Here is a link to the GitHub repository: https://github.com/fgheorghe/jsGPIO

Architecture

JSGPIOServer

The JSGPIOServer module creates an HTTP server that:

  • Handles Socket.IO events
  • Produces an example HTML interface
  • Interacts with the GPIO

The supported incoming event is write, taking in a data object made of a pin number and a numeric value. This event sends data to your GPIO using the pi-gpio module.

In return, it emits:

  • wrote, once writing has been successfully completed, passing in the input data
  • err, once an error occurs, passing in the input data along with a message property indicating the error reason

JSGPIOClient

The JSGPIOClient class creates a Socket.IO client that emits and receives events from the server.

Supported incoming events:

  • wrote, triggers a call to the configured wroteEventHandler function
  • err, triggers a call to the configured errEventHandler function

Public method:

  • write(data), sends a data object (pin number + value) to the server. The value is written to the given pin by the server.

Examples

  • Example client usage: index.html
  • Example server usage: example-server.js

More Info

Have a look at the README.md files for more details, or read the source code comments.