This allows people to pay for their electricity usage on a pay as you go basis. Payments are sent through the IOTA Tangle.
It is of paramount importance that we find cleaner energy sources. But we also have to use our current energy sources more efficiently. This project proposes to use the IOTA Tangle to charge or keep track of individual energy usage. This will allow everyone from large corporations to house owners to efficiently manage their energy requirements.
What is IOTA?IOTA is a cryptocurrency designed for the Internet of Things! IOTA looks to be Fee less and scalable. This alone makes IOTA incredibly promising as a technology for a huge number of applications.
IOTA has the following important features.
- Scalability
- Decentralization
- NO TRANSACTION FEES
- Quantum Computing Protection
If you want to dive in further, I recommend this great tutorial series on YouTube.
HardwareIn order to monitor the power usage, I purchased a Modbus enabled energy meter from ebay. This allowed me to measure parameters such as AC Voltage, Current, Power and Energy using modbus commands from the Raspberry Pi.
You will need to setup Raspbian on your Raspberry Pi. https://www.raspberrypi.org/documentation/installation/installing-images/
You will also need the following packages for iota (we will be using the python library for IOTA), modbus communication, and data visualization. Use the following commands to install them.
(This project was done using Python 3.6)
sudo pip3 install pyota flask falsk-socketio gevent minimalmodbus
As for the hardware part, I used a spare electrical socket and a sun box. This added an extra layer of safety since no electrical connections are exposed.
The wiring was done in the following way.
The project is built using Flask and Flask-SocketIO, which allows to create a continuously updating web dashboard that the user can monitor. A QR code for the IOTA address is also visible in the webpage, which allows users to 'top up'.
We start by importing the needed libraries. Take note of the checkbalance() function, which allows us to get the balance of the provided address. I have provided my address which I received by creating a wallet using Trinity. (This can be the address of the energy company/ landlord)
##IOTA
# Imports the PyOTA library
from iota import Iota
from iota import Address
# Function for checking address balance on the IOTA tangle.
def checkbalance():
print("Checking balance")
gb_result = api.get_balances(address)
balance = gb_result['balances']
return (balance[0])
# URL to IOTA fullnode used when checking balance
iotaNode = "https://nodes.thetangle.org:443"
# Create an IOTA object
api = Iota(iotaNode, "")
# IOTA address to be checked for new light funds
# IOTA addresses can be created using the IOTA Wallet
address = [Address(b'LMXNOWTOHIRSFFTJVIYYFOVG9LRPNKWSFZOBTNLKBQYDRANWURGMHSMAHPAAPDFGPWAERP9XJRFCCZMAWVYMDBBEZD')]
# Get current address balance at startup and use as baseline for measuring new funds being added.
startingbalance = checkbalance()
devicestatus = False
##MODBUS
import minimalmodbus
instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 1, mode = 'rtu') # port name, slave address - Use multiple objects to talk with multiple power meters
instrument.serial.baudrate = 9600
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4,GPIO.OUT)
GPIO.output(4,GPIO.HIGH)
prev_voltage = 0
prev_current = 0
prev_energy = 0
prev_power = 0
##Flask and SocketIO
from threading import Lock
from flask import Flask, render_template, session, request
from flask_socketio import SocketIO, emit, join_room, leave_room, \
close_room, rooms, disconnect
async_mode = None
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
thread = None
thread_lock = Lock()
Parameters such as current, power and energy are monitored in a separate thread. (Take note of the functions get_current, get_power and get_energy. These are used to issue modbus commands)
def background_thread():
global startingbalance
balchecker = 0
count = 0
deviceBalance = 0
currentbalance = 0
usedenergy = 0
firstflag = True
while True:
# Check for new funds and add to lightbalance when found.
if balchecker == 10:
currentbalance = checkbalance()
if currentbalance > startingbalance :
deviceBalance = currentbalance - startingbalance
startingbalance = currentbalance
balchecker = 0
balchecker = balchecker + 1
if(deviceBalance > 0) :
if devicestatus == False:
print("device ON")
GPIO.output(4,GPIO.LOW)
devicestatus=True
for i in range (0,10):
startingenergy = get_energy()
socketio.sleep(0.5)
newnenergy = get_energy()
usedenergy = newnenergy - startingenergy
deviceBalance = deviceBalance - usedenergy
if deviceBalance <0 :
deviceBalance = 0
socketio.emit('my_response',
{'iotabalance': deviceBalance, 'current' : get_current(), 'power' :get_power() , 'energy' :usedenergy},
namespace='/test')
else :
print("device OFF")
deviceBalance = 0
GPIO.output(4,GPIO.HIGH)
devicestatus=False
socketio.emit('my_response',
{'iotabalance': deviceBalance, 'current' : 0, 'power' : 0, 'energy' :usedenergy},
namespace='/test')
Every 10 cycles, the balance is polled to see whether it has been increased by the user. If there is a change, the increased amount is added to 'deviceBalance'
The electrical device plugged into the special socket is allowed to run till deviceBalance remains greater than zero. In this case, I am taking 1 IOTA for 1 Watt Hour of Energy Used.
We use routes to render out webpage. The thread is started when a client is connected to the server (when the webpage is opened on a browser)
@app.route('/')
def index():
return render_template('index.html', async_mode=socketio.async_mode)
@socketio.on('connect', namespace='/test')
def test_connect():
global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(background_thread)
emit('my_response', {'iotabalance': 0, 'current' : 0, 'power' :0 , 'energy' :0})
@socketio.on('disconnect', namespace='/test')
def test_disconnect():
print('Client disconnected', request.sid)
Finally the modbus handler functions are defined.
def get_energy():
global prev_energy
try:
energy_low = instrument.read_register(0x0005, numberOfDecimals=0, functioncode=4, signed=False)
energy_high = instrument.read_register(0x0006, numberOfDecimals=0, functioncode=4, signed=False)
energy = (energy_high << 8 | energy_low)
prev_energy = energy
return energy
except ValueError:
return prev_energy
except IOError:
return prev_energy
def get_power():
global prev_power
try:
power_low = instrument.read_register(0x0003, numberOfDecimals=0, functioncode=4, signed=False)
power_high = instrument.read_register(0x0004, numberOfDecimals=0, functioncode=4, signed=False)
power = (power_high << 8 | power_low)/10.0
prev_power = power
return power
except ValueError:
return prev_power
except IOError:
return prev_power
def get_current():
global prev_current
try:
current_low = instrument.read_register(0x0001, numberOfDecimals=0, functioncode=4, signed=False)
current_high = instrument.read_register(0x0002, numberOfDecimals=0, functioncode=4, signed=False)
current = (current_high << 8 | current_low)/1000.0
prev_current = current
return current
except ValueError:
return prev_current
except IOError:
return prev_current
Exception handling was done since the Energy Meter would stop responding at certain times.
Finally we run our app on the specified host and port. Be sure to provide the IP of the raspberry pi in order to access the webpage from other devices int he same network.
if __name__ == '__main__':
socketio.run(app,host = '192.168.8.106', port = 4500, debug=True)
The index.html file required for this project is available at the end of this article.
You can access the web page by directing your browser to <your Pi's IP address> :4500/
ConclusionIn these times, people are not concerned about how they individually contribute to the inefficient use of energy. By charging for power on a USER LEVEL, people will be more inclined to get the maximum usage without wasting power. This solution can be improved and implemented on offices, corporations and even factories, which allows top management to keep track of the energy usage of individual employees/divisions/machinery.
Comments