Author

Topic: Grab the price of bitcoin? (Read 1633 times)

full member
Activity: 224
Merit: 100
September 27, 2011, 07:56:40 AM
#6
OP, how are you able to run a PHP script on an Arduino?

Run a PHP script on a webserver, and grab the webserver text to the arduino then display on LCD.

I have managed to connect the arduino directly to the ticker, but have to work out a way to parse the output more easily,
hero member
Activity: 726
Merit: 500
September 23, 2011, 09:18:10 AM
#5
OP, how are you able to run a PHP script on an Arduino?
hero member
Activity: 490
Merit: 500
September 23, 2011, 09:14:34 AM
#4
You'll need to do your own error checking; this isn't particularly robust.

Code:
function mtgox()
{
        $mtgox = file_get_contents("https://www.mtgox.com/code/data/ticker.php");
        if (!$mtgox) return 0;
        $mtgox = json_decode($mtgox);
        return $mtgox->ticker->last;
}
not ideal. I'll advice to use volume-based rate. In this script it calculates average volume-based rate for last 3 hours.
Code:
$ch=curl_init('https://mtgox.com/code/data/getTrades.php');
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($ch,CURLOPT_TIMEOUT,15);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
$headers = array(
//      "Host: mtdox.com",
        "User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30",
        "Accept: */*",
        "Accept-Language: en-US;q=0.8,en;q=0.4",
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ret=curl_exec($ch);
if (curl_errno($ch)!=0) {
    echo curl_error($ch);
    curl_close($ch);
    exit;
}
curl_close($ch);

$o=json_decode($ret,true);
if (!is_array($o)) exit("Invalid data");

$o1=array_reverse($o);
$start=$o1[0]['date'];
$end=$start-3*60*60;
$total_btc=0;
$total_usd=0;
$cnt=0;
for (reset($o1);list(,$v)=each($o1);) {
    if ($v['date']<$end) break;
    $total_usd=$total_usd + $v['price']*$v['amount'];
    $total_btc+=$v['amount'];
    $cnt++;
}

if ($total_usd <=0 || $total_btc<=0) exit("Zero volume");

$rate=$total_usd / $total_btc;

echo "Rate is $rate USD/BTC\n";

Also, mtgox has some 'vwap' key in ticker array. I don't know what exact meaning it has, but it is near to the value my script provides. Maybe for different time period.
ne1
member
Activity: 84
Merit: 10
September 22, 2011, 08:29:31 PM
#3
That sound cool, is love to have an arguing mtgox ticker.  I've got an arduino, Ethernet shield and an lcd.  Love to see where you get with this.
hero member
Activity: 588
Merit: 500
September 22, 2011, 07:44:25 PM
#2
You'll need to do your own error checking; this isn't particularly robust.

Code:
function mtgox()
{
        $mtgox = file_get_contents("https://www.mtgox.com/code/data/ticker.php");
        if (!$mtgox) return 0;
        $mtgox = json_decode($mtgox);
        return $mtgox->ticker->last;
}
full member
Activity: 224
Merit: 100
September 22, 2011, 11:41:54 AM
#1
Wheres a good place to return the current price of bitcoins. Im preferbly after a PHP script that just returns the price, or some TCP connection or somthing?

the reason is so i can use it to display the price on my arudino
Jump to: