Building the Pinoccio Pomodoro

This week, I had the chance to hang with the team at Pinoccio – they’re building magical little wireless microcontrollers that talk to each other (via a radio mesh) and to a web API (via wifi). When you combine those boards with prebuilt “backpacks” of sensors and other tools, you get a powerful tool for building your personal internet of things.

I was looking for a short project to learn how to interact with my Pinoccio troop. The company has built an easy to use node.js module, so I decided that a little javascript app would be a great starting point. I also wanted to build a tool that would both talk and listen to the troop, passing commands via the API and receiving instructions back. Since this was both my first Pinoccio app and my first Javascript app, I wanted to keep things as simple as possible.

After poking around with a few ideas, I decided to build a little Pomodoro timer, powered by Pinoccio. I spend a lot of time at my desk context switching between writing/programming on my laptop, and reading/studying on my kindle or paper textbook. Having a big, visual indicator telling me to focus or take a break would be really useful – sort of a needlessly technical version of Chris Poole’s chart.

For this tutorial, I’ll assume you already have a Pinoccio troop, consisting of one wifi connected lead scout, and one scout with a button installed. I’ll also assume you have Node.js installed, and that you have a tiny bit of familiarity with javascript. No idea what I’m talking about? No problem – just start with this guide to setting up your troop (via the browser!) and this tutorial for getting a button working with your scout. Node.js docs can be found here.

Once you’re all setup, take a minute to play around with your troop in Pinoccio HQ. Turn your LED’s on/off, and make sure your scouts are responding to scoutscript commands (you can type them right there in the browser!).

Next, lets install the Pinoccio node module. After you login, run the “pinoccio token” command to make sure you’re setup properly. Save the token that returns, as we’ll need it in just a minute.

Now, on to setting up our javascript app. Create a new folder for your app, then create a new javascript file. I called mine “pinoccio-pomodoro.js”.

Next, you can run “npm install pinoccio prompt” from the folder you created your javascript file in. Make sure you also make the changes in the javascript comments, adding your token and the correct troop/scout ID numbers. Once our file is setup, we can start listening to Pinoccio’s streaming API:

The first console.log() is a quick test to make sure we’re properly connected. run your app from the terminal, and you should see a stream of JSON on your screen. Press the button on your scout, and you should see new data being reported.

Next, delete or comment out the first console.log(), and uncomment console.log(“Button-press task started.”). Rerun your app. Now, you’ll see “button press task started” each time you press the button on your scout. Not working? Make sure you’ve have the correct value for your troop and scout names.

Now, we need to write two simple functions to interact with the user via the command line, and to pass messages down to the pinoccio that will display our timer light. Starting with the user interaction, we’ll use a simple instance of the “prompt” module which makes it really easy to interact with users on the command line. Our code for this method looks like this:

The prompt documentation has a good explanation of the different configuration options, if you want to make any changes. To wrap everything up, we just need our actual timer method.

Most important here are the two calls we make to the Pinoccio REST API. We use the node module to post to the scout when the timer begins, and when the timer expires we change post a new command to switch to green.

These commands are simple ScoutScript, so read the docs and experiment with making changes: make the LED turn different colors, or pass entirely different commands.

So, now all that remains is putting it all together. The complete code:

You should note the addition of the call to setTimer() when a button press is detected in the API listener. The same call is added to the “yes” answer if-statement in the runPrompt() function.

So that’s it! You should now have a working indicator light that responds to either a button press or console command. Each time you start a new timer, it will run for 25 minutes, before prompting you to start a new pomodoro.

As a next step, I plan to add better tracking of completed tasks (maybe some sort of goal/progress tracking), and maybe figure out a few more interesting Pinoccio interactions to add in. I’ll probably also hook the lead scout (with LED) up to a larger board that can be hung up over my desk.

blog comments powered by Disqus