-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimal.py
102 lines (72 loc) · 2.1 KB
/
minimal.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
# -*- coding: utf-8 -*-
from time import sleep
import sys
import cloud4rpi
# these functions will be called by device when sending data
def room_temp():
return 25
def outside_temp():
return 4
def cpu_temp():
return 70
def ip_address():
return '8.8.8.8'
def host_name():
return 'hostname'
def os_name():
return 'osx'
# Put your device token here. To get the token,
# sign up at https://cloud4rpi.io and create a device.
DEVICE_TOKEN = '__YOUR_DEVICE_TOKEN__'
# Change these values depending on your requirements.
DATA_SENDING_INTERVAL = 60 # secs
DIAG_SENDING_INTERVAL = 650 # secs
POLL_INTERVAL = 0.5 # secs
def main():
# Put variable declarations here
# Available types: 'bool', 'numeric', 'string'
variables = {
'Room Temp': {
'type': 'numeric',
'bind': room_temp
},
'Outside Temp': {
'type': 'numeric',
'bind': outside_temp
}
}
# Put system data declarations here
diagnostics = {
'CPU Temp': cpu_temp,
'IP Address': ip_address,
'Host Name': host_name,
'Operating System': os_name
}
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:
diag_timer = 0
data_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
diag_timer -= POLL_INTERVAL
data_timer -= POLL_INTERVAL
sleep(POLL_INTERVAL)
except KeyboardInterrupt:
cloud4rpi.log.info('Keyboard interrupt received. Stopping...')
except Exception as e:
error = cloud4rpi.get_error_message(e)
cloud4rpi.log.error("ERROR! %s %s", error, sys.exc_info()[0])
finally:
sys.exit(0)
if __name__ == '__main__':
main()