Saturday, June 4, 2022
HomeElectronicsGood Residence Resolution utilizing Good Contract

Good Residence Resolution utilizing Good Contract


Remark errors or corrections discovered for this circuit, and get the prospect to win massive!

Good dwelling options allow controlling {the electrical} gadgets resembling lights, followers, water heater, water pumping motor, kitchen equipment and so forth. Good contracts work on Blockchain and management these gadgets. On this mini-project, a peer to look (P2P) community is constructed with two GCP (Google Cloud Platform) primarily based VM(Digital Machine)s and one RPI(Raspberry) primarily based methods. These two VMs are employed as miners and approve the transactions from the miner1 to RPI. RPI is linked with a relay which in-turn activates and off a light-weight. A sensible contract is deployed on Blockchain via Web3.0 and controls the sunshine by transactions resembling depositing the tokens and withdrawing the tokens. This mini-project entails applied sciences resembling Blockchain, IOT, Cloud computing and Web3.0.

Figure1: Architecture diagram
Fig. 1: Structure diagram

The next growth environments are required:

  1. Google cloud VMs (2nos) for working miners (Java script)
  2. Raspberry PI as a Blockchain node (Java script)
  3. Truflle Setting to construct Good Contract (Solidity)
  4. Management the sunshine from Miner1 via RPI (Java Script)
Figure 2: Google Cloud Platform VMs
Fig. 2: Google Cloud Platform VMs

Pre-requisites

//Launch atleast 2 VMs (35.247.47.68, 34.105.65.4)
//Set up the next in these 2 VMs
sudo apt-get set up software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get replace
sudo apt-get set up ethereum
geth model
//configure winscp to switch information

Configure Google cloud VMs for working miners

1.1. Create folders
cd ~/
in vm1,
mkdir -p ~/RPI/miner1
in vm2
mkdir -p ~/RPI/miner2

1.2. Create Genesis block – genesis.json underneath ~/RPI

{
 “nonce”: “0x0000000000000042”,
 “mixhash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
 “issue”: “0x400”,
 “alloc”: {}, 
 “coinbase”: “0x0000000000000000000000000000000000000000”,
 “timestamp”: “0x00”,
 “parentHash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
 “extraData”: “0x436861696e536b696c6c732047656e6573697320426c6f636b”,
 “gasLimit”: “0xffffffff”,
 “config”: {
    “chainId”: 42,
    “homesteadBlock”: 0,
    “eip155Block”: 0,
    “eip158Block”: 0
 }
}

1.3. Initialize miner #1
open cmd with admin

cd ~/RPI
geth –datadir miner1 init genesis.json

1.4. Initialize miner #2
open cmd with admin

cd ~/RPI
geth –datadir miner2 init genesis.json

1.5. Accounts for miner #1
default account for the miner #1

geth –datadir miner1 account new
Tackle: {0xFAeC65D82c950b9f21e46c506c359a74ecA5C9C9}
pw: blockchain
Add a further account for testing function:
geth –datadir miner1 account new
Tackle: {0x10dbd79D08bA50bc0Cc1D5D00F2b68D97cf72870}
pw: blockchain
record the accounts
geth –datadir miner1 account record

1.6. Accounts for miner #2
default account for the miner #2

geth –datadir miner2 account new
Tackle: {0x646634c4A86af893a57D4c8796b8747c3845000a}
pw: blockchain
Add a further account for testing function:
geth –datadir miner2 account new
Tackle: {0xAFa0Fd7b8f2B6220Ec618b7B517728EF5E21123D}
pw: blockchain
record the accounts
geth –datadir miner2 account record

1.7. Miner #1: setup
Put together password.sec file underneath ~/RPI
with content material = blockchain
Add the next to startminer1.sh

