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

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 = Serial.read();
    switch(ch)
    {
      case '1': // forward
        servo.write(90);
        forward();
        delay(RUN_DELAY);
        stop();
        break;
      case '2': // backward
        servo.write(90);
        backward();
        delay(RUN_DELAY);
        stop();
        break;
      case '3': // right
        servo.write(180);
        right();
        delay(TURN_DELAY);
        forward();
        delay(RUN_DELAY);
        stop();
        break;

        //...
    }
  }
}
Twitter Robot
Twitter Robot

On the computer we are running a Python that connects to Twitter.com, 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

consumer_key=""
consumer_secret=""
access_token=""
access_token_secret=""

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")
print

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

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

  if text.lower().find("cmd2") > -1:
    ser.write('2')

  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 = status.id
      break

    if status.id > lastStatusProcessed:
      lastStatusProcessed = status.id

    process_text(status.author, status.text)

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

    if msg.id > lastMessageProcessed:
      lastMessageProcessed = msg.id

    process_text(msg.sender.name, msg.text)

    if quit:
      break

  time.sleep(15)

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.
https://github.com/pchretien/tweetbot

4 Comments

  1. Ben Walker
    April 16, 2012

    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 :)

    Reply
  2. pchretien
    April 16, 2012

    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.

    Reply
  3. Ben Walker
    April 16, 2012

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

    Reply
    1. pchretien
      April 17, 2012

      Done … you can find the code at https://github.com/pchretien/tweetbot Would love to see what you did … send us come pics, vids … publish your code so others can play with it. Good luck!

      Reply

Leave a Reply

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

Scroll to top