forked from allthingstalk/raspberrypi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ATT_Win_Demo.py
55 lines (46 loc) · 2.2 KB
/
ATT_Win_Demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
#important: before running this demo, make certain that you import the library
#'paho.mqtt.client' into python (https://pypi.python.org/pypi/paho-mqtt)
import ATT_IOT as IOT #provide cloud support
from time import sleep #pause the app
In1Name = "Put the name of your sensor" #name of the button
In1Id = "1" #the id of the button, don't uses spaces. required for the att platform
Out1Name = "Put the name of your actuator"
Out1Id = "2"
#callback: handles values sent from the cloudapp to the device
def on_message(id, value):
if id.endswith(Out1Id) == True:
value = value.lower() #make certain that the value is in lower case, for 'True' vs 'true'
if value == "true":
print("true on " + Out1Name)
IOT.send("true", Out1Id) #provide feedback to the cloud that the operation was succesful
elif value == "false":
print("false on " + Out1Name)
IOT.send("false", Out1Id) #provide feedback to the cloud that the operation was succesful
else:
print("unknown value: " + value)
else:
print("unknown actuator: " + id)
#set up the ATT internet of things platform
IOT.on_message = on_message
IOT.ClientId = "put your client id here"
IOT.ClientKey = "put your client key here"
IOT.DeviceId = "put your device id here"
IOT.BrokerUserId = "put your username for the broker here"
#make certain that the device & it's features are defined in the cloudapp
IOT.connect()
IOT.addAsset(In1Id, In1Name, "put your description here", False, "bool")
IOT.addAsset(Out1Id, Out1Name, "put your description here", True, "bool")
IOT.subscribe() #starts the bi-directional communication
nextVal = True;
#main loop: run as long as the device is turned on
while True:
if nextVal == True:
print(In1Name + " activated")
IOT.send("true", In1Id)
nextVal = False
else:
print(In1Name + " deactivated")
IOT.send("false", In1Id)
nextVal = True
sleep(5)