pachube and twitter

we'll figure out a way to do this straight out of the pachube site some time later, but in the meantime, this is a bit of a quick hack: how to connect a pachube feed to twitter so that you automatically twitter with your latest pachube data.

first you'll need somewhere to host a php file, and set up cron to run the command at regular intervals.

your php can look something like this (i'm sure there are more efficient ways to do it):

<?php

// set these

$pachubeKey = "YOUR_PACHUBE_KEY";
$twitterName = "YOUR_TWITTER_ID";
$twitterPassword = "YOUR_TWITTER_PASSWORD";

$pachubeCSVurl = "http://www.pachube.com/api/60.csv"; // REPLACE WITH CSV URL

// write your twitter message, with the correct number of streams indicated by X1, etc.

$twitterMesg = "light: X0 // humidity: X1 // temperature: X2 // accesses: X3";

// probably don't need to change anything below here:

$result = file_get_contents($pachubeCSVurl."?key=".$pachubeKey);

if (
$result)
{
   
$streams = array("X0", "X1", "X2", "X3", "X4", "X5", "X6", "X7");
   
$streamValues = explode(",", $result);
   
$mesg = str_replace($streams, $streamValues, $twitterMesg);
   
$url = "http://twitter.com/statuses/update.xml?status=".urlencode($mesg);
   
$ch = curl_init();    
   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
   
curl_setopt($ch, CURLOPT_URL, $url); 
   
curl_setopt($ch, CURLOPT_USERPWD, "$twitterName:$twitterPassword"); 
   
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,10);
   
curl_setopt($ch, CURLOPT_POST, 1);
   
$result = curl_exec($ch); 
   
curl_close($ch); 
    if (
$result)
    {
        echo
"updated twitter";
    }
    else
    {
        echo
"no response from twitter";
    }
}
else
{
    echo
"no response from pachube";
}

?>

upload this file to your host.

then create a cron job to run that requests that php file at automatic intervals. (creating cron jobs is pretty straightforward, but slightly different depending on your host; you might check here for more info: http://www.htmlforums.com/linux-administration/t-cron-a-beginners-guide-...

your cron might look something like this:

0 */4 * * * /usr/local/bin/GET http://YOUR_URL_HERE.php > /dev/null 2>&1

to update every 4 hours.

see for example: http://twitter.com/hdr_office

small code alternative

Hi uh and all

you can replace the first curl call with the php file_get_contents function and it still works fine.

replace this...

$ch = curl_init();
$url = $pachubeCSVurl."?key=".$pachubeKey;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,10);
$result = curl_exec($ch);
curl_close($ch);

...with...

$result = file_get_contents($pachubeCSVurl."?key=".$pachubeKey);

...looses a few lines :)

NB. you still have to post to Twitter using curl because Twitter's api requires username and password authentication.

cheers

Nigel

thanks

thanks nigel!
(there may well be a way to update twitter by authenticating with a GET request too but i didn't look very hard...)

Re: pachube and twitter

is this still working? I get a "updated twitter" message without twitter being updated (the twitter account is valid and the $mesg value is correct, too) .. hm?

Re: pachube and twitter

possibly not - haven't looked in a while, but the twitter api has changed a little. worth looking it up though, because it's probably on a small change -- if you get something working do post it back here!