Giter Site home page Giter Site logo

iotex-tutorial-blockchain-smart-lock's Introduction

Blockchain controlled smart lock using Arduino

This tutorial demonstrates how to build a smart lock that can be controlled from a smart contract.
We will use popular Arduino boards like ESP32 and Arduino Nano 33 IoT and we will connect them to the IoTex blockchain.

First steps

  • Clone the repository.
  • Open a terminal and change directory to SmartLockBlockchain.
  • Run npm install to install the required dependencies.
  • Install Arduino IDE and install the IoTeX-blockchain-client library.
  • If you are using Nano33 IoT, install the FlashStorage and WiFiNina Arduino libraries.

The blockchain smart contract

We will create a smart contract that stores the device state in the IoTeX layer 1 blockchain and exposes functions for reading and changing the lock state.
A simple smart contract for this tutorial is located here.

Note that this is a very simplified smart contract, for the purposes of this tutorial. It has several limitations, including:

  • The contract implements the Ownable interface. Only the owner of the contract can read or write the smart lock state.
  • The status of the lock is stored in the public blockchain in an unencrypted manner.

Deploying the contract

  • Open a terminal and change directory to SmartLockBlockchain.
  • Create your .env file from the template by running cp .env-template .env.
  • Replace <your-private-key> with the private key you want to use to deploy the contract in .env.
  • Ensure that the deployer address has enough IOTX tokens for the deployment. You can use the faucet to get testnet IOTX.
  • Run npx hardhat run scripts/deploy.js --network <NETWORK> replacing <NETWORK> for either testnet or mainnet.

If the deployment is successful, you should see something similar to this:

$npx hardhat run scripts/deploy.js --network testnet
Compiled 3 Solidity files successfully
Lock deployed to:  0xbB21eD8dB133F0e4216F712D19C68F6b5af6b6e1
Lock state:  false

Take a note of the contract address. You will need to program it into the device as indicated in the next step.

The Arduino smart lock

We will create a DIY smart lock by wiring a magnetic lock to an Arduino board.

Supported devices

At the moment, the sketch can be built for ESP32 and Arduino Nano 33 IoT.

Wiring

Simply wire the magnetic lock to a digital output pin in the microcontroller. You would need a relay to power the lock.

See an example of the wiring below:

  • Wiring example

Building and flashing the firmware

A simple sketch for the Arduino board is located here.
The sketch "polls" the contract isOpen() function to read the lock state from the blockchain. Then it sets the relay pin to either open or close the lock based on the contract state.

You need to configure the sketch to suit your environment following the steps below:

  • Open the SmartLockDevice fodler in Arduino IDE.
  • Open the secrets.h file and replace the values with yours.
  • Select your board and port in the Arduino IDE.
  • Click the upload button and wait until the firmware is build and flashed to your device.

Opening/closing the lock

In order to open and close the lock, you need to call the setState() function of your contract.
You can do this with the ioctl command line tool. Follow the installation instructions to install the tool in your pc.

Once you have ioctl installed you can follow the next steps to configure it:

  • Import the contract owner private key with ioctl account import key smartLock.
  • Set the default signer account to the smart lock owner account ioctl config set defaultacc smartLock.
  • Point ioctl to testnet if you are using testnet ioctl config set endpoint api.testnet.iotex.one:443.
  • Change directory into the SmartLockBlockchain directory.

The you can use the follwing command to open the lock:
ioctl contract invoke function io1x6y5gjty378vjwqz7ahdkf5thfeum9q23h394w Lock.abi setState --with-arguments '{"open": true }'

And the following one to close it:
ioctl contract invoke function io1x6y5gjty378vjwqz7ahdkf5thfeum9q23h394w Lock.abi setState --with-arguments '{"open": false }'

After you run each command, wait for some seconds to ensure that the transaction is confirmed and the device opens or closes the lock.

iotex-tutorial-blockchain-smart-lock's People

Contributors

as-iotex avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.