This tutorial is part of a series of articles introduced here.
In part 6, we finalized the configuration of the private blockchain. We sent ether between our nodes and verified that the database is properly synchronised.
NB: It is important that what we mean by “private Ethereum blockchain” in this context has absolutely nothing to do with the “private blockchain” championed by Hyperledger, Eris/Monax, or the recently announced Enterprise Ethereum Alliance. These are different codebases with different client applications, so they correspond to different implementations of a different set of concepts. They are private in the sense that they limit who has access to their chain. They are also called permissioned blockchains, and to be perfectly transparent, we wish they were not even called blockchains at all. In this series of articles, what we call “private blockchain” is a private instance of the Ethereum implementation. Same code base, same client application, different network identifier and genesis block. In other words, what we will come to create in this series is a chain with the same rules as the main chain, the same consensus algorithm too, but a different root block. This will become clearer in part 3. For more information about the 3-layer model and differences between concepts, implementations and instances, you can also watch our Devoxx talk and read this article.
So far, we only worked with pure software. In this final part, we will connect our blockchain with the physical world and connect some wires.
The goal of this tutorial is to check whether you are allowed to use a service. By using a button and two LEDs (green and red), we will be able to check the Smart Contract (SmartToken) to retrieve the number of tokens assigned to the user account (identified by its address).
To complete this tutorial, we will use the following solutions:
- Hardware: A breadboard, 2 LEDs (green and red), a button, 4 resistors (2 x 220Ω, 1kΩ, 10kΩ), some wires
- Software: Node.js and two packages (Web3 and OnOff)
The sample was inspired by the post available here.
It’s time to build some electronics.
Step 1 – Start with some electronics
Before you proceed, ensure your RPi is turned off.
The goal is to connect a LED to the GPIO17 port of the RPi.
The schematic is the following:
Here below you can see the breadboard that shows how to physically connect the electronic parts:
The GPIO17 port (port 11) is linked to the anode pin (positive) of the led.
The cathode pin (negative) of the led is connected to the resistor which is linked to the GND port (port 6) of the RPi.
Ensure to respect the polarity of the LED (check here).
When you have finished, you can power on the RPi.
Step 2 – Connect to your RPi
Use the SSH command to connect to your RPi as illustrated here below:
computer$ ssh pi@192.168.1.31
Step 3 – Start nodes
Before deploying the Smart Contract, let’s start our nodes.
Start the miner #1:
computer$ cd ~/ChainSkills/miner1 computer$ ./startminer1.sh
Start the miner #2:
computer$ cd ~/ChainSkills/miner2 computer$ ./startminer2.sh
Start the RPi node:
pi$ cd ~/ChainSkills/node pi$ ./starnode.sh
Step 4 – Prepare the RPi
This step describes all the operations to perform on your RPi in order to set up all the pieces of software.
Step 4.1 – Install Node.js
We will use Node.js to run the script that interacts with the Smart Contract.
To install Node.js 7.x on your RPi, please follow the instructions illustrated here below:
pi$ curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash - pi$ sudo apt-get install nodejs pi$ node -v v7.4.0 pi$ npm -v 4.0.5
Step 4.2 – Install Git
Git will be required for the installation of Web3:
pi$ sudo apt-get install git
Step 4.3 – Prepare the project folder
pi$ mkdir -p ~/Projects/ChainSkills/SmartToken
Step 4.4 – Install Web3 module
The Ethereum Web3 package is a node module that provides a JavaScript API to interact with the Ethereum blockchain.
This module will be required to allow the JS script to fetch the status of the Smart Contract.
Install the Web3 module. We are installing an earlier version because the 1.0 beta version has compatibility issues.
pi$ cd ~/Projects/ChainSkills/SmartToken pi$ npm install web3@0.20.1
Step 4.5 – Install OnOff module
The OnOff module is used to easily interact with GPIO pins of the RPi and set them On or Off.
pi$ cd ~/Projects/ChainSkills/SmartToken pi$ npm install onoff
Step 5 – Create the client application
Let’s create a JS script the called “smart_token.js“.
This script will be used to:
- Interact with the Smart Contract
- Interact with RPi
Create the file “smart_token.js”:
pi$ cd ~/Projects/ChainSkills/SmartToken pi$ nano smart_token.js
Copy and paste the following code:
// Interaction with GPIO var Gpio = require('onoff').Gpio // Interaction with Ethereum var Web3 = require('web3') var web3 = new Web3() // connect to the local node web3.setProvider(new web3.providers.HttpProvider('http://localhost:8042')) // The contract that we are going to interact with var contractAddress = '0xe1d76d2bffc600333248343f7a1d144edaef29a2' // Define the ABI (Application Binary Interface) var ABI = JSON.parse('[{"constant":false,"inputs":[{"name":"recipient","type":"address"},{"name":"value","type":"uint256"}],"name":"depositToken","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"recipient","type":"address"}],"name":"getTokens","outputs":[{"name":"value","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"recipient","type":"address"},{"name":"value","type":"uint256"}],"name":"withdrawToken","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"status","type":"uint256"}],"name":"OnStatusChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"OnValueChanged","type":"event"}]') // contract object var contract = web3.eth.contract(ABI).at(contractAddress) // components connected to the RPi var greenLed = new Gpio(14, 'out') var redLed = new Gpio(15, 'out') var button = new Gpio(18, 'in', 'rising') // display initial state showStatus() // watch event on the button button.watch(function (err, value) { if (err) { throw err } showStatus() }) // wait for an event triggered on the Smart Contract var onValueChanged = contract.OnValueChanged({_from: web3.eth.coinbase}); onValueChanged.watch(function(error, result) { if (!error) { showStatus() } }) // power the LED according the value of the token function showStatus() { // retrieve the value of the token var token = contract.getTokens(web3.eth.coinbase) // display the LED according the value of the token if (token > 0) { // Green: you have enough token redLed.writeSync(0) greenLed.writeSync(1) } else { // Red: not enough token greenLed.writeSync(0) redLed.writeSync(1) } } // release process process.on('SIGINT', function () { greenLed.unexport() redLed.unexport() button.unexport() })
This script has to be changed according to your environment.
First, change the value of the variable “contractAddress” with the address of the contract deployed on your private blockchain (see part 6).
Then, provide the ABI of your SmartContract (see part 6) by replacing the content of the variable “ABI“.
Step 6 – Run the client application
Run the client application:
pi$ node smart_token.js
The client application will wait for a change to occur on the Smart Contract.
Step 7 – Test the application
Test your application using Mist by depositing tokens to the coinbase address of the Pi node.
You should see the appropriate LED displayed according the remaining amount of token.
You can also press the button to get the number of tokens.
Summary
Congratulations!
You have successfully completed this series of tutorials.
Of course, this is just a glimpse of what you can do with Ethereum but you had the opportunity to experiment some part of this technology.
Now, it’s up to you to move forward with the journey and to imagine other kinds of Smart Contracts and how to link them with IoT devices.
Shameless plug
Now that you are done with this series, we just wanted to let you know that we are currently preparing a full-blown online training about the development of distributed applications on Ethereum and we are looking for your feedback to figure out what you would like to see in this training. If you want to help us, you can take a few minutes to answer a survey here.
And if you just want us to keep you informed when the full online training program will be available, you can register to our mailing list on beta.chainskills.com.
I’m getting this error when I try to connect to my node via geth and port :8042
I am trying to build a javascript based dashboard.
Uncaught Error: CONNECTION ERROR: Couldn’t connect to node http://localhost:8042.
at Object.InvalidConnection (
Hi Charles
Sorry for my late answer.
I missed your question 🙁
Did you fixed your problem or do you still need some help?
Best
Said
Nice work!
Can you tell me how I can store data in the chain? Or on a predefined path on the RP? Is it possible to use it with your example?
Hi JJ
Thanks for your feedback.
Regarding the data, you can store them on the contract depending on their size.
But it’s not a good practice to store large volume of data in smart contract because you will have to pay for that.
But if you have some values to store (some status, temperature, etc.), of course, you can save them within your contract.
I would not recommend storing a path in a contract because this path will be only valid in your computer while the nature of a contract is to be used on any node of your the network.
My recommendation would be to store these files in a distributed and decentralized file storage such as IPFS (https://ipfs.io/). For example, your client application could store a file on IPFS, get its hash key and store the hash key in your contract.
To read the file (from any node connected to your network), you will have to read the contract, get the IPFS hash key and to retrieve the file on IPFS through this hash key.
I plan to post a sample tutorial describing how to integrate Ethereum and IPFS.
Best,
Said
Nice Work Said!,
Ive encountered a problem with the smart smart_token.js file, whenever I try to run it on the pi, it shows this error ” var token = contract.getTokens(web3.eth.coinbase)
^
TypeError: contract.getTokens is not a function”
Any assistance on this would be greatly appreciated!
Hi KK
Thanks for your feedback 😉
About your issue, this problem may happen if:
– the node is not running
– the contract is not properly deployed
Can you try to print the content of “contract”, like this:
…
// contract object
var contract = web3.eth.contract(ABI).at(contractAddress)
console.log(contract)
…
Thanks for the reply Said,
I’ll have a look later on. I changed the “var contract = web3.eth.contract(ABI).at(contractAddress)” to “var contract = new web3.eth.Contract(ABI, contractAddress)” to bring it in line with due to using the new web3 1.0.0 beta (Found here: https://ethereum.stackexchange.com/questions/22983/web3-eth-contract-is-not-a-function-when-making-contract), which may be the problem.
Do you know if there a way to roll back web3 to a version where “var contract = web3.eth.contract(ABI).at(contractAddress)” would work?
Really appreciate the help Said,
Many thanks.
Ok I got it!
You can use the following command:
npm install web3@0.20.1
Could tell me if that’s solve your problem?
If yes, I will update the page.
Hi Said.
I’ve just tried it now, and that has made it work! Brilliant. Thank you so much for your help!
Hi KK
Great !!!
I have updated the post to reflect this change.
Thanks for your support.
hi Said,
Your tutorial is really informative, all six parts.
I have a question…
I am just starting on the blockchain, bitcoin etc… I read ‘Mastering BitCoin…2nd Edition’ from O’reilly to start with and some articles as yours from the internet.
I want to build my own little blockchain network using two RaspberryPi. I am planning to have two C++ programs running on each RaspberryPi, this C++ program will generate some data. This data becomes payload for the next block. One of the two RaspberryPi will mine it, add it to its blockchain and publish the new block, the other one will accept and add to the chain.
My question is
1. Is it possible to do with libbitcoin on RaspberryPi? With libbitcoin, I have the impression that I cannot put arbitrary type of data in the block.
2. Or is there ethereum lib available in C/C++ that I can use on RaspberryPi?
Thanks in advance for any guidance.
aj
Hi aj
Thanks for your feedback.
To be honest, I’m not familiar with programming for the Bitcoin blockchain.
About C/C++ libraries, you can check the C/C++ implementation of Ethereum: http://www.ethdocs.org/en/latest/ethereum-clients/cpp-ethereum/
Cheers
Said
hello said, its amazing to see how good is this tutorial, I need your help to control servo along with the led, as a user send 1 token , servo motor should also rotate by 90 degree, can you help me please.
Mr Eloudrhiri, what is the function of the button in this circuit? Currently our circuit functions as yours does in your video demo, however we do not understand what the button is supposed to do. When we press it, nothing happens, and in trying to understand the logic of the circuit layout it still isn’t clear how the button should effect the circuit output. Can you please clarify if you have a chance? Thank you so much for the tutorials btw…this is fantastic!
Said,
Thank you for your demonstration! I was wondering if you have completed a tutorial involving Ethereum and IPFS? I would really like to experiment with IPFS but am not sure how!
Brittany