Crypto Status for Onion’s Omega2+

Standard

A long time ago on the magical world of this blog, and the opinion cloud  Twitter – I teased this photo:

Well since that photo I had the folks at Onion hit me up:

I did and today, after much waiting, a finally fixed up Onion (Wifi Problems) I’m tonight online and have finalized the guide! Without further backstory… Here’s the quick and dirty guide to getting it running:

Step 1: SSH In.

Get your self all ssh’d in and get ready for some copy and paste magic first off the bat is some key elements some may already be installed so any errors you hit saying “Up to date” or “Already Installed” are safe to ignore – here’s the one-liner:

opkg update; opkg install php7-mod-openssl php7-mod-json php7 php7-cgi php7-cli bash

This will handle everything you’ll need based upon some PHP trial and error no php.ini mods are needed for PHP, just install and you’re good to grab the feeds and get things rolling.

Step 2: A sprinkle of BASH

Grab the sh file, and for this instance save it as: coins.sh – Download it here, or copy and paste below.

This is not required, but it is what I will reference this file as moving forward, so if you change it, run the change through the entire process.

[UPDATE]: If you are just wanting general coin prices? Set all values to 1, it will simply return the value of a single coin if you’re just monitoring the market in general.

#!/bin/bash

## Set Your Currency
curr=$'USD'

## Coin Amounts
ltct=$'200'
btct=$'200'
etht=$'200'
dasht=$'200'

## Clear OLED Screen
oled-exp -i

## Create Values
btc=$(php-cli coins.php 'BTC' $curr $btct)
eth=$(php-cli coins.php 'ETH' $curr $etht)
dash=$(php-cli coins.php 'DASH' $curr $dasht)
ltc=$(php-cli coins.php 'LTC' $curr $ltct)

## Display on Screen
oled-exp write "CURRENT VALUE\nLTC: $ltc\nBTC: $btc\nETH: $eth\nDASH: $dash"

Basics, set your currency I’ve got mine as USD for the US Dollar, however, you may be elsewhere in the US, just put in your currency type and you’re off!

The second edit is your coin types and amounts – I didn’t do anything fancy here, just be sure anything you add is added to the value calls in Create Values, the format is:

VARIABLE FOR OUTPUT=$(php-cli coins.php ‘COIN TYPE‘ $curr $COINAMOUNT)

Lastly is the display to the screen, and you can format this as you see fit, but the \n is a new line, so anywhere you want a linebreak add a \n.

Step 3: A pinch of PHP

This is the PHP side of things which grabs the data, name it coins.php – Download it here, or copy and paste below.

<?php

/****
coins.php (v.04a) by Chuck Brown (http://www.zagnu.com/coin-source.txt)

A simple API call to get the value of a coin in the user's desired currency, great for using in Google Sheets (Use: =IMPORTDATA("yoururl") or if you simply want to grab
the current value of a certain coin. Currently geared towards USD, but hope in next version to add proper currency markings for the amount conversion.


## Changelog

.01 -   First Release
.02 -   Adjustments to code & calling methods.
        Changed from XML output to plaintext for use of IMPORTDATA vs IMPORTXML
.03 -   Removed dual posting of plaintext price for import & added amount conversion.
        Commented code.
.04 -   Added Conversion for ATM without Value Text for Onion pull - Also adding $ for easier formatting on Onion curl call.
.04a -  This is the Omega2+ version, can then be gathered from CLI vs. Installing Apache.
***/


//Get Data
$url = file_get_contents("https://min-api.cryptocompare.com/data/price?fsym=".$argv[1]."&tsyms=".$argv[2]."");
$json = json_decode($url,true);
//Check for Amount to Convert
if ($argv[3] != '') {
$value = $argv[3] * $json[$argv[2]];
$tvalue = round($value,2);
echo "$".$tvalue;
} else {
//no amount? provide plaintext response for dollar amount.
print $json[$argv[2]];
}
?>

Now if you look at the line that reads:

echo "$".$tvalue;

The $ is your currency format, so if your not using the USD as a default, just make a quick tweak and set it to yours.

I do have a slightly more up-to-date version I’m toying with on the PHP side, with configuration options up top for easy configuration & coins to take some of the bash config out of the equation and use an array to generate, but again still a very early release.

Step 4: Schedule with Cron

This can be run fairly regularly, however, be mindful if you are wanting to say lookup values every 2 minutes, Cryptocompare.com is going to block you. So every 30 works for me:

*/30 * * * * 

However if you want to do something a tad different, work on it with Crontab.guru. Handy tool, but I give that first so you can figure out your schedule then on the command line here’s what you need to do:

chmod +x /root/coins.sh
crontab -e
[PASTE in your schedule] /root/coins.sh
:x! {PRESS ENTER}

Next with that in place simply restart cron:

/etc/init.d/cron restart

And boom, you are off and running. Now be aware that cron will take and pick up at the next window, so let’s say you used my example and said every 30 minutes? Well if it’s 11:45 PM like it is now, it won’t run again until midnight. If you are before 11:30 PM it’d run at 11 PM – pretty simple.

Wrapping up!

Again probably not the cleanest code, or the most elegant, but it does a job, and has given me a nice little readout on coin values. I’m open to any and all comments and suggestions, happy to give credit where credit is due and post updated pastes and post info as improvements are contributed!

Thanks to Onion for a great little device! Quite handy indeed!

Leave a Reply