Posts Tagged ‘twitter’

Tweetbot … the code

You can now find the code of the twitter robot on my github at: https://github.com/pchretien/tweetbot Feel free to fork … copying is not stealing! :)

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 = 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.

A video of the presentation should be available on my Youtube channel soon …

 

Opting Out …

I just opt-out from Twitter and Facebook. Since they keep all your account data in their system upon deactivation, I first deleted all my pictures, personal information, friends, followers, etc.

This has nothing to do with concerns about privacy. This is only a matter of focusing on important and useful stuff. These media are asking me too much time for the bits of  information and entertainment I get from it.

I’ll call or email my family and my friends … I don’t want to know what they do on a daily basis. Having a drink still is my favorite way to meet my friends … my real friends.