Skip to content

Commit f073d58

Browse files
updated: readme, contract improvements, ci pipeline tests
1 parent d75ac98 commit f073d58

File tree

12 files changed

+201
-2318
lines changed

12 files changed

+201
-2318
lines changed

.github/workflows/e2e.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: e2e-test
2+
3+
on:
4+
push:
5+
branches: [main, develop, "feat/*"]
6+
pull_request:
7+
branches: [main, develop, "feat/*"]
8+
9+
jobs:
10+
nodejs-step:
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
node-version: [16.x, 18.x, 20.x]
15+
os: [macos-latest, windows-latest]
16+
17+
runs-on: ${{ matrix.os }}
18+
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v3
22+
- name: Set up Node.js ${{ matrix.node-version }}
23+
uses: actions/setup-node@v3
24+
with:
25+
node-version: ${{ matrix.node-version }}
26+
# - name: Install Windows Build Tools (if needed)
27+
# if: matrix.os == 'windows-latest'
28+
# run: |
29+
# npm install -g windows-build-tools
30+
- name: Install Node.js dependencies
31+
run: npm install
32+
- name: Compile contract
33+
run: npx hardhat compile
34+
- name: Start local blockchain
35+
run: npx hardhat node & sleep 5
36+
37+
python-step:
38+
strategy:
39+
fail-fast: false
40+
matrix:
41+
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
42+
os: [macos-latest, windows-latest]
43+
44+
runs-on: ${{ matrix.os }}
45+
46+
steps:
47+
- name: Checkout code
48+
uses: actions/checkout@v3
49+
- name: Set up Python ${{ matrix.python-version }}
50+
uses: actions/setup-python@v4
51+
with:
52+
python-version: ${{ matrix.python-version }}
53+
- name: Install Python dependencies
54+
run: pip install -r requirements.txt
55+
# - name: Start App
56+
# run: python app.py & sleep 5
57+
# Add your testing steps here
58+
# - name: Simulate GPIO 14 ON request
59+
# run: |
60+
# if [[ "${matrix.os}" == "windows-latest" ]]; then
61+
# curl http://localhost:8000/fourteen/on
62+
# else
63+
# curl http://host.docker.internal:8000/fourteen/on
64+
# fi

.github/workflows/pythonapp.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

README.md

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,72 @@
1-
## IoT-and-Blockchain
1+
# IoT-and-Blockchain
22

