This tutorial will be exploring the potential of combining IoT and blockchain using simple Python directly on microcontrollers, thanks to Zerynth tools.

In particular, the Zerynth Ethereum library allows microcontrollers, the heart of IoT solutions, to generate and sign Ethereum transactions in a few lines of Python. This empowers IoT developers to easily connect to the Ethereum blockchain, use Smart Contracts and seamlessly integrate IoT devices with DApps (decentralized applications).

A high-level interface is made available with this library, granting to anyone the possibility to start using blockchain and secure any kind of IoT application.

For those new to Zerynth, it allows to program the most popular 32-bit microcontrollers in Python and connect them to the top Cloud infrastructures, with high-level standards of security.

Why blockchain for embedded and IoT solutions

Having the possibility to generate, sign and send transactions from the inside of a microcontroller enables any device capable of running the cryptography algorithms to directly take advantage of smart contracts, removing centralized gateways and points of failure.

This can be applied to many different fields and increments trust from consumers and third parties.

For example, a supply chain can be monitored from the inside removing any single central authority storing the data and offering to final customers a certificate of the process goods are gone through. Going further, it can enable the economy of things, i.e. objects that can actually receive payments and offer services in exchange on-demand – like smart electricity meters offering a completely transparent service for the users.

For better understand the advantages of using Blockchain with Embedded Systems, take a look at this tech talk by BARR Group.

And here you can find the use case for logistics made by TOI – Things on Internet, the company that develops 4ZeroBox, an ESP32 board “powered by Zerynth” and tailored for Industrial IoT applications.

The Zerynth Ethereum module – open source Python library for Ethereum and Smart Contracts

The Zerynth Ethereum library takes advantage of the JSON-RPC interface available to interact with an Ethereum node and sending transaction. It also implements the RLP (Recursive Length Prefix) encoding scheme, and expose some high-level API to call the more common methods (e.g. getting the balance of an address). For the hashing and signatures, the Zerynth crypto module is used.

The main class available is RPC. From an RPC object, it’s possible to fetch network status information and make transactions. To get a higher level interface there are also two companion classes: Transaction and Contract. The first helps to build a correct signed transaction ready to be sent, and the latter can be used to register a smart contract and its methods for calling them later.

For more info take a look at the official documentation page. You can also find the source code of the library on GitHub!

In the following examples, both of these are shown in a real Ethereum test network.

Description of the examples: “Simple Transaction” and “Dice Game”

There are two examples available, showing how to make a simple transaction and how to configure and use a real smart contract.

In both examples the Ropsten test network is used, so no real value is actually being transferred, but it acts exactly the same way that it would be in the real Ethereum network.

Example 1: “Simple Transaction”

This example shows how to transfer value (Ether) from an address to another through an Ethereum transaction. After signing and sending the transaction it’s possible to use an external tool like https://ropsten.etherscan.io to monitor the status and the validity of it.

Take a look at a snippet of the code. You can find the complete code and more info at the related documentation page.