Blockchain Electricity Power Meter with a Cryptnox Card

Cryptnox Cards
4 min readSep 13, 2022

--

Use a Cryptnox Hardware Wallet smartcard for IOT projects related to blockchain

Crytpnox is a swiss company developing and producing hardware wallets (cold storage) in smart card format.
In this project, we will show how a Cryptnox Hardware wallet smart card can be used to secure the private key for a token minting power meter. It can potentially have an application to projects related to tokenized Kilowatts per hour (kWh) of renewable origin. The solution presented can also have application to any kind of sensor readings.

We are using an Ethereum Virtual Machine compatible blockchain with ERC20 smart contract. The amount of kWh token minted by the device is equivalent to the measurement of kWh production over a period T.

The code of the project is published on the following repository:

The hardware required:

The device will record electricity production from the SO pulse out reading of the meter.

The device’s core functionality is the following:

  1. Measure electricity production on a connected wire in kWh over a period of time T
  2. At the end of each period of time T, the device will perform the following actions:
    a) internally reads the total amount of kWh,
    b) generate a corresponding “mint” transaction for equivalent tokens of a specific smart contract,
    c) perform a digital signature with the Cryptnox Card,
    d) broadcast the signed transaction to the chain endpoint,
    e) reset the internal kWh reading to zero

Part 1: Connecting The Meter pulse out connectors to the Raspberry Pi

The meter will be connected to pins 2 and 40 of the Pi (pin description below)

The Pi 5v output (pin 2) is connected to the pulse out input connector of the Meter (connector 21). The pulse out output connector of the Meter (connector 20) is connected to a 10 K Ohm resistor, and then to pin 40 (GPIO 21) of the Pi (Note: to avoid “floating” and false pulse detection, it is mandatory to activate the internal pull down resistor on GPIO 40. This has been implemented in our code).

The meter will output an idle low voltage output (T_low), and on each pulse, a 90 ms high voltage output (see diagram below).

The Meter pulse out output will be connected to pin. Each pulse will be recorded by a python script.

In our case, the setup looks like that:

Part 2: Initialize a Cryptnox Hardware Wallet Card

You can either use CryptnoxPro command line software from Cryptnox, or you can use an Iphone (more user friendly):

Download Cryptnox Wallet from the Apple store:

Start application -> scan card -> Initialize card and follow instructions.

Save the public address on Icloud for easy recovery for later.

Part 3: deploy the contract KWHContract.sol to the EVM compatible chain of your choice (We used Rinkeby testnet).

  1. Go to `https://remix.ethereum.org`, create a new workspace
  2. Under contracts, rename file as desired, paste code from solidity file from repository into renamed file on remix:
    https://github.com/Cryptnox-IOT-Projects/kWh-Minting-Burning-Meter/tree/main/energyERC20contract/remix
  3. Compile and Deploy
  4. In Deployed Contract, find the addMinter function, paste the public address of the Cryptnox Hardware Wallet card and press “Transact”

Part 4: Install and update the Python script on the Raspberry Pi

Install the script from the repository as per the Readme file, and update contract address in line 29 of pulse_monitor.py

Part 5: Start the electricity production and minting process

You can use consumption from the grid for simulation. Execute the script in the console to start the measuring and minting loop.

Dont forget the arg arguments to the python pulse_monitor.py command, <wallet_public_address> <gpio_number>

The console should look like that when it mints tokens:

On etherscan, the minting transaction should appear:

--

--

Cryptnox Cards

Cryptnox is a swiss based company developing smart cards (hardware wallet) for blockchain