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.

a’twittering Arduino without an Ethernet shield

The Arduino Ethernet shield is pretty awesome and can, for example, allow a mini webserver to run off the board displaying sensor data. Although not outrageously expensive, at $45, it’s still $15 more than an Arduino Uno itself. There does seem to be quite a bit of functionality that you can do with it, especially as the official Arduino Ethernet shield includes a micro SD slot allowing to read + write data to card, but for my tinkering right now, it’s a little too much money.

But, it would be cool to at least look at how to push data from the Arduino out the Internet, and so I started looking around at ways to utilize the USB connection and have the computer the Arduino is connected to act as a go-between. Cue some fun python libraries. The setup entails using pySerial to monitor the USB port and read in data printed to the Arduino serial port, and then python-twitter to push that data out as a status update to twitter. This was all based off an awesome post from Brad Wells on using Arduino to Twitter over USB.

To get the python libraries installed and ready to roll:

  • Download pySerial – unpack the archive and then from the command line within that directory run
    python install
  • python-twitter has three dependencies that need installing first:
    • simplejson – download and unpack the archive, and then from the directory run
      python install
    • httplib2 – download and unpack the archive, and then from the directory run
      python install
    • python-oauth2 – download and unpack the archive, and then from the directory run
      python install
  • Now you can download python-twitter – unpack the archive, and then from the directory run:
    python build
    python intall

Now we need to get twitter ready. The python script uses the twitter API, so it requires you to register your own application in order to get the access tokens needed to function. Most people create a separate twitter account specifically for the Arduino to tweet to (the entry in the Arduino playground for tweeting using the Ethernet shield has you running through an external service to pass the tweet, so you probably shouldn’t use your primary account), and when registering your application at, make sure you’re signed in to twitter using this dedicated Arduino account. After registering, your consumer key and consumer secret key are immediately provided – click on ‘My Access Token’ to gain the access token and access token secret.

The python script, broken down and detailed in Brad’s blog post, requires you to copy and paste those four twitter tokens in to the script. On the Arduino side, the sketch can be as simple as:

Serial.println("Tweeting arduino!");

within setup(). All you need to do is initialize a 9600 baud serial instance and then print out a line. Whatever your print out to the serial port is what the python script will read in and tweet. Make sure you compile and upload the sketch to your Arduino before you run the python script – as you have python monitoring your USB port to watch for the serial output, you can’t then upload via USB to the Arduino. Once you sketch is uploaded, you can fire up the python script and then hit the reset button on your Arduino.

Tweeting Arduino

The python script will just sit at the command line, watching for messages sent out to the Arduino’s serial port. If you check your twitter feed, you should see your tweet 🙂 You can easily expand beyond simply printing a line of text to reading in data from sensors and have that included. It would definitely make it more useful, and within the python-twitter library, there are functions to also monitor twitter updates and send data back to the Arduino. That’s something I haven’t quite figured out yet, but is definitely an appealing feature!

Arduino RTC timer sketch

Something somewhat useful, and the main reason I started playing the the DS1307 real time clock, was to be able to accurately control events based on the current time. In the same way you can get a simple plug-in timer to turn on or off an appliance depending on the time, I wanted to be able to do the same with the Arduino.

I wrote a fairly wide-ranging RTC timer sketch that I posted to Github that will allow you to execute a given block of code or function based on a start and end time that you specify. Although the default sketch is simply turning on and off an LED connected to digital pin 2, there’s probably a lot of situations beyond just turning a light on or off that this could be used for.

Hopefully it’s useful, and it’s definitely something I’ll be incorporating in to other programs I’ll be writing 🙂

Ardunio RTC, temp sensor, and LCD

I figured that if I was going to end up building some kind of aquarium controller / monitoring system based around the Arduino, I’d need a way of keeping track of time. Either to automate certain events like turning lights and off, or simply to log the time temperature readings we taken, I’d like something more advanced that milli() built in to the Arduino to track time since it was last turned on. Plus, if the power goes out (we lost power for a few hours on Monday after a big storm came through…), then I’d have to tell the Arduino to reset it’s time and calculate afresh when it needed to carry out certain events.

There’s a few basic little circuits built around the DS1307 chip that act as a real time clock – I went with this little kit from Adafruit. It only took 5-10 minutes to assemble and solder, and then I had fun playing with outputting various time formats to the serial monitor and calculating dates in the past and future. Ultimately, I wanted to hook it up to the LCD screen though.

I also got a tiny TMP36 temperature sensor, with the plan being to craft a waterproof housing so it could sit in the aquarium to record water temperature. It only uses one analog pin (in it’s most basic form of connecting), which is great since I’m basing things of the Arduino Uno so don’t have a massive number of pins available. But, with the LCD backpack reducing the numbers of pins required, it’s not too bad. I discovered to output the degrees symbol (°) is written to the LCD using lcd.print((char)223); or to the serial monitor with Serial.print(176, BYTE);

Arduino with LCD, RTC, and temp sensor

Anyway, after figuring out running both the LCD backpack and the RTC off analog pins 4 + 5, I managed to get the LCD displaying the current time and temperature in both degrees celsius and fahrenheit as shown above. Here’s the actual diagram of the circuit (Fritzing file available via Github below):

Circuit schematic

It was a pretty cool set up, though it was kinda annoying that there’s nothing built in to Arduino or the RTC library that allows for a leading zero on the time if only a single digit is present, such as 7 seconds when on a clock it should ideally display 07 seconds. A simple if (now.second() < 10) lcd.print("0"); checks if the value returned from the RTC requires this leading zero and outputs it to the LCD before the actual time value.

The code and circuit schematic for this little project is available on Github at Arduino-LCD-time-temp, and I guess is the first little program beyond basically copying and pasting example code that I've written. It was largely based on the DS1307 RTC tutorial by Ladyada along with her TMP36 temperature sensor tutorial. The Ardiuno forums also had a good few little snippets on how to write out the degrees symbol both to the LCD and to serial when I was testing, and different methods to write out the leading zeros when dealing with time.

Playing with Ardunio

With college classes done for the summer, I’ve gotten back in tinkering with electronics a little. I had trouble with my Basic Stamp 2 after bouncing around Alaska and being boxed up for a couple of years, so I decided to try tinkering with an Ardunio, something I’ve looked at for a while but never had time. I’m currently in the process of cycling a 55 gallon freshwater aquarium (something else I’m very excited about!), and have some cool ideas for building a basic monitoring system that can keep track of water temperatures, control lights, and provide a status report and XML feed or something for use elsewhere. So, I picked up a cool Arduino Uno.

I bought a 16×2 LCD from Adafruit which helpfully included the header pins, along with an i2c/SPI backpack which allows easier connection to the Arduino and uses less pins. After remembering which end of the soldering iron to hold, I got the header connected and the backpack attached (though I really need to clean up my soldering job…):

i2c LCD backpack

Using a modified LiquidCrystal library for using the i2c connection made it pretty straightforward to write out to the LCD. Using two analog input pins along with the 5V and GND made it really quick to hook up the board too. The basic code for displaying two lines of text is:

#include < Wire.h >
#include < LiquidCrystal.h >

LiquidCrystal lcd (0); // Initialize a single i2c LCD

void setup() {
lcd.begin(16, 2); //Set up our 16x2 LCD
lcd.print("Line 1 text");
lcd.setCursor(0,1); // Set our cursor to character 0, line 1
lcd.print("Line 2 text");

void loop() {

Which should display something a little like this:

Arduino and 16x2 LCD

This was all based on the awesome tutorials from Ladyada on connecting the i2c backpack and writing characters to an LCD. There’s a wealth of awesome info and tutorials, and a great starting place for components and code ideas 😀 I’m looking forward to playing around with the temperature sensors and battery powered real time clock over the next week or so.