-
Notifications
You must be signed in to change notification settings - Fork 2
/
control.py
122 lines (98 loc) · 2.91 KB
/
control.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
#
# Cloud4RPi Example for Omega2
# ============================
#
# This example demonstrates how to use Cloud4RPi service to control Omega2 LEDs.
#
# For complete instructions on how to run this example, refer
# to the [How To](https://cloud4rpi.github.io/docs/howto/) article.
from os import uname
from socket import gethostname
from time import sleep
import sys
import cloud4rpi
import omega2
# Put your device token here. To get the token,
# sign up at https://cloud4rpi.io and create a device.
DEVICE_TOKEN = '__YOUR_DEVICE_TOKEN__'
DATA_SENDING_INTERVAL = 60 # seconds
class RGB_LED(object):
def __init__(self, omega2_instance):
self.o2 = omega2_instance
self.R, self.G, self.B = 0, 0, 0
def init(self):
for _, pin in self.o2.RGB_pins.items():
self.o2.gpio_dir_out_1(pin) # HIGH = OFF
def set_R(self, value):
if value == self.R:
return value
self.R = value
self.o2.RGB_color(self.R, self.G, self.B)
return value
def set_G(self, value):
if value == self.G:
return value
self.G = value
self.o2.RGB_color(self.R, self.G, self.B)
return value
def set_B(self, value):
if value == self.B:
return value
self.B = value
self.o2.RGB_color(self.R, self.G, self.B)
return value
o2 = omega2.Omega2()
rgb = RGB_LED(o2)
def main():
rgb.init()
# Put variable declarations here
variables = {
'Omega LED': {
'type': 'bool',
'value': False,
'bind': o2.led_control
},
'RGB LED - Red': {
'type': 'numeric',
'value': 0,
'bind': rgb.set_R
},
'RGB LED - Green': {
'type': 'numeric',
'value': 0,
'bind': rgb.set_G
},
'RGB LED - Blue': {
'type': 'numeric',
'value': 0,
'bind': rgb.set_B
}
}
# Put system data declarations here
diagnostics = {
'Host': gethostname(),
'Operating System': " ".join(uname()),
'Omega2 Version': 'Omega2 Plus' if 'p' in o2.version else 'Omega2',
'Client Version:': cloud4rpi.__version__
}
device = cloud4rpi.connect(DEVICE_TOKEN)
device.declare(variables)
device.declare_diag(diagnostics)
device.publish_config()
# Adds a 1 second delay to ensure device variables are created
sleep(1)
try:
device.publish_diag()
while True:
device.publish_data()
sleep(DATA_SENDING_INTERVAL)
except KeyboardInterrupt:
cloud4rpi.log.info('Keyboard interrupt received. Stopping...')
sys.exit(0)
except Exception as e:
error = cloud4rpi.get_error_message(e)
cloud4rpi.log.error("ERROR! %s %s", error, sys.exc_info()[0])
sys.exit(1)
if __name__ == '__main__':
main()