Pages

Saturday, 28 February 2015

Tweeting Morse Code with Raspberry Pi and Arduino

I'm stuck at home and have seized the opportunity to prepare for a workshop that will link the Shrimp and the Raspberry Pi.

I love reusing previous projects. This one combines two bits of software that I prepared earlier :)

Twitter client meets Morse Code flasher

The core idea is simple. The project listens to what's happening on Twitter, and displays selected tweets by flashing the contents in Morse Code.

You could do this using the Pi on its own, or the Arduino on its own, but the project is easier and more reliable if you combine the strengths of both platforms.

Why the Pi?

It's possible to run a Twitter client on the Arduino but you're pushing the hardware to its limits. I've found some Arduino Internet libraries to be unstable, and your application is likely to be using most or all of the Arduino's limited program and data memory.

The Pi's memory is roughly a million times larger than the Arduino's RAM, and Internet applications are very stable. There are several Python-based twitter clients on the Raspberry Pi to chose from. In an earlier project I picked tweepy, which was easy to use and well supported.

Why the Arduino?

You could write a morse code flasher on the Raspberry Pi but

  • you'd need some extra hardware to flash an LED, and
  • you couldn't easily ensure that the Morse code timings were correct.
The difficulty with timing comes from the fact that the Pi is running a multi-tasking operating system. This means that your application can be preempted by a more urgent task at any time.

If you've just turned the LED on, it will stay on until your program gets a chance to turn it off again. There's no way to predict how soon that will be.

By contrast the Arduino has no Operating System, so timing is much easier to predict and control.

Connecting the two

It's not hard to write a Morse Code flasher for the Arduino, and I use that as one of the exercises in Life after Blink. It took me a couple of minutes to install the code on an Arduino, and a few more to get the Arduino talking to the Pi over a serial connection using py-serial.

I already had the Twitter client code from my earlier project. The project filtered the stream of tweets, printing all those with a particular hashtag. I installed the script on the PI and modified it to send its data over the serial port.

Voila! As tweets are received the Python program  sends their text over a serial link to the Arduino. Then the Arduino converts the text to Morse Code and flashes out the content using the on-board LED.

What next

There are a few more things I plan to do. 
  • replace the Arduino with a Shrimp.
  • improve buffering so the project can handle very popular hashtags
  • make the script and sketch available on GitHub
I'll tweet when I've done them; follow @rareblog if you're interested.

Please comment below if you have any questions, or have ideas about how to improve the buffering!