Skip to content

Instantly share code, notes, and snippets.

@huggre
Last active March 17, 2020 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huggre/a33df65c5461c4b8181701cdb0334057 to your computer and use it in GitHub Desktop.
Save huggre/a33df65c5461c4b8181701cdb0334057 to your computer and use it in GitHub Desktop.
Python code for the Integrating physical devices with IOTA - Philips Hue edition tutorial
#!/usr/bin/python
# Imports some Python Date/Time functions
import time
import datetime
# Imports the phue library
from phue import Bridge
# Create a Hue Bridge object
# Replace IP address with the IP address of your Hue Bridge
b = Bridge('192.168.0.71')
# If the app is not registered and the button is not pressed, press the button and call connect() (this only needs to be run a single time)
#b.connect()
# 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'NYZBHOVSMDWWABXSACAJTTWJOQRPVVAWLBSFQVSJSWWBJJLLSQKNZFC9XCRPQSVFQZPBJCJRANNPVMMEZQJRQSVVGZ')]
# Get current address balance at startup and use as baseline for measuring new funds being added.
currentbalance = checkbalance()
lastbalance = currentbalance
# Define some variables
lightbalance = 0
balcheckcount = 0
lightstatus = False
# Assign hue device ID
device_id = 1
# Main loop that executes every 1 second
while True:
# Check for new funds and add to lightbalance when found.
if balcheckcount == 10:
currentbalance = checkbalance()
if currentbalance > lastbalance:
lightbalance = lightbalance + (currentbalance - lastbalance)
lastbalance = currentbalance
balcheckcount = 0
# Manage light balance and light ON/OFF
if lightbalance > 0:
if lightstatus == False:
print("light ON")
b.set_light(device_id,'on', True) # Turn Hue light ON
lightstatus=True
lightbalance = lightbalance -1
else:
if lightstatus == True:
print("light OFF")
b.set_light(device_id,'on', False) # Turn Hue light OFF
lightstatus=False
# Print remaining light balance
print(datetime.timedelta(seconds=lightbalance))
# Increase balance check counter
balcheckcount = balcheckcount +1
# Pause for 1 sec.
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment