-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSample_Code_for_GrovePi_Sensor.py
More file actions
executable file
·54 lines (39 loc) · 1.43 KB
/
Copy pathSample_Code_for_GrovePi_Sensor.py
File metadata and controls
executable file
·54 lines (39 loc) · 1.43 KB
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
import time
from grovepi import grovepi
from DevIoTGateway.gateway import Gateway
# connect the Grove Light Sensor to analog port A2
# SIG,NC,VCC,GND
light_sensor = 2
# connect the LED to digital port D3
# SIG,NC,VCC,GND
led = 3
grovepi.pinMode(light_sensor, "INPUT")
grovepi.pinMode(led, "OUTPUT")
# turn on/off the led when receive action from DevIot
# action name will be 'on' or 'off'
def trigger_grove_led(sensor_id, action):
print('led get action:' + action.name)
if action.name == 'on':
grovepi.digitalWrite(led, 1)
else:
grovepi.digitalWrite(led, 0)
# create a gateway service instance
# the parameters are: app name, deviot address, mq server address, deviot account
app = Gateway("grovepi_test", "www.ciscodeviot.com", "mqtt.ciscodeviot.com:1883", "")
# register input sensors
# the parameters are: sensor kind, sensor id, sensor display name
app.register("light", "grovelight", "GroveLight")
# register output sensors
# the parameters are: sensor kind, sensor id, sensor display name, action call back function
app.register_action("led", "groveled", "GroveLed", trigger_grove_led)
# run service
app.run()
while True:
try:
# get sensor value
sensor_value = grovepi.analogRead(light_sensor)
# update the sensor value, the parameters are: sensor id, new sensor value
app.set_value("grovelight", sensor_value)
time.sleep(.5)
except IOError:
print ("Error")