geth –identification “miner1” –networkid 42 –datadir “~/RPI/miner1” –nodiscover –mine –rpc –rpcport “8042” –port “30303” –allow-insecure-unlock –unlock 0 –password “~/RPI/password.sec” –ipcpath “~/.ethereum/geth.ipc” –rpcapi “db,eth,internet,web3,private,miner,admin,txpool,debug”
sudo chmod +x startminer1.sh
sh startminer1.sh
test error with > ps -ef | grep -i geth
To connect
open separate cmd window
sudo geth connect “http://localhost:8042”
miner.begin()
after a while,
miner.cease()
eth.blockNumber
//Observe few blocks are mined
cease miner by ctrl+c
To establish working miner:
ps aux | grep geth
kill -INT procid

1.8. Miner #2: setup
Put together password.sec file underneath ~/RPI
with content material = blockchain
Add the next to startminer2.sh

geth –identification “miner2” –networkid 42 –datadir “~/RPI/miner2” –nodiscover –mine –rpc –rpcport “8043” –port “30304” –allow-insecure-unlock –unlock 0 –password “~/RPI/password.sec” –rpcapi “db,eth,internet,web3,private,miner,admin,txpool,debug”
sudo chmod +x startminer2.sh
sh startminer2.sh
test error with > ps -ef | grep -i geth
To connect
open separate cmd window
sudo geth connect “http://localhost:8043”
miner.begin()
after a while,
miner.cease()
eth.blockNumber
//Observe few blocks are mined
cease miner by ctrl+c
To establish working miner:
ps aux | grep geth
kill -INT procid

1.9. Ship ethers inside miner #1

//Begin miner1
sh startminer1.sh
geth connect “http://localhost:8042”
//default account
eth.coinbase
//all accounts
eth.accounts
eth.getBalance(eth.coinbase)
eth.getBalance(eth.accounts[1])
//get steadiness in ether
web3.fromWei(eth.getBalance(eth.coinbase))
//cease mining
miner.cease()
//ship ethers
eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], worth: web3.toWei(10, “ether”)})
eth.pendingTransactions
//Observe one transaction is pending
//begin miner
miner.begin()
eth.pendingTransactions
//Observe null array
web3.fromWei( eth.getBalance(eth.accounts[1]))
//It has to show 10
//cease miner
miner.cease()

1.10. Ship ethers inside miner #2

//Begin miner2
sh startminer2.sh
geth connect “http://localhost:8043”
//default account
eth.coinbase
“0x646634c4a86af893a57d4c8796b8747c3845000a”
//all accounts
eth.accounts
[“0x646634c4a86af893a57d4c8796b8747c3845000a”, “0xafa0fd7b8f2b6220ec618b7b517728ef5e21123d”]
eth.getBalance(eth.coinbase)
eth.getBalance(eth.accounts[1])
//get steadiness in ether
web3.fromWei(eth.getBalance(eth.coinbase))
//cease mining
miner.cease()
//ship ethers
eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], worth: web3.toWei(10, “ether”)})
eth.pendingTransactions
//Observe one transaction is pending
//begin miner
miner.begin()
eth.pendingTransactions
//Observe null array
web3.fromWei( eth.getBalance(eth.accounts[1]))
//It has to show 10
//cease miner
miner.cease()

