How to Annoy Your Coworkers for $42 using ClojureScript and Rust

Control Spotify music, Hue lights and everything else at your office with voice commands and Hipchat messages

The Wit.ai Team
Wit.ai
5 min readSep 12, 2014

--

In our last few Friday Hacks we’ve been on a mission to create a real life version of our interactive home automation demo for our office. Project Cambridge has been a great way for us to play with some new technologies and dogfood our own product at the same time. Today we’re releasing the two major components for Project Cambridge: Witd and Cherry.

Witd: Wit SDK for Raspberry Pi

One of Wit’s coolest features is that the API accepts text and voice commands. We try hard to make it easy for developers to record and send audio to the Wit API with tools like our iOS and Android SDKs. So when we turned our attention to the Raspberry Pi we knew we had to give it a super simple way to record audio. Witd is our solution for handling audio recording and streaming it to the Wit API.

Raspberry Pi hanging out during some new plugin development

The initial goal of Witd was to have a small, stand-alone, open-source daemon that would handle both the voice recording and the queries to Wit.ai. We originally implemented one in Python, because the language is easy, widely used, flexible and compatible with many different platforms. However, as we moved away from our powerful Intel Core i7 laptops to the world of embedded devices, we realized that spawning a Python interpreter was less than ideal for performance, and that a lightweight, self-contained executable would be much nicer. C or C++ seemed like the most reasonable choice but it was also a terribly unsexy one. Instead, we decided to a give a chance to the promising new star in the world of systems programming languages: Rust.

Since the language is designed to help you write safe code, the compiler is really unforgiving. The initial steps can be a bit difficult, but it makes for a deep feeling of satisfaction when your program finally compiles. At this point you have:

  • a high confidence that your code is correct
  • a super-fast and lightweight binary

Rust also makes it very easy for you to use existing C libraries, which is especially important since the ecosystem is very young. For example, we used the Sox library for recording audio. There is a bit of boilerplate involved because you have to redeclare all the prototypes of the C functions, structs and enums you use, but once you get used to moving back and forth between Rust types and C types, it becomes a fairly easy process. The main caveat is that your Rust code becomes less “pure”: you have to enclose your C function calls in unsafe{} blocks and you may need to do some memory management and casting by yourself. Rust does a pretty good job at making this as non-ugly as possible, though.

Overall, we’re really satisfied with our Rust experience, and we will probably use it for other projects. The community is moving very fast but it’s shaping up to be an excellent way to do some low-level programming without ending up with an unmaintainable mess after a few thousand lines of code. And you can even bring on board some of your beloved functional programming constructs and type-safe macro wizardry.

Cherry

Cherry: An Extensible Hub for Office and Home Automation

Cherry is an easily extensible framework for home automation. It runs on Node.js and is written in ClojureScript. Cherry acts as the hub of your smart home, allowing your connected components to communicate with each other. Cherry’s power comes from its plugin system. Plugins are building blocks that you assemble to create your own smart system.

Hooking up a system on Cherry is easy. As an example, let’s say you have a Philips Hue light and you want to turn it on by pressing a button. You just need a few lines of Javascript.

module.exports = function (cherry) {
console.log("lightswitch ready to rock!");
cherry.handle({
pin: function (message) {
var plugins = cherry.plugins();
if (message.state === "high") {
plugins.hue({on: true});
} else if (message.state === "low") {
plugins.hue({on: false});
}
}
});
}

A simple Cherry plugin to connect Hue lights to a GPIO.

Cherry also acts as a repository for home automation/internet of things plugins. We believe in an open, crowd-sourced solution for home automation and this is our go at it. We already created a handful of plugins to show how easy it is to connect devices together (cherry-wit, cherry-spotify, cherry-hue, cherry-gpio). As new devices come out, we’ll be releasing more plugins (Nest, Myo, Google Glass, Pebble, …). We’ll also feature the plugins from the community.

Project Cambridge

Project Cambridge — The Automated Office

Our office automation system is built on a cheap Raspberry Pi running Cherry and Cherry’s suite of plugins including Witd. The Pi is also connected to various services through those plugins as well as a microphone for voice recording and speakers. Using the microphone, you can ask to search for some new music, change the color of the lights or ask the system some details about itself. We’ve also integrated HipChat so we can control the music in our office through text or voice commands. Check out the video below to see Project Cambridge fully in action.

http://www.youtube.com/watch?v=UXdybbdOZj0

Both Cherry and Witd are open source and getting started is easy!

Check out the Cherry README to get started with Cherry. We’ve even included a small script to get everything up and running on a Raspberry Pi.

Witd is written in Rust and we recommend you use Cargo to build it.

A few plugins to interact with your favorite components:

Team Wit

--

--