3-
- [Demonstration Article](https://link.medium.com/TcNvHHZoY1)
3+
A simple IoT and Blockchain based application to demonstrate the use of blockchain in IoT.
44

5-
##### Quick Demo
6-
>Note: *Windows Build Tools is required to install web3. Install through Powershell(Admin) if not installed already*
5+
## Getting Started
76

8-
```
7+
### Prerequisites
8+
9+
- [Node.js](https://nodejs.org/en/download/)
10+
- [Python](https://www.python.org/downloads/)
11+
- Windows 8+ or MacOS X
12+
13+
> Note: _Windows Build Tools is required to install web3. Install through Powershell(Admin) if not installed already_
14+
15+
```bash
916
npm install -g windows-build-tools
1017
```
1118

19+
### Steps
20+
21+
Before starting with app, you need to compile the smart contract and start a local blockchain. Follow the steps below to do so:
22+
23+
1. Install required dependencies:
24+
25+
```bash
26+
npm install
27+
```
28+
29+
2. Start a local blockchain using Hardhat:
1230

31+
```bash
32+
npx hardhat node
1333
```
14-
git clone https://github.com/Salmandabbakuti/IoT-and-Blockchain.git
15-
cd IoT-and-Blockchain
16-
yarn install
17-
yarn start-node # local testnet for deployments
18-
yarn compile # compile contract
19-
yarn deploy # deploy contract
2034

35+
3. Compile contract in a separate terminal:
36+
37+
```bash
38+
npx hardhat compile
39+
```
40+
41+
4. Install python dependencies and start the app:
42+
43+
```bash
2144
pip install -r requirements.txt
22-
python app.py # run app: open localhost:8000
45+
46+
python app.py
2347
```
2448

25-
<img align="center" src="https://github.com/Salmandabbakuti/IoT-and-Blockchain/blob/master/assets/screen.png" width="90%">
49+
Open http://localhost:8000 in your browser to see the app and Interact with the IoT device.
50+
51+
### Demo
52+
53+
![screen](https://github.com/Salmandabbakuti/IoT-and-Blockchain/assets/29351207/4e684842-095e-4472-85fc-5621295ce6a3)
54+
55+
## Built With
56+
57+
- [Flask](https://flask.palletsprojects.com/en/1.1.x/) - The web framework used for the backend
58+
- [Web3.py](https://web3py.readthedocs.io/en/stable/) - Python library for interacting with Ethereum blockchain
59+
- [Hardhat](https://hardhat.org/) - Ethereum development environment for compiling, testing, deploying, and interacting with smart contracts
60+
- [Solidity](https://docs.soliditylang.org/en/v0.8.4/) - Ethereum's smart contract programming language
61+
- [GPIO Simulator](https://pypi.org/project/GPIOSimulator/) - Python library for simulating GPIO pins
62+
- [RPi.GPIO](https://pypi.org/project/RPi.GPIO/) - Python library for accessing GPIO pins on Raspberry Pi
63+
64+
## Safety
2665

66+
This is experimental software and subject to change over time.
2767

28-
##### Author
68+
This is a proof of concept and is not ready for production use. It is not audited and has not been tested for security. Use at your own risk. I do not give any warranties and will not be liable for any loss incurred through any use of this codebase.
2969

30-
##### :wave: [Salman Dabbakuti](https://salmandabbakuti.github.io)
70+
## License
3171

32-
<a href="https://www.buymeacoffee.com/Salmandabbakuti" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
72+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

app.py

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
contract = w3.eth.contract(abi=abi, bytecode=bytecode)
2626

2727
# Get transaction hash from deployed contract
28-
tx_hash = contract.constructor().transact({'from': w3.eth.accounts[0], 'gas': 410000})
28+
tx_hash = contract.constructor().transact()
2929

3030
# Get tx receipt to get contract address
3131
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
3232

3333
# Deployed Contract instance
34-
contract_instance = w3.eth.contract(abi=abi, address=tx_receipt.contractAddress)
34+
contract_instance = w3.eth.contract(address=tx_receipt.contractAddress, abi=abi, )
3535

3636
#Control Interface
3737
for i in pinList:
@@ -43,77 +43,77 @@
4343
def index():
4444
return render_template('index.html')
4545

46-
@app.route("/<pin>/<action>")
47-
def control(pin, action):
46+
@app.route("/<pin_id>/<action>")
47+
def set_pin_status(pin_id, action):
4848
#Actuator configuration
49-
if pin=='fourteen':
50-
actuator=14
51-
elif pin=='fifteen':
52-
actuator=15
53-
elif pin=='eighteen':
54-
actuator=18
55-
elif pin=='twentythree':
56-
actuator=23
57-
elif pin=='twentyfour':
58-
actuator=24
59-
elif pin=='twentyfive':
60-
actuator=25
61-
elif pin=='eight':
62-
actuator=8
63-
elif pin=='seven':
64-
actuator=7
65-
elif pin=='twelve':
66-
actuator=12
67-
elif pin=='sixteen':
68-
actuator=16
69-
elif pin=='twenty':
70-
actuator=20
71-
elif pin=='twentyone':
72-
actuator=21
73-
elif pin=='two':
74-
actuator=2
75-
elif pin=='three':
76-
actuator=3
77-
elif pin=='four':
78-
actuator=4
79-
elif pin=='seventeen':
80-
actuator=17
81-
elif pin=='twentyseven':
82-
actuator=27
83-
elif pin=='twentytwo':
84-
actuator=22
85-
elif pin=='ten':
86-
actuator=10
87-
elif pin=='nine':
88-
actuator=9
89-
elif pin=='eleven':
90-
actuator=11
91-
elif pin=='five':
92-
actuator=5
93-
elif pin=='six':
94-
actuator=6
95-
elif pin=='thirteen':
96-
actuator=13
97-
elif pin=='nineteen':
98-
actuator=19
99-
elif pin=='twentysix':
100-
actuator=26
49+
if pin_id=='fourteen':
50+
pin_number=14
51+
elif pin_id=='fifteen':
52+
pin_number=15
53+
elif pin_id=='eighteen':
54+
pin_number=18
55+
elif pin_id=='twentythree':
56+
pin_number=23
57+
elif pin_id=='twentyfour':
58+
pin_number=24
59+
elif pin_id=='twentyfive':
60+
pin_number=25
61+
elif pin_id=='eight':
62+
pin_number=8
63+
elif pin_id=='seven':
64+
pin_number=7
65+
elif pin_id=='twelve':
66+
pin_number=12
67+
elif pin_id=='sixteen':
68+
pin_number=16
69+
elif pin_id=='twenty':
70+
pin_number=20
71+
elif pin_id=='twentyone':
72+
pin_number=21
73+
elif pin_id=='two':
74+
pin_number=2
75+
elif pin_id=='three':
76+
pin_number=3
77+
elif pin_id=='four':
78+
pin_number=4
79+
elif pin_id=='seventeen':
80+
pin_number=17
81+
elif pin_id=='twentyseven':
82+
pin_number=27
83+
elif pin_id=='twentytwo':
84+
pin_number=22
85+
elif pin_id=='ten':
86+
pin_number=10
87+
elif pin_id=='nine':
88+
pin_number=9
89+
elif pin_id=='eleven':
90+
pin_number=11
91+
elif pin_id=='five':
92+
pin_number=5
93+
elif pin_id=='six':
94+
pin_number=6
95+
elif pin_id=='thirteen':
96+
pin_number=13
97+
elif pin_id=='nineteen':
98+
pin_number=19
99+
elif pin_id=='twentysix':
100+
pin_number=26
101101
else:
102102
return render_template('index.html')
103103

104104
#Control interface
105105
if action=='on':
106-
is_active = True
106+
pin_status = 1
107107
else:
108-
is_active = False
109-
tx_hash = contract_instance.functions.controlPin(actuator, is_active).transact({'from': w3.eth.accounts[0]})
108+
pin_status = 0
109+
tx_hash = contract_instance.functions.setPinStatus(pin_number, pin_status).transact({'from': w3.eth.accounts[0]})
110110
print('Transaction submitted:', tx_hash.hex())
111-
pin_status = format(contract_instance.functions.pinStatus(actuator).call())
112-
print(f'Pin {actuator} status changed to {pin_status}')
113-
if pin_status == 'True':
114-
GPIO.output(actuator,GPIO.HIGH)
111+
pin_status = format(contract_instance.functions.pinStatus(pin_number).call())
112+
print(f'Pin {pin_number} status changed to {pin_status}')
113+
if pin_status == "1":
114+
GPIO.output(pin_number,GPIO.HIGH)
115115
else:
116-
GPIO.output(actuator,GPIO.LOW)
116+
GPIO.output(pin_number,GPIO.LOW)
117117

118118
return render_template('index.html')
119119

assets/screen.png

-136 KB
Binary file not shown.

contracts/PinController.sol

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ contract PinController {
88
owner = msg.sender;
99
}
1010

11-
mapping(uint8 => bool) public pinStatus;
11+
enum PinStatus {
12+
Off,
13+
On
14+
}
15+
mapping(uint8 => PinStatus) public pinStatus;
1216

13-
function controlPin(uint8 _pin, bool _isActive) public {
17+
function setPinStatus(uint8 _pin, PinStatus _pinStatus) public {
1418
require(msg.sender == owner);
15-
pinStatus[_pin] = _isActive;
19+
pinStatus[_pin] = _pinStatus;
1620
}
1721
}

hardhat.config.js

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,3 @@
1-
require("@nomiclabs/hardhat-ethers");
2-
3-
// This is a sample Hardhat task. To learn how to create your own go to
4-
// https://hardhat.org/guides/create-task.html
5-
task("hello", "Prints Hello World", () => console.log("Hello World!"));
6-
7-
task("deploy", "Deploys Contract", async () => {
8-
const contractFactory = await ethers.getContractFactory("PinController");
9-
const contract = await contractFactory.deploy();
10-
await contract.deployed();
11-
console.log("contract deployed to:", contract.address);
12-
});
13-
141
module.exports = {
15-
defaultNetwork: "local",
16-
networks: {
17-
hardhat: {
18-
chainId: 1337
19-
},
20-
local: {
21-
url: "http://127.0.0.1:8545",
22-
}
23-
},
24-
solidity: {
25-
version: "0.8.13",
26-
settings: {
27-
optimizer: {
28-
enabled: true,
29-
runs: 200
30-
}
31-
}
32-
},
33-
paths: {
34-
sources: "./contracts",
35-
tests: "./test",
36-
cache: "./cache",
37-
artifacts: "./artifacts"
38-
},
39-
mocha: {
40-
timeout: 20000
41-
}
42-
}
2+
solidity: "0.8.13",
3+
};

0 commit comments

Comments
 (0)