Bi-Color LEDs with Arduino

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.

Turns out it is nice and easy.

Automatic Twitter Posting in PHP

I got a suggestion a few months ago to make FlyingPieNotifier.com post to Twitter. It is now working and you can see it posting every day at @FlyingPieNotify

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:


$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
echo 'Authorize with Twitter';

Make sure you replace the consumer key and secret with the ones you got when you registered your application.

Now create a second file with the name and URL that you put as the callback URL when you registered your application with Twitter. In this file, put:


$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);

$twitterObj->setToken($_GET['oauth_token']);
$token = $twitterObj->getAccessToken();

echo 'oauth_token: '.$token->oauth_token.'
';
echo 'oauth_token_secret: '.$token->oauth_token_secret;

Again, make sure to include the Epi files and to put in your consumer key and secret.

Now load your first file in your browser, click the link, authorize your application with Twitter, and copy down the token and token secret that are shown in your browser.

Now we have authenticated, so we don’t need to do that again! You can just delete the 2 files you created, but leave the libraries there, we will use them again.

Step 3: Posting to Twitter

Now that we have an authentication token for our account and our application, we are ready to post.

To post, all we need to do is:


$twitterObj = new EpiTwitter($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);

$result = $twitterObj->post_statusesUpdate(array('status' => $post));
$result = $result->responseText;

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.

After this, there is a lot more you can do with these PHP scripts. The documentation for the Twitter-async library shows how to use the rest of the Twitter API in addition to posting.

thing-a-day

Brittney and I have decided to do thing-a-day this year.

Each year, we will make at least one thing. Sometimes it will be something we do together, sometimes something just one of us did it.

Hopefully a few of my things will be some of the blog posts I have been meaning to write for a while.

You can check us out at alanandbritt.com and see more about thing-a-day at thing-a-day.com.

We didn’t put our first day posts up on the thing-a-day site (trying to figure out how to get posterous to do it automatically didn’t seem to work) but we will probably post the rest of them there.