[2013–10–10] FeedAl: Twitter Triggered Motor with Johnny-Five, Arduino and Raspberry Pi

This article expands on my previous topic Setting up a Raspberry Pi web server using Node & Express and it assumes that you have your Raspberry Pi set up in that fashion, or a fashion similar to it.

  • (1) Arduino Uno Board
  • (1) Raspberry Pi (Model B)
  • (1) Red LED
  • (1) Diode
  • (1) MOFSET Transistor
  • (1) 10 Kilo-Ohm resistor
  • (1) 220 Ohm resistor
  • Jumper wires

If you already have completed the process of setting up your Raspberry Pi as a web server, then actually getting your Arduino to communicate with the outside world is quite easy.

Setting Up the Circuit

First and foremost, we need to build the circuit. Below I’ve included a drawing of my circuit, feel free to modify or expand it in any way you’d like. Mine is simple, I draw power for the motor from a 9V battery so as not stress the Arduino board. I included a simple push button as well, that allows you to trigger the motor at any time and ensure that the circuit is still operating correctly. This is especially helpful when you’re trying to debug if an issue is in the circuit or in your code.

[Image lost to the sands of time.]

Once you’ve built your circuit, you should test it by pushing the button and seeing if your motor spins. If it does, we’re in business.

Setting Up Johnny Five

Before your Arduino is ready to start communication with the Raspberry Pi, we want to set it up for usage with Johnny-Five, a Firmata based JavaScript Arduino framework. We’ll follow the steps listed on the GitHub page for getting up and running on the Arduino. These steps are:

  • Plug your Arduino or Arduino compatible microcontroller into your computer via USB
  • Open up the Arduino IDE
  • Select: File > Examples > Firmata > StandardFirmata
  • Click the “Upload” button

If the upload was successful, the board is now prepared and you can close the Arduino IDE. Now your Arduino is ready to start running some Johnny-Five code. You do not have to complete this next step now, as we’re going to have you install Johnny-Five again on your Raspberry Pi. To install Johnny-Five, we’ll use NPM:

However, you may be nervous about leaving the Arduino IDE behind. Don’t be, we’ll be running our apps like we run our normal Node.js apps now, using the node terminal command. But I you may want to see for yourself, and there is a very simple example here. Create a new file app.js, copy the source, and when you're ready, run node app.js on your new file. You may not want to do this if you just spent a good chunk of time setting up your circuit. If that's the case, you can go ahead an unplug your Arduino from your computer now.

Accessing the Twitter API


Now we’re running JavaScript on our Arduino. It’s time to head on over to Twitter’s dev site and create yourself an account. Once you have one, visit your applications.

[Image lost to the sands of time.]

Select the application that you would like to be using, in my case, that application is called “Feed Albert”. You may have to create an application to do this first, go ahead and do that.

[Image lost to the sands of time.]

With your application selected, you should see a group of tabs at the top of the page. We want to keep the Details tab open.

[Image lost to the sands of time.]

Scroll down the page to find the consumer key, consumer secret, access key, and access secret. We need all of these in order for our application to work, so either write these down, or leave this information open in a tab for the time being.

[Image lost to the sands of time.]

Connecting to the Raspberry Pi

Now that we have a working circuit, and our Twitter API access codes, we can begin work on the Raspberry Pi.


This portion is where I really encourage you to read my previous article Setting up a Raspberry Pi web server using Node & Express. It walks you through the steps of setting up your Pi for SSH access either wirelessly (if you have a WiFi dongle for your Raspberry Pi) or on a wired connection. This allows you to develop on your Pi from your normal workstation.

If you know how to do this, or have already set up your Pi for SSH, you can skip the article.

Plug your Raspberry Pi into it’s power source and wait a few seconds (about 20) while it boots up. Once it’s booted, you should be able to SSH into the machine using pi@XXX.XXX.XXX.XXX where you replace the XXX s with your IP address. Once you're logged into the box, lets set up where we're going to build our project.

Via Keyboard and Monitor

With the keyboard and monitor attached, you can just login as normal and begin developing your project.

Starting our Project

Now that we’re on our Raspberry Pi, we’re ready to develop our project.

Once you have your file created, you can open it in your editor of choice. I’ll be using vi in this tutorial, but feel free to swap that out with nano, emacs, etc. Open the file.

And copy the following to your file. Follow along with the comments.

Once we have the code written, we’re ready to deploy it to the Arduino. Plug the Arduino into your Raspberry Pi via USB. Once you see that the Arduino has completed it’s short boot up, you’re ready to run your JavaScript. Run:

You should see some startup output, and then you’re ready to go! Login as the account you want to be able to trigger from (mine is @FeedAlbert) and try your trigger word! Once your tweet has successfully sent you should see your motor hop to life the same way it does when you press the button.

Yay! You’re triggering your motor wirelessly using Twitter, a Raspberry Pi, and Arduino!

Feel free to watch the video example and grab yourself the source. If you have any questions or concerns, please reach out to me at @neildahlke.

Engineer. @hashicorp , formerly @memsql , @UChiResearch . @depaulu alum.