Our Printer has a Twitter Account

/

toyota-priusDallas has turned over a new leaf this year, with a very vocal commitment to the environment.

He proclaimed loudly on twitter:

@DallasClark reducing the amount I print (one page at a time) 1:14 PM Jan 11th

Now, we were skeptical about his commitment given his keenness for printing specification documents the size of phone books.

We decided to keep him honest and give our printer a Twitter account http://twitter.com/thekndprinter. Every time he prints, it sends out a tweet. Genius!

Here’s how it’s done.

NOTE: If you are not a programmer, you may prefer to douse yourself in petrol and take up smoking than read this next bit.

We run a HP LaserJet 2600n in the office. It has some very light-on SNMP traps. We queried the MIB’s available to us via the PHP-SNMP Module and snmpwalk() and came up with the following check:

$status = snmpget(“192.168.1.76″,”public”,”HOST-RESOURCES-MIB::hrPrinterStatus.1″);

The defintion for the printer statuses that this trap returns can be found in RFC1514 (Host Resources MIB).

The available statuses were as follows:

  • other(1),
  • unknown(2),
  • idle(3),  //aha this looks like the one we want!
  • printing(4), // this one too…..
  • warmup(5)

By placing a cron job on one of our servers to run very frequently, we could assess whether the printer was idle or busy.

The next step was to hook the script into the twitter API and post a tweet every time Dallas prints.

Like this:

switch ($status) {
 case "INTEGER: idle(3)":
 actionIdle();
 break;
 case "INTEGER: printing(4)":
 actionPrinting();
 break;
 default:
 actionDefault();
 break;
 }
 function actionIdle() {
 echo "Dallas is not printing";
 }
 function actionDefault() {
 echo "The Printer is not ready to print";
 }
 function actionPrinting() {
 global $pithyquotes;
 $twitter_username    = 'theKNDprinter';
 $twitter_psw         = '##########';
 $twitter_message     = "@dallasclark is printing on me again!";
 $twitter_status = postToTwitter($twitter_username, $twitter_psw, $twitter_message);
 }
 function postToTwitter($username,$password,$message){

 $host = "http://twitter.com/statuses/update.xml?status=".urlencode(stripslashes(urldecode($message)));

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $host);
 curl_setopt($ch, CURLOPT_VERBOSE, 1);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
 curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
 curl_setopt($ch, CURLOPT_POST, 1);

 $result = curl_exec($ch);
 // Look at the returned header
 $resultArray = curl_getinfo($ch);

 curl_close($ch);

 if($resultArray['http_code'] == "200"){
 $twitter_status='Your message has been sent! <a href="http://twitter.com/'.$username.'">See your profile</a>';
 } else {
 $twitter_status="Error posting to Twitter. Retry";
 }
 return $twitter_status;
}

Everyone else at KND made a conscious decision to go green a few years ago. Now we just have to wait until Twitter politely points out Dallas’ tree slashing, babybird bashing, species endangering, ozone hole poking ways.

This was the only practical solution because there is no way you’ll find us buying Toyota Prius’ to off-set his trigger-happy ctrl-p finger(s).

Peace out.

Author:

Related Articles