DEV Community

Ayan Pahwa
Ayan Pahwa

Posted on • Updated on

Build a crypto miner using Raspberry Pi in 10 minutes

Mine Monero on Raspberry Pi

Lately I’ve been hearing a lot of hype around web3- cryptocurrencies, NFTs, DAOs, DeFi, GameFi and all of these cool jargons giving me a lot of FOMO (Fear of missing out), so I decided that I’m gonna try some of them for myself and see what’s it all about.

I tried a few things like buying some cryptos on an exchange, made a couple of web3 projects and joined a gazillion of discord channels doing the morning ritual of gm(good morning!) and responding to WAGMI(we all gonna make it!) but this one specific project called Monero caught my attention and I wanted to share it with the community here.

Before proceeding I want to put out that this is in no way an endorsement of the project or whole blockchain/web3 ecosystem in general, nor it’s a financial, get rich by mining cryptocurrency advice. This is in fact a fun project which you can build along to learn more about web3 and being a developer advocate, I’m also gonna use this project to share about the concept of :

Using a separate docker images for build-time and run-time within the same Dockerfile, we will see why it’s important and how to achieve that later in this post

Nevertheless, this project is kind of a fun to build, it allows you to mine cryptocurrency called XMR of Monero blockchain, profitable or not you can no doubt brag about mining cryptocurrency at your home to your friends, so let’s get started 😉

You can skip the inner details and jump straight to the build part down in the post if you like.

Background

Without going too much into the detail, for someone new into the web3 ecosystem - A blockchain is a distributed ledger which keeps records of every transaction happening over it. Just like how a bank keeps the record of who sent money to whom, the amount as well as the date and time, similarly in case of blockchain this immutable information is maintained within distributed blocks connected by a network.

blockchain

This validation of all the transactions on a blockchain is done by certain users who lend their compute power in the form of miners or validator nodes. There are also some programs called smart contracts which can run automatically on blockchain when certain conditions are met but that is out of scope for this blog so will not go into more details of smart contracts.

When you think of Bitcoin or Ethereum miners you might imagine a big server room consisting of massive GPUs or ASICs machines dedicatedly solving complex cryptographic problems sent to them by blockchain and once they solve it they earn rewards in the form of cryptocurrencies The process of successful submission to earn the rewards follows certain consensus mechanism which can vary depending on the type of blockchain

bitcoin miner farm

For example: Bitcoin blockchain works on a consensus mechanism known as Proof of Work (PoW), while Solana works on Proof of Stake (PoS) and Proof of History(PoH).
You can read all about types of consensus mechanism here

consensus mechanisms

If you’re feeling disheartened seeing the cost of these mining computers crushing your dream to have a mining rig of yourself, don’t be because the project we’re talking today called Monero allows or in fact encourages mining on CPUs, so even a small single board computer like Raspberry Pi 4 can become a miner to help validate the transactions and get the rewards in form of XMRs, the cryptocurrency of monero blockchain.

The philosophy behind this is since the cost and entry barrier to mine is pretty high, the miners are usually owned by few persons or organizations which may not be good for blockchain’s decentralization (no ownership and trustlesness by design), so monero optimizes to allow more and more people to contribute making their blockchain more decentralized. Monero also works on Proof of Work but instead of your small device solving a really complex cryptography puzzle it can join a mining pool with other devices to lend the compute power and together they can solve it fast and depending on how much your device contributed to the solution it’ll be rewarded suitably for that.

Is it profitable? Maybe not, given the device needs to be powered on and running but it surely is fun and maybe you can really earn if you add a lot of devices in your mining pool with good resources, say Rpi with 8 GB RAM; nevertheless it’s not financial advice so please do your due diligence :)

You can read all about Monero project, how much it pays for mining, costs associated, etc all on official website: here

Build


Hardware Needed

  • A single board computer like raspberry pi 4 (more ram the better).

The following SBCs were tested by my friend Lambros and this was the hashrate result:

