Simone Romano
Published © Apache-2.0

A Blockchain-Powered Smart-Lock

Securely open/close your front door with a smart contract

IntermediateFull instructions provided2 hours8,262
A Blockchain-Powered Smart-Lock

Things used in this project

Hardware components

Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
DC 12V Solenoid Electromagnetic Mini Door Lock
×1
12V Power Supply 24W with free wires adaptor
×1
Micro USB Power Supply for Raspberry Pi Zero
×1
16GB Micro SD Card
×1
4 Channel DC 5V Relay Module for Arduino Raspberry Pi
×1
Female to Female Breadboard Dupont Jumper Wires
×1

Software apps and online services

Blockchain
IoTeX Blockchain

Story

Read more

Code

SimpleDoorLock Contract

C/C++
A very basic smart contract to keep and toggle the state of the door lock.
pragma solidity ^0.5.0;

// Import the "Ownable" interface from OpenZeppelin, that implements
// the "onlyOwner" modifier we use to make sure only the contract
// owner can operate the smart-lock
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v2.5.0/contracts/ownership/Ownable.sol";

/**
 * @title Simple Smart-Lock
 * @dev Very simple smart contract to manage the state of a smart-lock
 */
contract SimpleDoorLock is Ownable {

    // Keeps the status of the smart-lock: true = door is locked!
    bool public State;
    
    /*
     * For safety, let's initialize the state to false (lock is open)
     */
    constructor () public { State = false;  }
    
    /*
     * Toggles the state of the door lock, 
     * only the contract owner account can call this function!
    */
    function toggle() public onlyOwner() { State = !State; }
}

checkState Script

SH
This script will periodically query the smart contract state and drive the lock accordingly
#!/bin/bash

# Replace the address belowwith the address of your SimpleDoorLock 
# contract address you got from IoTeX Studio IDE
CONTRACT_ADDRESS=io1vj8x48y87pa2mj5703ksa8apgeupdggx2r6vrd 

while :
do

echo "Polling the contract..."

STATE=$(ioctl contract test function $CONTRACT_ADDRESS abi.json State |tr -d ':'| awk '{print $2}')

echo "Contract state = " $STATE


if [ "$STATE" = "true" ];
then
 # Open the Lock
 echo 0 > /sys/class/gpio/gpio14/value
else
 # Close the Lock
 echo 1 > /sys/class/gpio/gpio14/value
fi

# Cycle every 10 seconds
sleep 10
done

Credits

Simone Romano

Simone Romano

5 projects • 26 followers
Developers Community Growth @ IoTeX

Comments