Arduino tweeting back and forth via USB

Following on from last night writing up how to get the Arduino to tweet via USB using a Python script running on the local computer that monitors the serial port to act as a gateway to twitter, I explored python-twitter further to look at then monitoring twitter for @fouldsy_arduino mentions that would then pass data back over the serial connection to the Arduino. This would basically allow you to send a status update to the Arduino twitter account that would be read by the Python script, passing some data to the Arduino to then carry out some code. My basic example involved simply writing the status update “lights on” or “lights off” to then have the Arduino call the appropriate lightsTurnOn() or lightsTurnOff()

Again, all this is done via the USB connection. I don’t have an Ethernet shield which would have made this somewhat easier, as removes the need for Python to do most of the lifting. But, for those without an Ethernet shield and wanting to be able to control their Arduino via twitter, this method works pretty good and is a lot cheaper given you have a USB cable and computer already ;-)

The Python script and Arduino sketch is available under twittering-arduino on my Github. The Python script uses the same twitter API as previously, so assumes you have all the required Python dependencies and application registered at dev.twitter.comcheck the blog post I wrote outlining getting all that going if you need to set all that fun stuff up.

All we’re doing is calling api.GetMentions(), grabbing the most recent tweet sent to our Arduino twitter account, and then parsing out so we end up with just the status message sent to us. We then do an if: elif: to do an arduino.write(x) where x is the integer we determined in our if logic dependent on the twitter status sent to us, and arduino is our initiated serial connection via USB. You’ll need to edit the Python script to include your own twitter API tokens, as well as adjust what you want your if statements to process. I wanted to watch for ‘lights on’ or ‘lights off’ and send a ’1′ or ’2′ respectively:

if message == "lights on":
elif message == "lights off":

As shown below, tweeting @fouldsy_arduino lights off causes our Python script to read in the ‘lights off’ part, convert that to the integer ’2′ and send it via the serial port to the Arduino, which is monitoring it’s serial port for incoming data and then calls turnLightsOff();. I was running a Python script that was monitoring both ways and so the Arduino then tweeted back out “turned lights off” via the same mechanism outlined in my earlier blog post:

Tweeting to control an Arduino

My Python isn’t quite up to speed to figure out how to constantly monitor the serial port for the Arduino sending data that then needs tweeting out, whilst being nice to twitter and only making an API call every 5 minutes to see if any tweets have been sent to it that need passing back to the Arduino, so I’ve made the code to read from twitter to the Arduino available on Github, and hope someone else might be able to figure out that niceity and contribute back ;-)

Also note that the Python script passes back ASCII code data, so you’d need to some converting based on an ASCII table like this if you wanted to pass something other than a single integer. You can pass entire strings back and forth, but to me that seemed inefficient and required a lot more processing when the Arduino receives the serial data, so the method I used in this code, at least to me, made more sense.


Senior Engineer for a St. Louis cloud computing, managed services, and co-location company with a passion for virtualization and server management.

Posted in ardunio, my projects
One comment on “Arduino tweeting back and forth via USB
  1. Google Chrome 30.0.1599.101 Windows 7

    Hello Iain,

    I can’t find any contact info in the site so forgive me for using the comment section. I’m Algen, I work with engineering website and would love to do an exchange of website links (with your website: and feature you as a site of the day on EEWeb (you can see an example here Is this of interest to you?

    Hope to hear from you soon.


    Algen Dela Cruz

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About Me
Iain Foulds, 30 years old. Originally from England, now living in St. Louis after 4 years in Alaska. I currently work as a senior engineer for a St. Louis cloud computing, managed services, and co-location company . Very passionate about photography. More...