How to do Bitcoin mining on the Raspberry Pi and what not to do.

So, I’ve been busy lately with, among other things, repurposing my Raspberry Pi for Bitcoin mining. Yet, in spite of a proliferation of guides on how to do Bitcoin mining on the Raspberry Pi, I struggled a bit with getting it all setup. So, while this is mostly about getting it all setup, this article is as much about the pitfalls to avoid.

First of all, it should be noted that Bitcoin, capitalized, is the concept, the network and associated items (such as the protocol). However, bitcoin, not capitalized, is the currency itself. You can obtain bitcoins using the Bitcoin network to mine bitcoins. Mining is the act of solving mathematical equations that are super-complex and comparing the result with current Bitcoin blocks in order to add more bitcoins to the system. Over the years, the complexity has become increasingly difficult, so the chances of getting a real return on your investment is almost impossibly small.

So, this is not a get-rich guide by any means. The increasing difficulty in mining also is part of the reason that competitors to Bitcoin have sprung up. However, if anyone has doubts about Bitcoin, then how much more the alternatives? So, I’m not going to get into all of these alternatives, and I want to stress that this is more about learning about Bitcoin and how it works as well as putting a spare Raspberry Pi to use.

My Pi has done quite a few good things. It originally streamed video from a security camera to Justin.tv before they got stupid and ruined the protocol. It was then repurposed to a scan and print server. During my move, my AIO took a nosedive onto the concrete. Since most printers come wifi enabled these days, it didn’t make sense to not purchase one without it. So, my lonely Pi sat in a closet gathering dust for a few months.

So, I came across it one day, and I decided it was time to do something with it. Why not put it to use in its earning its keep instead? Well, in actuality, that probably will not happen, but it is an interesting exercise nevertheless.

I looked up online tutorials such as “How to Create a Raspberry Pi Bitcoin Miner“, which mostly worked. However, I discovered two problems right away which were supremely annoying.

First and foremost, not all USB hubs are created equal. I initially put out a decent sum of money for one that is supposedly Pi compatible only to find out I could not even get a USB keyboard and mouse to work off of them! I later read that you should avoid at all costs those USB 3.0 hubs, as they are not always backwards compatible (and that in spite of even the guide I read having that very hub on it!). Before reading that, I ordered one from Pi Hut, thinking that at least it would be Pi compatible. Well, it was, but the plug right away told me there would be issues, as it is obviously built for 220V and not US 110V.

BTW, a good article on this is “Which USB hub to use with Block Erupters | NanoFury NF1 | BPMC Red Fury | Ant U1“. I ended up getting a Rosewill 10-Port High Speed USB 2.0 Hub (RHB-500) in case you are interested.

The other aggravating thing was that I could not find a decent way to autostart bfgminer. In theory, it should be easy enough, but the Pi uses LXDE, and that is difficult enough. Worse, it looks like bfgminer simply does not like running in the background.

So, there are Pi environments that supposedly are mining friendly. I found out about Minepeon. It looked very promising, but it obviously is very outdated. Worse, it is broken. I tried updating the system, and Arch was so out of date that it corrupted the card doing updates — twice even.

Now for a rant. I read “The Rokos Core OS Turns Your Raspberry Pi Into A Bitcoin Node” with great enthusiasm, as it sounded like the ideal environment. It is not. It is just a stupid desktop environment with a Bitcoin wallet already installed and a Bitcoin wallpaper. The Rokos home page lies when it tells you “Just Burn and Click to Start your own Full Bitcoin Node or Client”. Don’t waste your time. There is no mining software to be found.

So, what I winded up doing (after setting up ssh and VNC)  is killing the silly Rokos desktop and configuring the Pi to boot up in commandline mode. Make sure to choose autologin for the pi account.

Then, I installed cgminer. I had to compile the code, which meant searching for libraries, but it actually did not take very long. The main stumbling point was having to enable the Nano Fury ASIC. The documentation says all ASICs are enabled by default, but that is referring to the binary and not the makefile!

