WebRTC Lights from Neil Stratford

WebRTC LightsBy Neil Stratford (@neilstratford)

For the WebRTC themed London TADHack-mini I wanted to build something that demonstrated WebRTC in a different way, that wasn’t traditional audio or video communication.

The hack, WebRTC Lights, has its roots in the festive season, the idea being to replace boring old strings of blinking lights with modern LED strips, where each light is individually addressable and can produce any colour or intensity you can think of. They come in handy lengths of 150 lights on a strip and can be daisy-chained as long as you like, so long as you can power them. The challenge is how to control them – which it turns out WebRTC is great for.

The Data Channel is a powerful part of the specification that has a lot of interesting applications. It provides a way for browsers to communicate directly, peer to peer, with low latency, through NATs (Network Address Translation) and the same security we benefit from for audio and video. In many respects it’s the perfect protocol for connecting devices on the Internet of Things. You just have to replace browsers with devices.

The lights themselves are controlled with a one-wire protocol that has fairly strict timing requirements. Within the bounds of a hackathon the easiest way to build the system was to use a dedicated microcontroller to generate this signal, under the command of a Raspberry Pi running a WebRTC protocol gateway. Ultimately the full stack would be optimised and run on the microcontroller, but that’s a level of pain for next year.

The protocol running over the data channel is a simple string of RGB (Red Green Blue) values, sent to a plugin running in the versatile Janus gateway on the Pi. These RGB values are then relayed to the microcontroller and sent to the lights.

Once this RGB protocol is exposed to javascript the fun really starts. The Web Audio API provides a rich set of audio analysis capabilities and when combined with GetUserMedia it’s possible to take the microphone input, perform an FFT (Fast Fourier Transform) and render that on the lights. All in javascript, with low latency.

GetUserMedia also provides access to the video camera, but rather than stream the video, in this case we can read individual scan lines and render them to the lights. A possible use of this is for ambient lighting that responds to room conditions, or a fun remote light viewing experience. (Or building a really-big television.)

To conclude, WebRTC is about a lot more than just audio and video. The data channel is equally exciting and it’s going to be interesting to see what the intersection of WebRTC and the Internet of Things makes possible.

(Editors note: Congratulations to Neil on joining Nexmo)