Want to see more?
► App Reviews
► Product Reviews
► Tutorials
Trending
Blog Archive
 RSS feed

May 2, 2011 | 1:36 pm

Arduino to Twitter over USB

Arduino to Twitter over USB

There are many reasons to want a project with the ability to provide status feedback.  One useful method for providing feedback is by posting updates to Twitter.  This enables one (or many people) to monitor a system from anywhere.  It is even possible to, then, receive SMS updates to your phone.  I will describe how your Arduino can become a versatile Twitter-enabled device.

A simple Google search shows that there is plenty of documentation available for using an Arduino with an ethernet shield.  However, in many cases, if the Arduino can be plugged into an ethernet port, it could be plugged into a computer.  This tutorial will explain how to send status updates to Twitter without an ethernet shield.  You simply need to have your Arduino connected to an internet-connected computer.

For this tutorial, I will be using the Python programming language to help my device post to Twitter.  To make this tutorial compatible with all operating systems, I will use pySerial, a wrapper library that provides Python access to the USB serial port.  Additionally, I will utilize the Python-Twitter library to interface with the Twitter API.


Functional Overview
The basic software flow of this project (sans configuration) is shown in Figure1.  


Figure1: Functional Flow Diagram

Observe that the Arduino prints status updates to the serial port.  Python then reads the serial message, and posts it to Twitter.  The details regarding the End of Line character and timeout will be discussed later.

Twitter API
Before your project can integrate with Twitter, you must register your application and get an API key.  First, you must create a Twitter account for your project.  This is the account to which the status updates will be posted.  Then, navigate to dev.twitter.com and register your app as a Client with Read & Write access.  Obtain the following tokens:

  • Consumer Key
  • Consumer Secret
  • Access Token
  • Access Token Secret

This information will be used in the Python code, later.

Software-Arduino
Configuration of the Arduino is fairly straightforward.  First, note the port location of your Arduino.  For me, it was /dev/ttyUSB0.  This will be used in our Python code.  In the Setup procedure, be sure the Serial library is initalized and the baud rate is established.  I am using 9600bps.  The same value must be used in the Python code, later.

void setup()   { 
Serial.begin(9600); // set up Serial library at 9600 bps 
 }

Place the code for the functionality you wish to implement within the Loop procedure.  This will vary depending upon your application.  You may want to process data from sensors, for example.  When you have the message you wish to post to Twitter, print it to the serial port.  In Python, we will read from the serial until we see the newline character.  The easiest way to append the newline character to your serial messages is to issue the following command.

Serial.println("This is your status message");

Software-Python
Next, I will cover the Python code.  Python is an indentation sensitive language, so you may wish to start from this template.

First, ensure the pySerial and Python-Twitter modules are installed by following the installation instructions on each page. Next, import the corresponding libraries (and the time library if you wish to use Python’s sleep function).

import twitter
import serial
import time
Then, authenticate yourself with the Twitter API.
api = twitter.Api(consumer_key='your key here', consumer_secret='your key here', access_token_key='your key here', access_token_secret='your key here') 

Now, we configure our serial port.  Be sure to include the location and baud rate from before.

arduino = serial.Serial('/dev/ttyUSB0', 9600)

Next, we will wait for a new line to come across the serial port.

status = arduino.readline()

PySerial’s .readline() function will wait indefinitely until a message with an EOL character is sent across the serial port, so be careful.  (If you know when you are expecting a serial message from your Arduino, you can use a timeout to ensure the code does not lock up.)  Next, we will post the status to Twitter.

api.PostUpdate(status.text)

That is all there is to it!  Place the appropriate code into a loop if your Arduino will be periodically relaying information.  If, for example, you want to post a status (maybe with the current temperature) every hour, the following could be done.  Rather than placing an hour-long delay on the Arduino, you can choose to delay the Python code and only read the information coming across the serial port every hour.

while 1:
     status = arduino.readline()
     api.PostUpdate(status)
     time.sleep(3600) ## sleep for 3600 seconds
     del status

Note:
The Python-Twitter library can also be used to read messages from Twitter.  This could be useful for issuing remote commands to your Arduino.  The simplest way to implement this functionality is to parse the tweet (looking for a keyword), and then send a command byte to the Arduino.  When the Arduino receives the command byte across the serial port, it can execute the desired action.


For more from Brad Wells, visit

Notes

  1. djdusk reblogged this from 5m0k3
  2. 5m0k3 posted this