Twitter Controlled Robot

I presented this project at the 28th Montréal Python meeting. I posted the slides of the presentation in my previous post here.

The objective of this project was to demonstrate the use of  pyserial and XBee to wirelessly control a robot. This project was also my first attempt at using the Tweepy library. The requirements for the project were the following:

  1. The communication with he robot must be wireless
  2. The connection to the Twitter website is made by a separate computer using the Python library Tweepy
  3. The robot should move according to the messages it receives
  4. In addition to the movement, the robot should have a servo that indicate its direction
Tweeter Controlled Robot

Tweeter Controlled Robot

The code running in the Arduino is a very simple program. The micro-controller read on the serial port to get a char command. The commands are text integers. The robot moves according to the command it receives. You can find the code to drive the robot in my previous post Arduino Motor Controller Using an L293D Chip. The following loop() function of the Arduino program receives commands from a computer trough the serial port. Depending on the nature of the command, the robot will perform different actions.

void loop()
  if ( Serial.available())
    char ch =;
      case '1': // forward
      case '2': // backward
      case '3': // right

Twitter Robot

Twitter Robot

On the computer we are running a Python that connects to, read it’s messages and send commands to the robot that are related to the content of the messages.

import tweepy
import time
import serial


ser = serial.Serial('COM8', 19200, timeout=0)

# Initialize the tweepy API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

api.update_status('Start listening at ' + time.strftime("%Y-%m-%d %H:%M:%S"))
print "Start listening at " + time.strftime("%Y-%m-%d %H:%M:%S")

# This is where we should send the command to the robot ...
def process_text(author, text):
  print author
  print text

  if text.lower().find("cmd1") > -1:

  if text.lower().find("cmd2") > -1:

  if text.find("#mpr-bye") > -1:
    quit = True

quit = False
lastStatusProcessed = None
lastMessageProcessed = None

while True:
  for status in tweepy.Cursor(api.friends_timeline, since_id=lastStatusProcessed).items(20):

    if lastStatusProcessed is None:
      lastStatusProcessed =

    if > lastStatusProcessed:
      lastStatusProcessed =

    process_text(, status.text)

  for msg in tweepy.Cursor(api.direct_messages, since_id=lastMessageProcessed).items(20):
    if lastMessageProcessed is None:
      lastMessageProcessed =

    if > lastMessageProcessed:
      lastMessageProcessed =

    process_text(, msg.text)

    if quit:


api.update_status('Bye! ' + time.strftime("%Y-%m-%d %H:%M:%S"))
print "Bye!"

The Twitter anti-spam filter will block you to send twice the same tweet to someone. it is then difficult to use specific commands to control the robot since you can’t send it more than once.

The solution is to use common words as commands and to incorporate it into your tweets. That way you can send multiple variations of the same command simply by decorating the command with more text.

You can download the source code for both Python and Arduino from my Github account.



Tags: , , , ,

4 Replies

Trackback  •  Comments RSS

  1. Ben Walker says:

    Hi, you have just kind of saved my grade with this post. I am making a scale that moves depending on tweets, kind of a social commentary. I read through the previous post but it only has the void setup and a void loop but none of your ints like checkdistance. Is there any chance you can post the full ‘.ino’ script that you used for this twitter controlled robot. I just want to compare it and change parts to mine if possible?

    Thanks for your time :)

  2. pchretien says:

    Thanks for the reminder … I had planned to put the project on github and completely forgot about it. I’ll post the complete sources tonight.

  3. Ben Walker says:

    thanks a lot. It’s been really useful reading it through and your work with arduino and python is really impressive :)

Post a Reply

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