Created
August 14, 2020 14:38
-
-
Save huggre/70ae3d2d543f4dcbbb376199772ea910 to your computer and use it in GitHub Desktop.
Python code for the "Integrating physical devices with IOTA - Peer-to-peer energy trading with IOTA Part 2" tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Import PyOTA library | |
import iota | |
from iota import Address | |
# Import time | |
import time | |
# Import the INA3221 library | |
import SDL_Pi_INA3221 | |
# Define the INA3221 object | |
ina3221 = SDL_Pi_INA3221.SDL_Pi_INA3221(addr=0x40) | |
# Define INA3221 channels | |
LED_CHANNEL = 1 # The LED is connected to CHN1 on the INA3221 | |
SOLAR_PANEL_CHANNEL = 2 # The solar panel is connected to CHN2 on the INA3221 | |
# Hotel owner IOTA seed used for payment transaction | |
# Replace with your own devnet seed | |
seed = b"YOUR9SEED9GOES9HERE" | |
# Solar panel owner reciever address | |
# Replace with your own devnet reciever address | |
addr = "MICIKTVQFXDBZARARUUBXY9OBFDCOFBTYXGOWBWYFZIPYVZVPDLMBVKRF9EUSFASVECRT9PBVBMWMZWADPWZPDDLOD" | |
# URL to IOTA fullnode used when interacting with the Tangle | |
iotaNode = "https://nodes.devnet.iota.org:443" | |
# Define IOTA api object | |
api = iota.Iota(iotaNode, seed=seed) | |
# Function for retrieving current power consumption from the LED | |
def get_current_LED_mW(): | |
try: | |
led_voltage_mV = ina3221.getShuntVoltage_mV(LED_CHANNEL) | |
led_current_mA = ina3221.getCurrent_mA(LED_CHANNEL) | |
led_watts_mW = led_current_mA * led_voltage_mV | |
print("Current LED Power Consumption: %.3f mW" % led_watts_mW) | |
return led_watts_mW | |
except DeviceRangeError as e: | |
print(e) | |
# Function for retrieving current power generation from the solar panel | |
def get_current_PANEL_mW(): | |
try: | |
panel_voltage_mV = ina3221.getShuntVoltage_mV(SOLAR_PANEL_CHANNEL) | |
panel_current_mA = ina3221.getCurrent_mA(SOLAR_PANEL_CHANNEL) | |
panel_watts_mW = panel_current_mA * panel_voltage_mV | |
print("Current solar panel Power generation: %.3f mW" % panel_watts_mW) | |
return panel_watts_mW | |
except DeviceRangeError as e: | |
print(e) | |
# Function for sending IOTA payment | |
def pay(payment_value): | |
# Display preparing payment message | |
print('Preparing payment of ' + str(payment_value) + ' IOTA to address: ' + addr + '\n') | |
# Create transaction object | |
tx1 = iota.ProposedTransaction( address = iota.Address(addr), message = None, tag = iota.Tag(iota.TryteString.from_unicode('HOTELIOTA')), value = payment_value) | |
# Send transaction to tangle | |
print('Sending transaction..., please wait\n') | |
SentBundle = api.send_transfer(depth=3,transfers=[tx1], inputs=None, change_address=None, min_weight_magnitude=9) | |
# Display transaction sent confirmation message | |
print('Transaction sendt...\n') | |
# Define some variables | |
pay_frequency = 60 | |
pay_counter = 0 | |
accumulated_LED_mW = 0.0 | |
accumulated_PANEL_mW = 0.0 | |
# Main loop that executes every 1 second | |
while True: | |
# Check if payment round has been completed | |
if pay_counter == pay_frequency: | |
# Calculate average power consumption for this round | |
average_LED_mW = accumulated_LED_mW / pay_frequency | |
print("*** Average LED Power Consumption: %.3f mW" % average_LED_mW) | |
# Calculate average solar panel power generation for this round | |
average_PANEL_mW = accumulated_PANEL_mW / pay_frequency | |
print("*** Average Solar Panel Power Generation: %.3f mW" % average_PANEL_mW) | |
# Calculate energy price (10 IOTA's devided by the average ammont of power (mW) generated by the solar panel) | |
# The more power generated by the solar panel the lower the price, and vise versa. | |
mW_price = 10 / average_PANEL_mW # | |
# Calculate IOTA payment based on current mW price and average power consumption | |
# Discard any decimals | |
pay_value = int((average_LED_mW * pay_frequency)*mW_price) | |
# Send IOTA payment | |
pay(pay_value) | |
# Reset and prepare for next payment round | |
accumulated_LED_mW = 0.0 | |
accumulated_PANEL_mW = 0.0 | |
pay_counter = 0 | |
# Get current LED power consumption | |
current_LED_mW = get_current_LED_mW() | |
# Add current LED power consumption to accumulated LED power consumption | |
accumulated_LED_mW = accumulated_LED_mW + current_LED_mW | |
# Get current solar panel power generation | |
current_PANEL_mW = get_current_PANEL_mW() | |
# Add current solar panel power generation to accumulated solar panel power generation | |
accumulated_PANEL_mW = accumulated_PANEL_mW + current_PANEL_mW | |
# Increase pay counter | |
pay_counter = pay_counter +1 | |
# Wait for one second before taking a new reading | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment