Monthly Archives: February 2011

A few years ago I bought some bi-color or dual-color LEDs. These are LEDs that have only 2 leads like regular LEDs, but can display 2 colors.

An LED is a type of diode, so it only allows electricity to flow one way through it. But when that electricity is flowing, it lights up.

The bi-color LEDs are essentially 2 of these in one package in opposite directions.

With an Arduino, you normally connect one end to ground and the other end to a digital pin and just write a HIGH to that pin to light the LED.

I wasn’t sure at first how to use the bi-color LEDs with the Arduino, but it turns out it is simple. All you have to do is connect each end to a digital pin and write LOW to one and HIGH to the other. Reverse them and it lights in the other color.

This works because writing LOW sets the pin to 0V just like writing HIGH sets it to 5V. This means that one at LOW and one at HIGH creates a voltage drop across the LED, lighting it up.

Since the rest of the site was written in PHP already, I figured I would do the Twitter posting in PHP as well. I had done it before when working on webbr so I figured it would be just as easy.

But it turns out Twitter recently deprecated Basic authentication and made OAuth mandatory. This means I can’t just send my username and password, but instead needed to register as an application and go through a handshake process before I could post.

The good news is, if you are just posting from a single account and a single script, you only need to do this once.

Here is how I was able to get my server posting a name from the database to Twitter every day. For this, I assume you have a webserver running PHP that is accessible from the general internet (not on a local network only), and a basic knowledge of PHP.

Step 1: Register as an application

To use OAuth with Twitter, they require you to register as an application. This makes sense if you are writing an application to deal with multiple users, but even for 1 you have to do it.

If you go to http://twitter.com/apps you can register a new application. Most of the information here doesn’t matter for our purposes. The only thing you need to pay attention to specifically is the callback URL. For this, put a URL to a page that you can create on your server, such as http://example.com/twitter_callback.php. We will create this site soon.

Once your application is created, you will be given a consumer secret and a consumer key. We will need these in the next step.

Step 2: OAuth authentication

Now we need to go through the OAuth process to give our code access to the account. Since we only want to post for this one account, we don’t need to make this code very robust.

I used the Twitter-async library from https://github.com/jmathai/twitter-async. Grab the EpiCurl.php, EpiOAuth.php, and EpiTwitter.php files and put them on your server somewhere you can access them easily, preferably in your include_path.

Now create a file on your server that you can access. In this file, include the files we just created, and then add this code:

You need to make sure to have the keys from your application and the secret that you got when you authenticated here or it will not work.

Now if you look at this, you may be wondering about the last line. It looks like it is unnecessary, but because of the library we are using, we actually need it. This is because it is an asynchronous library, so our call to post does not mean that the post has been made. Referencing the response of our call forces our code to block until the call has completed.

Now you just need to write some code to generate your message and run when needed. For Flying Pie Notifier, I just used a cron job that runs once a day and pulls the name from the database for that day.