Skip to content

Instantly share code, notes, and snippets.

@bjyurkovich
Last active October 20, 2019 00:49
Show Gist options
  • Select an option

  • Save bjyurkovich/22d11a286cd85a0b2a893948fa44766f to your computer and use it in GitHub Desktop.

Select an option

Save bjyurkovich/22d11a286cd85a0b2a893948fa44766f to your computer and use it in GitHub Desktop.
from time import time, sleep
from iotery_embedded_python_sdk import Iotery
import os
# Need to specify the Iotery Team ID (I found this on the system page: https://iotery.io/system)
TEAM_ID = os.getenv("IOTERY_TEAM_ID")
# Instantiate the Iotery Connector object
light_connector = Iotery()
auth_data = {
"key": "FRED_THE_LIGHT",
"serial": "FRED_THE_LIGHT",
"secret": "FRED_THE_LIGHT_SECRET",
"teamUuid": TEAM_ID
}
# Get a JWT token from Iotery to be used in subsequent calls
light_auth = light_connector.getDeviceTokenBasic(data=auth_data)
# Set the token
light_connector.set_token(light_auth["token"])
# Get info about Fred
fred = light_connector.getMe()
light_is_on = True
while True:
timestamp = int(time())
if light_is_on:
print("I am shining brightly!")
else:
print("I am off.")
# Data to send to Iotery
data = {
"packets": [{
"timestamp": timestamp,
"deviceUuid": fred["uuid"],
"data":{
"light_state": 1 if light_is_on else 0
}
}]
}
# Report to Iotery
response = light_connector.postData(
deviceUuid=fred["uuid"], data=data)
# Loop through all command instances from response
for command_instance in response["unexecutedCommands"]["device"]:
# If the command instance to turn Fred off
if command_instance["commandTypeEnum"] == "TURN_LIGHT_OFF":
light_is_on = False
# Sets the command instance as executed to keep Fred an Iotery on the same page
light_connector.setCommandInstanceAsExecuted(
commandInstanceUuid=command_instance["uuid"], data={"timestamp": timestamp})
print("command executed! Light Off")
if command_instance["commandTypeEnum"] == "TURN_LIGHT_ON":
light_is_on = True
light_connector.setCommandInstanceAsExecuted(
commandInstanceUuid=command_instance["uuid"], data={"timestamp": timestamp})
print("command executed! Light On")
# Sleep for 1 second
sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment