forked from allthingstalk/raspberrypi-python-client
/
ATT_RPI_Shield_Demo.py
63 lines (53 loc) · 2.49 KB
/
ATT_RPI_Shield_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
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Important: before running this demo, make certain that grovepi & ATT_IOT
# are in the same directory as this script, or installed so that they are globally accessible
import grovepi #provides pin support
import ATT_IOT as IOT #provide cloud support
from time import sleep #pause the app
#set up the ATT internet of things platform
IOT.DeviceId = "YourDeviceIdHere"
IOT.ClientId = "YourClientIdHere"
IOT.ClientKey = "YourClientKeyHere"
sensorName = "Button" #name of the sensor
sensorPrev = False #previous value of the sensor (only send a value when a change occured)
sensorPin = 2
sensorId = "1" #the id of the button, don't uses spaces. required for the att platform
actuatorName = "Diode"
actuatorPin = 4
actuatorId = "2"
#set up the pins
grovepi.pinMode(sensorPin,"INPUT")
grovepi.pinMode(actuatorPin,"OUTPUT")
#callback: handles values sent from the cloudapp to the device
def on_message(id, value):
if id.endswith(actuatorId) == True:
value = value.lower() #make certain that the value is in lower case, for 'True' vs 'true'
if value == "true":
grovepi.digitalWrite(actuatorPin, 1)
IOT.send("true", actuatorId) #provide feedback to the cloud that the operation was succesful
elif value == "false":
grovepi.digitalWrite(actuatorPin, 0)
IOT.send("false", actuatorId) #provide feedback to the cloud that the operation was succesful
else:
print("unknown value: " + value)
else:
print("unknown actuator: " + id)
IOT.on_message = on_message
#make certain that the device & it's features are defined in the cloudapp
IOT.connect()
IOT.addAsset(sensorId, sensorName, "Push button", False, "bool")
IOT.addAsset(actuatorId, actuatorName, "Light Emitting Diode", True, "bool")
IOT.subscribe() #starts the bi-directional communication
#main loop: run as long as the device is turned on
while True:
if grovepi.digitalRead(sensorPin) == 1:
if sensorPrev == False:
print(sensorName + " activated")
IOT.send("true", sensorId)
sensorPrev = True
elif sensorPrev == True:
print(sensorName + " deactivated")
IOT.send("false", sensorId)
sensorPrev = False
sleep(.3)