1.11. Pair the miners
cd ~/RPI
First, begin each miners (miner #1 and miner #2).

//Begin miner1
sh startminer1.sh
//from one other cmd window with admin 
geth connect “http://localhost:8042”
admin.nodeInfo.enode
//be aware down enode handle
“enode://f9d48547c373fd61124d110791168572382298979d18ab5d6314cd399538ef77b[email protected]35.247.47.68:30303?discport=0”

//Begin miner2
sh startminer2.sh
//from one other cmd window with admin 
geth connect “http://localhost:8043”
admin.nodeInfo.enode
//be aware down enode handle
“enode://991f8279ae27e665151621a9940b42e208bb7cda0108c3cda219059bed28fbb7e[email protected]34.105.65.4:30304?discport=0”

1.12. GCP Firewall settings

In GCP, prime navigation->VPC Community->Firewall
Identify=ethereum
Ingress
Tag identify=miner
IP vary=0.0.0.0/0
TCP = 30303,30304
UDP = 30303,30304
Create
In VMs, Edit VM
Community tag identify = miner
Save VMs
Re-start VMs

1.13. Create static-nodes.json by including these enodes
[
“enode://f9d48547c373fd61124d110791168572382298979d18ab5d6314cd399538ef77b[email protected]35.247.47.68:30303?discport=0”,
“enode://991f8279ae27e665151621a9940b42e208bb7cda0108c3cda219059bed28fbb7e[email protected]34.105.65.4:30304?discport=0”
]

//copy static-nodes.json into the folders
in vm1,
/dwelling/digitransolutions7/RPI/miner1
in vm2
/dwelling/digitransolutions7/RPI/miner2
//exit each miners and once more re-start miners
//from connected home windows, test peer standing
admin.friends
//Observe each miners are paired.

1.14. Ship ethers between nodes of the non-public blockchain

//from each cmd window attachments,
miner.begin()
//Ship ethers from Miner #1 to Miner #2
//From miner2 connected cmd window,
//test miner2 account
eth.accounts[1]
“0xafa0fd7b8f2b6220ec618b7b517728ef5e21123d”
//test miner2 steadiness
web3.fromWei(eth.getBalance(eth.accounts[1]))
//observe steadiness=10
//From the Geth console connected to the miner #1
eth.sendTransaction({from: eth.coinbase, to: “0xafa0fd7b8f2b6220ec618b7b517728ef5e21123d”, worth: web3.toWei(10, “ether”)})
//from miner1 attachment, test the steadiness of miner2 account
web3.fromWei( eth.getBalance(“0xafa0fd7b8f2b6220ec618b7b517728ef5e21123d”))
//from miner2 attachment, test the steadiness of miner2 account
web3.fromWei( eth.getBalance(eth.accounts[1]))
//now steadiness = 20

1.15. ship 2 ethers between the next accounts:

miner #2(eth.accounts[1]) -> miner #1(eth.accounts[1])
//from each cmd window attachments,
miner.begin()
//From the Geth console linked to the miner #1
eth.accounts[1]
//”0x10dbd79d08ba50bc0cc1d5d00f2b68d97cf72870″
//test steadiness
web3.fromWei(eth.getBalance(eth.accounts[1]))
//0
//From the Geth console linked to the miner #2
eth.sendTransaction({from: eth.accounts[1], to: “0x10dbd79d08ba50bc0cc1d5d00f2b68d97cf72870”, worth: web3.toWei(2, “ether”)})
//Error: authentication wanted: password or unlock
//From the miner #2, unlock the account #1 with its password:
private.unlockAccount(eth.accounts[1], ‘blockchain’)
//From the miner #2
eth.sendTransaction({from: eth.accounts[1], to: “0x10dbd79d08ba50bc0cc1d5d00f2b68d97cf72870”, worth: web3.toWei(2, “ether”)})
//From the miner #2, 
web3.fromWei( eth.getBalance(eth.accounts[1]))
//17.9999
//From the miner #1,
web3.fromWei( eth.getBalance(eth.accounts[1]))
//2

1.16. Synchronize RPI
End RPI configuration primarily based on Secton 2.
Replace the file “static-nodes.json” with RPI enode

[
“enode://f9d48547c373fd61124d110791168572382298979d18ab5d6314cd399538ef77b[email protected]35.247.47.68:30303?discport=0”,
“enode://991f8279ae27e665151621a9940b42e208bb7cda0108c3cda219059bed28fbb7e[email protected]34.105.65.4:30304?discport=0”,
“enode://2bb141277704d6ca1e7920c744090e01f53c3842f238a09c9b14c425eb0eb75ef[email protected]192.168.0.106:30303?discport=0”
]

/copy static-nodes.json into the folders
in vm1,
/dwelling/digitransolutions7/RPI/miner1
in vm2
/dwelling/digitransolutions7/RPI/miner2
/dwelling/ubuntu/rpi-gcp/node
//exit all 3 miners and once more re-start miners
//from connected home windows, test peer standing
admin.friends
//Observe all 3 miners are paired.

1.17. Ship ethers from Miner #1 to RPI
//begin mining in two pc miner attachments: 
miner.begin()
//ship 10 ethers between the next accounts:
//miner #1(eth.coinbase) -> RPI(eth.accounts[1])
//From the Geth console linked to the RPi
eth.accounts[1]
“0x442f086b987d703887710af9ab59dce364c5f922”
//get steadiness
web3.fromWei(eth.getBalance(eth.accounts[1]))
//7.9999
//From the Geth console linked to the miner #1
eth.sendTransaction({from: eth.coinbase, to: “0x442f086b987d703887710af9ab59dce364c5f922”, worth: web3.toWei(10, “ether”)})
//From the miner #1
web3.fromWei( eth.getBalance(“0x442f086b987d703887710af9ab59dce364c5f922”))
//From the RPI
web3.fromWei( eth.getBalance(eth.accounts[1]))
//17.9999

1.18. Ship ethers from RPI to Miner #2

//ship 2 ethers between the next accounts:
//RPI(eth.accounts[1]) -> miner #2(eth.accounts[1])
//From the Geth console linked to the miner #2
eth.accounts[1]
//”0xafa0fd7b8f2b6220ec618b7b517728ef5e21123d”
//get steadiness
web3.fromWei(eth.getBalance(eth.accounts[1]))
//0
//From the Geth console linked to the RPI
eth.sendTransaction({from: eth.accounts[1], to: “0xafa0fd7b8f2b6220ec618b7b517728ef5e21123d”, worth: web3.toWei(2, “ether”)})
//From the RPi, unlock the account #1 with its password:
private.unlockAccount(eth.accounts[1], ‘blockchain’)
//once more strive
eth.sendTransaction({from: eth.accounts[1], to: “0xafa0fd7b8f2b6220ec618b7b517728ef5e21123d”, worth: web3.toWei(2, “ether”)})
//From the RPi
web3.fromWei( eth.getBalance(eth.accounts[1]))
//15.99958
//From the miner #2
web3.fromWei( eth.getBalance(eth.accounts[1]))

Fig. 3: Raspberry PI

2. RPI Configuration

Pre-requisites:
The setup of the RPi would require the next {hardware}:
A Raspberry Pi
An SD Card with not less than 16Gb
A LAN cable
A keyboard
A show

2.1. Put together Ubuntu SDcard and login
Kind: different
consumer: ubuntu
pw: ubuntu

2.2. Setup wifi

2.3. set up Geth
cat /proc/cpuinfo
//processor -> ARMV7
https://geth.ethereum.org/downloads/
obtain https://gethstore.blob.core.home windows.internet/builds/geth-linux-arm7-1.8.23-

c9427004.tar.gz
tar zxvf geth-linux-arm7-1.8.23-c9427004.tar.gz
cd geth-linux-arm7-1.8.23-c9427004
sudo cp geth /usr/native/bin
geth model

2.4. Synchronize the Raspberry PI with the Personal Blockchain

cd ~/
mkdir rpi-gcp
sudo chown ubuntu:ubuntu -R rpi-gcp
sudo chmod -R 777 rpi-gcp
Use winscp to repeat CustomGenesis.json from home windows PC to rpi-gcp
cd rpi-gcp
pwd
/dwelling/ubuntu/rpi-gcp

2.5. Arrange the rpi node (RPI)

cd /dwelling/ubuntu/rpi-gcp
geth –datadir node init genesis.json
//default account for the miner #1
geth –datadir node account new
//Tackle: {e6f8bbc22d6ef8cb0991e1a27d9909c92422c119}
//pw: blockchain
//Add a further account for testing function:
geth –datadir node account new
//Tackle: {ed163d61901483eb7611b1c4a60abe56a8af5765}
//pw: blockchain
//record the accounts
geth –datadir node account record

2.6. Node: setup
Put together password.sec file underneath /dwelling/ubuntu/rpi-gcp
with content material = blockchain

geth –identification “node1” –networkid 42 –datadir “node” –nodiscover –mine –rpc –rpcport “8042” –port “30303” –unlock 0 –password “password.sec” –ipcpath “/dwelling/ubuntu/.ethereum/geth.ipc” –rpcapi “db,eth,internet,web3,private,miner,admin,txpool,debug”
To connect
open separate cmd window
geth connect “http://localhost:8042”
//get enode handle
admin.nodeInfo.enode
//”enode://2bb141277704d6ca1e7920c744090e01f53c3842f238a09c9b14c425eb0eb75ef[email protected]172.16.115.162:30303?discport=0″

2.7. Permission to node
cd rpi-gcp

sudo chmod -R 777 node

Truffle console with SmartToken address and ABI
Figure4: Truffle console with SmartToken handle and ABI

3. Create and deploy a sensible contract

3.1. Set up truffle
open a brand new cmd window with admin
mkdir SmartToken
chmod -R 777 SmartToken
cd /dwelling/digitransolutions7/SmartToken/
//https://registry.npmjs.org/truffle/-/truffle-5.1.66.tgz
sudo npm set up -g [email protected]
npm set up [email protected]
truffle init
truffle compile

3.2. Create good contract
cd /dwelling/digitransolutions7/SmartToken/contracts
Within the “contracts” listing, create a file named “SmartToken.sol” and paste the next code:

pragma solidity ^0.4.15;

contract SmartToken {
mapping(handle => uint) tokens;
occasion OnValueChanged(handle listed _from, uint _value);

perform depositToken(handle recipient, uint worth) public returns (bool success) {
tokens[recipient] += worth;
emit OnValueChanged(recipient, tokens[recipient]);
return true;
}

perform withdrawToken(handle recipient, uint worth) public returns (bool success) {
if (int(tokens[recipient] – worth) < 0) {
tokens[recipient] = 0;
} else {
tokens[recipient] -= worth;
}
emit OnValueChanged(recipient, tokens[recipient]);
return true;
}

perform getTokens(handle recipient) fixed public returns (uint worth) {
return tokens[recipient];
}
}

3.3. Create deployment file
Change the content material of “migrations/2_deploy_contracts.js” with the next content material as a way to deploy our “SmartToken” Good Contract:

var SmartToken = artifacts.require(“./SmartToken.sol”);

module.exports = perform(deployer) {
deployer.deploy(SmartToken);
};

3.4. Replace truffle.js
The file named “truffle.js” incorporates community settings used to establish your deployment platform.

module.exports = { networks: { growth: { host: “localhost”, port: 8042, network_id: “*”} } };

3.5. Begin miners in 2 cmd home windows in pc
cd /dwelling/digitransolutions7/RPI/
//Begin miner1 in cmd window with admin
sh startminer1.sh
//in miner1 attachment window
geth connect “http://localhost:8042”
miner.begin()

//Begin miner2 in cmd window with admin
cd /dwelling/digitransolutions7/RPI/
sh startminer2.sh
//in miner2 attachment window
geth connect “http://localhost:8043”
miner.begin()

3.6. Compile and deploy your contract
Run the command “truffle migrate” to compile and deploy your contract to your blockchain:
cd /dwelling/digitransolutions7/SmartToken
truffle compile
//ought to compile efficiently
truffle migrate –reset

3.7. Establish the contract
//open a cmd window with admin
cd /dwelling/digitransolutions7/SmartToken
truffle console
//Observe truffle(growth)>
SmartToken.handle
‘0x0ce678aa4dabef4627fd351deeb35889e6c3d246’
JSON.stringify(SmartToken.abi)
//ABI (Software Binary Interface)

[{“anonymous”:false,”inputs”:[{“indexed”:true,”name”:”_from”,”type”:”address”},{“indexed”:false,”name”:”_value”,”type”:”uint256″}],”identify”:”OnValueChanged”,”sort”:”occasion”},{“fixed”:false,”inputs”:[{“name”:”recipient”,”type”:”address”},{“name”:”value”,”type”:”uint256″}],”identify”:”depositToken”,”outputs”:[{“name”:”success”,”type”:”bool”}],”payable”:false,”stateMutability”:”nonpayable”,”sort”:”perform”},{“fixed”:false,”inputs”:[{“name”:”recipient”,”type”:”address”},{“name”:”value”,”type”:”uint256″}],”identify”:”withdrawToken”,”outputs”:[{“name”:”success”,”type”:”bool”}],”payable”:false,”stateMutability”:”nonpayable”,”sort”:”perform”},{“fixed”:true,”inputs”:[{“name”:”recipient”,”type”:”address”}],”identify”:”getTokens”,”outputs”:[{“name”:”value”,”type”:”uint256″}],”payable”:false,”stateMutability”:”view”,”sort”:”perform”}]

3.8. Check the contract
//get default handle created by RPI
//in RPI attachment cmd window,
eth.coinbase

“0xe6f8bbc22d6ef8cb0991e1a27d9909c92422c119”

//In geth console connected to miner1:
//Create occasion of Good Contract
var abi=[{“anonymous”:false,”inputs”:[{“indexed”:true,”name”:”_from”,”type”:”address”},{“indexed”:false,”name”:”_value”,”type”:”uint256″}],”identify”:”OnValueChanged”,”sort”:”occasion”},{“fixed”:false,”inputs”:[{“name”:”recipient”,”type”:”address”},{“name”:”value”,”type”:”uint256″}],”identify”:”depositToken”,”outputs”:[{“name”:”success”,”type”:”bool”}],”payable”:false,”stateMutability”:”nonpayable”,”sort”:”perform”},{“fixed”:false,”inputs”:[{“name”:”recipient”,”type”:”address”},{“name”:”value”,”type”:”uint256″}],”identify”:”withdrawToken”,”outputs”:[{“name”:”success”,”type”:”bool”}],”payable”:false,”stateMutability”:”nonpayable”,”sort”:”perform”},{“fixed”:true,”inputs”:[{“name”:”recipient”,”type”:”address”}],”identify”:”getTokens”,”outputs”:[{“name”:”value”,”type”:”uint256″}],”payable”:false,”stateMutability”:”view”,”sort”:”perform”}]
var MyContract = web3.eth.contract(abi);
var MyContractInstance = MyContract.at(‘0x0ce678aa4dabef4627fd351deeb35889e6c3d246’);
//switch 10 tokens to RPI
MyContractInstance.depositToken(“0xe6f8bbc22d6ef8cb0991e1a27d9909c92422c119”, 10, {from: eth.accounts[0], fuel:3000000});
//test the transaction after 10 seconds
MyContractInstance.getTokens(“0xe6f8bbc22d6ef8cb0991e1a27d9909c92422c119”, {from: eth.accounts[0], fuel:3000000});
//it ought to show 10
//withdraw 10 tokens from RPI account
MyContractInstance.withdrawToken(“0xe6f8bbc22d6ef8cb0991e1a27d9909c92422c119”, 10, {from: eth.accounts[0], fuel:3000000});

Figure 5: Project setup
Determine 5: Venture setup

4. Management RPI ports

4.1. Begin miners in 2 cmd home windows in pc
cd /dwelling/digitransolutions7/RPI/
//Begin miner1 in cmd window with admin
sh startminer1.sh
//in miner1 attachment window
geth connect “http://localhost:8042”
miner.begin()

//Begin miner2 in cmd window with admin
cd /dwelling/digitransolutions7/RPI/
sh startminer2.sh
//in miner2 attachment window
geth connect “http://localhost:8043”
miner.begin()

4.2. Put together RPI
//Set up Node.js
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash –
sudo apt-get set up nodejs
node -v
npm -v
//Set up Git
sudo apt-get set up git
//Set up web3 module
mkdir -p ~/RPI/SmartToken
cd ~/RPI/SmartToken
npm set up [email protected]
//Set up OnOff module
ln -s /usr/bin/python /usr/native/bin
——————~/.bashrc—–
export PYTHONPATH=$PYTHONPATH:/usr/native/bin
export PATH=$PATH:/usr/native/bin
————————–
supply ~/.bashrc
npm rebuild node-sass
cd ~/RPI/SmartToken
npm set up onoff

4.3. Create the Shopper utility
//create – “smart_token.js”
cd ~/RPI/SmartToken
sudo nano smart_token.js
//Add SmartToken.handle and ABI

// Interplay with GPIO
var Gpio = require(‘onoff’).Gpio
// Interplay with Ethereum
var Web3 = require(‘web3’)
var web3 = new Web3()
// connect with the native node
web3.setProvider(new web3.suppliers.HttpProvider(‘http://localhost:8042’))
// The contract that we’re going to work together with
var contractAddress = ‘0x0ce678aa4dabef4627fd351deeb35889e6c3d246’
// Outline the ABI (Software Binary Interface)
var ABI = JSON.parse(‘[{“anonymous”:false,”inputs”:[{“indexed”:true,”name”:”_from”,”type”:”address”},{“indexed”:false,”name”:”_value”,”type”:”uint256″}],”identify”:”OnValueChanged”,”sort”:”occasion”},{“fixed”:false,”inputs”:[{“name”:”recipient”,”type”:”address”},{“name”:”value”,”type”:”uint256″}],”identify”:”depositToken”,”outputs”:[{“name”:”success”,”type”:”bool”}],”payable”:false,”stateMutability”:”nonpayable”,”sort”:”perform”},{“fixed”:false,”inputs”:[{“name”:”recipient”,”type”:”address”},{“name”:”value”,”type”:”uint256″}],”identify”:”withdrawToken”,”outputs”:[{“name”:”success”,”type”:”bool”}],”payable”:false,”stateMutability”:”nonpayable”,”sort”:”perform”},{“fixed”:true,”inputs”:[{“name”:”recipient”,”type”:”address”}],”identify”:”getTokens”,”outputs”:[{“name”:”value”,”type”:”uint256″}],”payable”:false,”stateMutability”:”view”,”sort”:”perform”}]’)
// contract object
var contract = web3.eth.contract(ABI).at(contractAddress)

// elements linked to the RPi
var greenLed = new Gpio(14, ‘out’)
var redLed = new Gpio(15, ‘out’)
var button = new Gpio(18, ‘in’, ‘rising’)
// show preliminary state
showStatus()
// watch occasion on the button
button.watch(perform (err, worth) {
if (err) {
throw err
}
showStatus()
})
// watch for an occasion triggered on the Good Contract
var onValueChanged = contract.OnValueChanged({_from: web3.eth.coinbase});
onValueChanged.watch(perform(error, outcome) {
if (!error) {
showStatus()
}
})
// energy the LED in accordance the worth of the token
perform showStatus() {
web3.eth.defaultAccount = web3.eth.coinbase
//web3.private.unlockAccount(web3.eth.accounts[1], ‘blockchain’)
//web3.eth.defaultAccount = web3.eth.accounts[0]
// retrieve the worth of the token
var token = contract.getTokens(web3.eth.defaultAccount)
// show the LED in accordance the worth of the token
const led = new Gpio(17, ‘out’);       // Export GPIO17 as an output
if (token > 1) {
// Inexperienced: you’ve got sufficient token
redLed.writeSync(0)
greenLed.writeSync(1)
led.writeSync(1)
} else {
// Pink: not sufficient token
greenLed.writeSync(0)
redLed.writeSync(1)
led.writeSync(0)
// Toggle the state of the LED linked to GPIO17 each 200ms
//const iv = setInterval(_ => led.writeSync(led.readSync() ^ 1), 200);
}
}
// launch course of
course of.on(‘SIGINT’, perform () {
greenLed.unexport()
redLed.unexport()
button.unexport()
})

4.4. Run the consumer utility
cd ~/RPI/SmartToken
node smart_token.js

4.5. Check the appliance
//Check your utility utilizing Mist by depositing tokens to the coinbase handle of the Pi node.
//get default handle created by RPI
//in RPI attachment cmd window,
eth.coinbase
“0xe6f8bbc22d6ef8cb0991e1a27d9909c92422c119”

//In geth console connected to miner1:
//Create occasion of Good Contract
var abi=[{“anonymous”:false,”inputs”:[{“indexed”:true,”name”:”_from”,”type”:”address”},{“indexed”:false,”name”:”_value”,”type”:”uint256″}],”identify”:”OnValueChanged”,”sort”:”occasion”},{“fixed”:false,”inputs”:[{“name”:”recipient”,”type”:”address”},{“name”:”value”,”type”:”uint256″}],”identify”:”depositToken”,”outputs”:[{“name”:”success”,”type”:”bool”}],”payable”:false,”stateMutability”:”nonpayable”,”sort”:”perform”},{“fixed”:false,”inputs”:[{“name”:”recipient”,”type”:”address”},{“name”:”value”,”type”:”uint256″}],”identify”:”withdrawToken”,”outputs”:[{“name”:”success”,”type”:”bool”}],”payable”:false,”stateMutability”:”nonpayable”,”sort”:”perform”},{“fixed”:true,”inputs”:[{“name”:”recipient”,”type”:”address”}],”identify”:”getTokens”,”outputs”:[{“name”:”value”,”type”:”uint256″}],”payable”:false,”stateMutability”:”view”,”sort”:”perform”}]
var MyContract = web3.eth.contract(abi);
var MyContractInstance = MyContract.at(‘0x0ce678aa4dabef4627fd351deeb35889e6c3d246’);
//switch 10 tokens to RPI
MyContractInstance.depositToken(“0xe6f8bbc22d6ef8cb0991e1a27d9909c92422c119”, 10, {from: eth.accounts[0], fuel:3000000});
//test the transaction after 10 seconds
MyContractInstance.getTokens(“0xe6f8bbc22d6ef8cb0991e1a27d9909c92422c119”, {from: eth.accounts[0], fuel:3000000});
//it ought to show 10
//Observe RPI port = excessive
//withdraw 10 tokens from RPI account
MyContractInstance.withdrawToken(“0xe6f8bbc22d6ef8cb0991e1a27d9909c92422c119”, 10, {from: eth.accounts[0], fuel:3000000});
//Observe RPI port = low

5. Demo

The demo of this venture is given on this hyperlink

Supply code:

Hyperlink: 1

Hyperlink: 2

References

1.Create a non-public Ethereum blockchain with IoT gadgets (1/6) – All 6 components

https://chainskills.com/2017/02/24/create-a-private-ethereum-blockchain-with-iot-devices-16/

2.Ethereum Good Contract – Methods to execute a Good Contract?

https://www.edureka.co/weblog/ethereum-smart-contract-project#CreatingEthereumAccount

3. Node.js Raspberry Pi – GPIO Introduction

https://www.w3schools.com/nodejs/nodejs_raspberrypi_gpio_intro.asp

https://chainskills.com/2017/04/10/raspberry-pi-and-ethereum-the-epilogue/

4. IOT Pattern code

https://www.npmjs.com/package deal/onoff



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments