Handle smart contract on Ethereum with Arduino or ESP32

Takahiro Okada
3 min readMar 2, 2018

--

Introduction

Many of you know blockchain and bitcoin. One of blockchain system — Ethereum — can handle “Smart Contract” which is a protocol to digitalize contracts like payment, account, fund, gamble and ICO.
Some of you think this kind of blockchain system requires high spec computers. But this is not true — very cheap micro computer like Arduino can handle blockchain and smart contract.

I developed a software library to handle smart contract on Ethereum with Arduino or ESP32. (Arduino is a very cheap micro computer system and its core microchip is around 2USD. EPS32 is a similar chip which includes Wi-Fi system and radio antenna (the price is also around 2USD).)

The library named web3-arduino is behave as an arduino library and enable to develop below functions

  • get some information on Ethereum blockchain like current gas price or block number
  • call a function and get current contract status
  • send a transaction to contract and update contract status

So if you already have deployed contracts on Ethereum blockchain, you can use your contracts from small & cheap micro controller.

Note: This system cannot do below

  • mining
  • compile and issue smart contracts
  • be Ethereum contract (you must have accessible Ethereum client like geth)

Overall development flow

Precondition: You should prepare below to start

  • ESP32 development environment
  • a Ethereum address
  • access right to INFURA (or your own Ethereum client)

Procedure 1: Deploy your smart contract to Ethereum mainnet or testnet

Procedure 2: Install web3-arduino to your Arduino IDE

Procedure 3: Develop ESP32 software

Procedure1: Deploy your smart contract to Ethereum mainnet or testnet

As I wrote above, you have to develop and deploy your smart contract with PC. As an example, I deployed below contract as a sample.

I deployed to Rinkeby network, which contract address is 0x868d93bf82b9047574af4297d4af0fbaa012bbcd

If you are not familiar to develop Ethereum smart contract, please check below article.

Learning Solidity Part 1: Contract Dev with MetaMask

Procedure 2: Install web3-arduino to your Arduino IDE

web3-arduino is an Arduino library to operate Ethereum blockchain and smart contract. This is under development and only confirmed ESP32.

You can download from github.

You can install like below.

Installing Additional Arduino Libraries

Procedure 3: Develop ESP32 software

Now, you can write Arduino source code. Whole source code is here

In summary, you will create web3 instance. Here, I used rinkeby.infura.io as a host, but you can use another client service or your own server.

At first, let’s try to call Web3ClientVersion() as a test. If you can connect to host, you can get Ethereum client version

Next, you can send transaction to smart contract as below.

If it works well, transaction is handle on the blockchain. You can check the result with Etherscan.

According to my test, sent transaction is handled well.

Summary

  • With the library, ESP32 can control smart contracts on Ethereum blockchain.
  • For now, this library is not mature and Ethereum blockchain might not be mature (i.e. gas price is expensive). However, these system will be mature and soon. So we can enjoy blockchain and smart contract with micro controller soon!
  • For example, I am developing smart outlet with using M2M payment!

--

--