Arduino Twitter Printer Experiment: Part 1

Arduino Twitter Printer Experiment: Part 1

Arduino Twitter Printer Experiment: Part 1

Introduction

One of the perks of working for a company like GRIT is that, between projects, we occasionally get the time to experiment with new technologies. Usually this experimentation takes the form of trying out new software and frameworks with the hopes of passing on the benefits to our customers if we discover something amazing, but sometimes we like to experiment for the sake of fun and nothing else.

As a web developer it’s a rare occurrence for me to dabble with hardware, past building myself a new computer or setting up a network; it’s always been something I’ve wanted to try my hand at, but I imagined the complexity to be too great for anyone without an electrical engineering degree to get into.

Enter the [Arduino]: an open source hardware prototyping board that makes the world of hardware accessible enough, so even a purely software person like myself can get stuck into making anything from [a robot] to a [Twitter-enabled coffee pot].

Myself and one of our resident front-end developers, Tony, put our heads together to come up with an interesting starter project to get to grips with the world of Arduino, past the standard blinking LED exercises that can be found on tutorial sites. We came up with a myriad of ideas, some of which I’m sure we’ll experiment with in the future, and settled on a printer connected to Twitter.

Our objective was to use the Arduino to connect to a thermal printer (exactly the same technology used in receipt printing at your favourite local store), and through some means of communication, listen for certain keywords on Twitter, printing the corresponding tweets out in real-time. At this stage we didn’t have much of an idea how we would go about transferring the tweets to the Arduino, but like all good experiments we felt it best to get stuck in and learn as we go.

The Hardware

Tony and I pitched our ideas to the team, and within a few days we were greeted with the below starter kit, a thermal printer, and a few bits and pieces that we had worked out we would require to get this project up and running.

starter_kit (1)

Upon opening the box I was greeted with a huge array of components, most of which I had only seen when I used to have the terrible habit of disassembling household electronics as a child, much to the dismay of my family, in a vain attempt to discover what magic inside made them work. The kit itself appeared to be pretty well stocked with everything a beginner would require, having components such as LEDs, resistors, wires and some parts I only vaguely remembered from high school physics lessons.

components

The Arduino itself was tiny; much smaller than I had imagined, which came as quite a surprise to me given the amount of elaborate projects I had read about people constructing on-line. Here’s a Banana for scale:

banana

The Set-up

The next few hours were spent reading up on the thermal printer and discovering, like with most open source projects, that someone had already gone to the effort of creating the tools I would require to begin: namely a [thermal printer library], to handle all of the low level input and output.

A few days of tinkering during my lunches and I managed to get the first printout, sent directly from the Arduino to the thermal printer, to work. As is a long standing tradition in the world of programming, the first words were of course:

Hello World“.

hello_world

From this point I decided to add a little eye-candy to the board, as much as all the mess of wires on the breadboard would allow, in the form of an RGB LED. Unlike the usual LEDs you’re probably used to seeing, this one was special as it had three separate lights inside of itself; one for red, blue and green. Mixing various levels of brightness in each of these lights allows for the bulb to appear many different colours. Hooking this up to the board was little trouble, and required the use of three resistors to avoid damaging the component. Soon we now had a status indicator for the printer, turning green when the printer was idle, red when busy, and flashing blue for the soon to be implemented communication.

breadboard_ledSerial Communication

Once the printer was working, it was time to look at a means to actually send the data, required for printing out the tweets, to the Arduino. After some reading it looked like there were several options for doing so, one being using an Arduino shield (an add-on board, used to upgrade the Arduino’s functionality) and another using the actual USB connection currently in use by the computer to send compiled code to the board. Not being the patient type, I elected to forgo the waiting period of a delivery for the shield, and tried using the USB instead.

One of the technologies that we’ve been experimenting with recently at GRIT is Node.js, a JavaScript-based server framework which gives us Web Developers the ability to code server-side programs in a familiar language usually used exclusively for the front-end of websites. JavaScript is great like that; it’s easy to understand and forgiving for those just starting out, which makes it a perfect choice for a wannabe hardware hacker.

Once again I was delighted to discover that most of the hard work had been done for me, and there existed a [node library] for serial communication that would allow me to get to work with sending data to and from my computer to the Arduino, as well as a [Twitter library] for pulling in tweets as and when they are made using Twitter’s streaming API.

The Challenge

Now came the hard part. Up until this point the experiment had mainly consisted of playing around with wiring, following a few guides, and using open source libraries kindly provided by those with more experience with hardware – now I had to get my hands a little dirty with some code, which is all part of the fun of course!

To send data from the Arduino to the PC is a fairly simple process, involving only the following lines of code to work at it’s very basic:

Serial.begin(9600);
Serial.println("Hello World!");

Sending data from the PC to the Arduino, however, is another matter entirely. Serial communication sends data in bytes, meaning that if you were sending the word “Hello”, the Arduino would only receive one letter at a time, for example “HELLO“. This poses a problem if we want to print out a tweet, as we need to store an entire sentence or more before we instruct our printer to output it onto paper, and the Arduino needs to know when that sentence is completed before it does so.

The trick here, I found, was to create a “buffer”: a method to store the incoming data until it was ready, which we would be able to determine by the sending of a special character known a carriage-return. In computer code we represent this character as a “\r”, and you’ve used this yourself many times without probably realising by pushing the return key on your keyboard.

A portion of the finished code below shows how this was achieved, with some of the unimportant parts removed:

void loop()
{
  while(Serial.available() > 0)
  {
    char received = Serial.read();
    //if the character received is not a carriage return, add it to the buffer
    if(received != '\r')
    {
      inData += received;
    }
    else
    {
      //Send the message to the printer
      printerStatus(2);         //Tell the PC the data is received, and flash the LED blue
      printerStatus(0);         //Tell the PC the printer is busy, and change the LED to red
      printer.wake();           //Wake the printer up
      printer.println(inData);  //Print the message
      printer.feed(1);          //Move the paper up one row
      printer.sleep();          //Tell the printer to sleep
      inData = "";              //Empty the buffer
      printerStatus(1);         //Tell the PC the printer has finished and is ready
    }
  }
}

A few hours of tinkering later, and a few bug fixes, and the entire project had come together. It was now time for the first test:

tweet

printout

Not bad for a first foray into the world of Arduino, thanks in no small part to the incredible hard work that went into making the open source libraries mentioned above, and of course the device itself.

One thing to note here is that the printout itself appears quite faded, from what we’ve gathered this is likely due to our power supply not being enough for the job – something to look at in later experimentation!

For a short time we plan on leaving this set-up as-is, making it so any tweets directly to our twitter account are printed out for our amusement, but it won’t be long before we get the chance to tinker again which leads me on to:

The Next Step

While doing this project some of us had a few ideas on expanding the concept further. In the future we plan to look into printing images from the printer, and creating a web interface so visitors to our website can play with the printer directly. We have already managed to print an image using the device as a small proof of concept, albeit having to dither the image beforehand in Photoshop to allow for black and white to appear multiple shades. Look out for a part 2 in the future!

In the meantime, please enjoy the printout of Arnold Schwarzenegger (believe it or not!).

arnie