-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcontrol.py
150 lines (116 loc) · 3.6 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
from time import sleep
import sys
import random
import cloud4rpi
import ds18b20
import rpi
import RPi.GPIO as GPIO # pylint: disable=F0401
# Put your device token here. To get the token,
# sign up at https://cloud4rpi.io and create a device.
DEVICE_TOKEN = '__YOUR_DEVICE_TOKEN__'
# Constants
LED_PIN = 12
# Change these values depending on your requirements.
DATA_SENDING_INTERVAL = 60 # secs
DIAG_SENDING_INTERVAL = 650 # secs
POLL_INTERVAL = 0.5 # 500 ms
LOCATIONS = [
{'lat': 51.500741, 'lng': -0.124626}, # Big Ben, London, United Kingdom
{'lat': 40.689323, 'lng': -74.044503} # Statue of Liberty, New York, USA
]
# Configure GPIO library
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED_PIN, GPIO.OUT)
# Handler for the button or switch variable
def led_control(value=None):
GPIO.output(LED_PIN, value)
return GPIO.input(LED_PIN)
def listen_for_events():
# Write your own logic here
result = random.randint(1, 5)
if result == 1:
return 'RING'
if result == 5:
return 'BOOM'
return 'IDLE'
def get_location():
return random.choice(LOCATIONS)
def sensor_not_connected():
return 'Sensor not connected'
def main():
# Load w1 modules
ds18b20.init_w1()
# Detect ds18b20 temperature sensors
ds_sensors = ds18b20.DS18b20.find_all()
# Put variable declarations here
# Available types: 'bool', 'numeric', 'string', 'location'
variables = {
'Room Temp': {
'type': 'numeric' if ds_sensors else 'string',
'bind': ds_sensors[0] if ds_sensors else sensor_not_connected
},
# 'Outside Temp': {
# 'type': 'numeric' if ds_sensors else 'string',
# 'bind': ds_sensors[1] if ds_sensors else get_empty_value
# },
'LED On': {
'type': 'bool',
'value': False,
'bind': led_control
},
'CPU Temp': {
'type': 'numeric',
'bind': rpi.cpu_temp
},
'STATUS': {
'type': 'string',
'bind': listen_for_events
},
'Location': {
'type': 'location',
'bind': get_location
}
}
diagnostics = {
'CPU Temp': rpi.cpu_temp,
'IP Address': rpi.ip_address,
'Host': rpi.host_name,
'Operating System': rpi.os_name,
'Client Version:': cloud4rpi.__version__,
}
device = cloud4rpi.connect(DEVICE_TOKEN)
# Use the following 'device' declaration
# to enable the MQTT traffic encryption (TLS).
#
# tls = {
# 'ca_certs': '/etc/ssl/certs/ca-certificates.crt'
# }
# device = cloud4rpi.connect(DEVICE_TOKEN, tls_config=tls)
try:
device.declare(variables)
device.declare_diag(diagnostics)
device.publish_config()
# Adds a 1 second delay to ensure device variables are created
sleep(1)
data_timer = 0
diag_timer = 0
while True:
if data_timer <= 0:
device.publish_data()
data_timer = DATA_SENDING_INTERVAL
if diag_timer <= 0:
device.publish_diag()
diag_timer = DIAG_SENDING_INTERVAL
sleep(POLL_INTERVAL)
diag_timer -= POLL_INTERVAL
data_timer -= POLL_INTERVAL
except KeyboardInterrupt:
cloud4rpi.log.info('Keyboard interrupt received. Stopping...')
except Exception as e:
error = cloud4rpi.get_error_message(e)
cloud4rpi.log.exception("ERROR! %s %s", error, sys.exc_info()[0])
finally:
sys.exit(0)
if __name__ == '__main__':
main()