After that, however, I was able to see the Nano and do some mining. It still had to be run under sudo, so make sure your pi account does not need a password to run sudo (and make sure you don’t do anything else requiring security or privacy on your Raspberry Pi, as that is a huge security risk).

This example came from the original tutorial where I setup an account on Slush Pool (you would substitute login info where it says “username” and “password”, obviously):

sudo ./cgminer -o stratum.bitcoin.cz:3333 -u username.worker1 -p password

It is important that once you get your setup running that you save it to a configuration file. It makes life so much simpler and more secure (although the last point is arguable, as the only thing they can steal is credentials for adding bitcoins to your account rather than withdrawing them).

Next, create a file in your /home/pi/bin directory called bitmine.sh:

#!/bin/bash
 
ps -ef | grep cgminer | grep -v grep
RUNNING="$?"
if [ "$RUNNING" == "1" ]
then
        echo "Starting bitcoin mining..."
        sudo cgminer
else
        echo "Bitcoin already running, exiting script"
fi

Make sure to chmod the file as executable (chmod a+x ~/bin/bitmine.sh), else it will not work as expected.

Next, drop bitmine.sh into the end of your .profile file, and it will run every time you login. It will check to see if mining is already running and if not start doing so.

Reboot and test.

The nice part is that you can still ssh into the Pi and even run VNC as normal.

It works well enough that I ordered a couple more Nanos. I figure that with 4 Nano Fury sticks, I should be able to do my first cash out in 5 years. 🙂

Obviously, this was an interesting exercise but not exactly a profitable one, at least monetarily. So, I figured this article would not be complete without mentioning some other alternatives.

Naturally, you could buy bitcoins by finding an exchange and trading dollars for them. Like all currency, you have to watch the exchange rate for the opportune time to do so. For example, yesterday the rate was about $450 per bitcoin, while today it seems to be down to almost $400. Last week, it had dipped even below that, so it is, just like all currencies, a bit of a gamble. However, I suspect that you are reading this article because you want to learn more about Bitcoin mining on the Raspberry Pi and not because you want to learn about purchasing bitcoins. As with all currency exchanges, check the fine print for transaction fees and such.

You can go out and spend a lot of money on one of those large miners, but I still question whether or not you will get your investment back. In theory, you could, but the initial cost of some of these units is $1,000 or more! That will take a lot of mining! I’m not sure whether these units require a computer or not, as I just don’t feel that into it to give it a try.

Of course, others have purchased them, and some of them sell mining contracts where you purchase a specific block of time for their mining efforts. Initially, this sounds like a win-win situation. They get back their investment in hardware and electricity, and you get bitcoins. However, I soon became wary of such ventures when I calculated the amount that could be made from the mining and compared it to the bitcoin exchange rate. Now, I understand there is some overhead, and I understand that this is a service that is being paid for, but when the amount is double or even triple the amount that could be exchanged for bitcoins, I certainly balk. After all, the reason I would (and I assume anyone would) go this route is for the investment opportunity, but a true investor isn’t going to be overly willing to spend 3 x the amount it is worth to purchase the item, right? What am I missing here?

Now, I can see where a limited contract of this type might be advisable. If you are really, really close to cashing out on a pool and want to hasten it just a tad, then perhaps this makes more sense. As a long-term strategy, though, I just cannot see it.

Similarly, there are online companies that you can purchase similar contracts. However, their rates are actually no better than those on eBay. Furthermore, some have turned out to be nothing more than Ponzi schemes where you put out your money and wind up with nothing in return. At least on eBay you can check their reputation against how long they have been a member and judge whether or not they are trustworthy. An online company can hide their true nature a while longer.

So, I want to stress this is not a get-rich-quick scheme. You are better off just getting a job and working for your money the old-fashioned way. 🙂 There are alternative currencies of course, but then their stability and reliability have to be taken into account. At very least, Bitcoin is an established alternative to traditional currencies, whereas the others struggle to have the reach and influence that Bitcoin has.