- Raspberry Pi 3  - 20 H/s
- Raspberry Pi 4, 1 GB RAM - 45 H/s
- Raspberry Pi 4, 4 GB RAM - 99 H/s
- Nvidia Jetson Nano 2GB (without GPU enabled) - 62 H/s
Enter fullscreen mode Exit fullscreen mode

Hash rate(H/s) is the number of hashes a device can solve per second. I’ve not enabled CUDA(GPU backend) for Nvidia Jetson since monero encourages mining on CPU though if you want to give it a shot I’d love to see how it performs.

Other things you'll need are

  • SD card
  • Power supply
  • Connectivity to Internet via LAN or WiFi

Software needed

  • monero wallet (this is where you’ll get the rewards) download from https://www.getmonero.org/downloads/
    Information of mining pools : as mentioned earlier we will be joining a mining pool, the address of active mining pools can be found by a simple google query, it’s better to use one near you ex: google search monero mining pools in Los Angeles. Each mining pool has their own threshold after which they start paying out to the wallets.

  • balenaCloud account to manage miners

  • balenaEtcher to flash the SD card

Deployment

The easy way (deploy with balena)

Sign up for a free balenaCloud account. Your first ten devices are free and fully-featured! Then use the button below to create and deploy the application:

deploy button

Note: I have used a Raspberry Pi 4 in the image below but be sure to select the correct device type for the device you are using.

  • Select the Device

  • Add OS type: Production vs Development

  • (optionally) add in your home WiFi credentials, download and flash the OS to SD card using etcher

The Advanced way

If you are already a balena user it might be better for you to use this way. You can clone the project from this github repo and use the balena CLI command

balena push <fleet_name>

to push the application to your devices in the fleet created on dashboard. This is the best option if you want to tinker with the project and have full control. The Getting Started Guide covers this option. After you've created the application and pushed the code using the CLI, follow the steps below.

First device boot and configurations

When the device boots for the first time, it connects to the balenaCloud dashboard, after which you’ll be able to see it listed online. In the meanwhile we need to Install Monero wallet on our computer where we will be getting the mining rewards

boot the pi

Software Setup

Download the Monero wallet from https://www.getmonero.org/downloads/ GUI or CLI as per your preference. I recommend using a GUI wallet and create a new wallet using Simple Mode.

Image description

Once the wallet is created you’ll have a unique wallet address copy that to clipboard and head over to:

balena cloud > your device > Device Variables

tab and add the following

Device Variables

VARIABLE NAME VALUE CHANGE TYPE DESCRIPTION
WALLET_ADDRESS from last step Must add This is the wallet address where you’ll earn your mining rewards in the form of XMRs which you can trade on any crypto exchange
MINER_POOL Default value is: http://xmr.2miners.com:2222/ Optional This is the miner pool you will join by default. You can change this to another miner pool by searching addresses of miner pools on Google. The one near to your location will be good. For ex:

This will restart the container and your miner will be registered to the mining pool and start getting the jobs. All the rewards will be sent to your monero wallet after your device meets the threshold of the miner pool.

Monero Mining logs on balenaCloud

Now as I mentioned above this may not be profitable at all but it's definitely fun and let’s look at some totally unrelated things that we're gonna learn here.

Container Learnings

Here is the project’s Dockerfile.template. If you take a closer look at it, you’ll see there are two different base images being used. One of them is a build image and other one is the run image

Why we do that is simply because we don't want our run image to be bloated with extra packages which were needed to build a source code. So we conduct the build in a separate image and copy the artifacts or binaries in the run image using

COPY --from=build /usr/src/app/xmrig/build/xmrig /usr/local/bin
Enter fullscreen mode Exit fullscreen mode

This way our main image is minimal and less bloated. All of the balena base images are available as build and run. The build image has additional packages such as gcc, build-essential which are needed to build from source whereas the run image is minimal. You can read more about it here.

So this was the container lesson from this blog. Now I hope you do use this project to dip into the world where IoT edge computing meets web3 and as always if you have any suggestions, feedbacks or questions, please write on balenaForums or any social media channel. I'll be back with another interesting project and lesson soon 🍻


Attribution

===

Discussion (0)