Introducing Tweet-a-Veyor: An MDR Conveyor Driven by Twitter
Written By: Ric Halvorson
You could say one goal of our marketing department is to use social media to push conveyors. For this blog post, as a nerdy engineer may be inclined, I took that a little too literally. Allow me to introduce the Tweet-a-Veyor – an MDR (Motorized Drive Roller) conveyor driven by tweets:
There are several hurdles you might run into when developing an application like this – having never even sent a tweet before – I’m pretty sure I ran into them all. But if you’re like me and want to learn how to use Twitter to drive a nifty embedded gadget of your own, this might help get you started.
The first step to any project like this is to register your application with Twitter. It’s a fairly simple process, and can be started here: https://dev.twitter.com/. You sign in with your regular Twitter account and give some information about your application. When you’re registered, you’ll be given two of the four OAuth (Open Authorization) security keys needed to use many of the API’s functions. The remaining two keys will come from the user as he or she authenticates with your application. For simplicity’s sake, I limited myself to one user – me – so I went ahead and generated my user tokens as well. (More information for this method can be found here: Single-user OAuth with Examples)
I wrote a simple program in C# to send the tweet-driven signals to my microcontroller (MCU), an ATmega328P. Obviously, neither C# nor that particular MCU are required to get a device like this up and running; I’m just comfortable with C# and had some spare ATmega328P MCUs. There are also a number of handy .NET libraries available that provide an easier interface between C# and Twitter, such as Twitterizer, which a friend of mine suggested. To start using Twitterizer in your C# application, follow the instructions on the “How Do I?” section of the Twitterizer documentation page. Just be sure you’re using the .NET Framework 4 as your Target Framework as opposed to the .NET Framework 4 Client Profile, and you’ll be good to go.
Twitterizer’s documentation didn’t include a “Here’s How to Drive Conveyors with Tweets” entry (I was surprised, too), so getting useful information from my posts proved to be the biggest challenge in this project. Fortunately, the library’s classes are intuitively organized, so I was able to dig down into a TwitterTimeline object while debugging and find what I needed. The line of code below will give you the text of the most recent tweet in your timeline, assuming you’ve set up your OAuth tokens object properly:
latestTweet = TwitterTimeline.UserTimeline(tokens).ResponseObject.Text.ToString();
My code in particular searches the most recent tweet for the word “forward”, “reverse”, or “stop” and sends a serial command to my MCU accordingly. I poll my Twitter timeline for the latest tweet a little more than once every second, which is probably not the most elegant solution, but I haven’t discovered a way to have a new Tweet be automatically recognized. (I’d welcome that in the comments section if any reader knows how.)
Connecting to the Hardware
The details of setting up the interface between the microcontroller and your PC could fill another article, but simply put, you need to open a port in your application corresponding to the device, send serial signals to it, and program your MCU to perform based on those commands. If you’re using a .NET language, the SerialPort class in the System.IO.Ports namespace will get you the basic functionality you need.
Many microcontrollers contain a peripheral for handling serial data, and the ATmega328 is no different. Some excellent tutorials can be found here regarding how to setup the USART on an AVR microcontroller. It’s up to you how to set up the port parameters and individual commands; you just have to be consistent in your microcontroller firmware and PC application software.
You’ll also likely want a USB-to-Serial converter to make your life easier when connecting to the MCU. The one I used was actually a converter to RS-485, which is a standard for communicating a single signal as a difference in voltage across two wires. Translating that into the logic levels the microcontroller could read necessitated an additional chip.
There are similar products that convert USB directly to a serial signal the microcontroller can automatically read, such as this FT232RL USB to Serial breakout board. That may be the more sensible route, but it keeps you from learning about RS-485, differential pairs, and a clever way to mitigate electrical noise. I might also suggest it doesn’t bestow the same level of nerd cred. There’s always a trade-off.
In either case, with your PC pulling information from Twitter and a successful connection to a microcontroller, you should have your own gadget running in no time! If you have more detailed questions than this post could answer, or if you want to share your own project, please feel free to do so in the comments section.
Special thanks to Jarrod Stormo, software engineer at Interactive Intelligence, for pointing me to Twitterizer and clarifying OAuth.