-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
178 lines (140 loc) · 4.4 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import streams
import json
import requests
import mcu
import pwm
from wireless import wifi
from espressif.esp32net import esp32wifi as wifi_driver
from riverdi.displays.bt81x import ctp50
from bridgetek.bt81x import bt81x
from okdo.iot import iot
from okdo.iot import mqtt_client
import gui
# image resources
new_resource('images/gui_riverdi_logo.png')
# wifi credentials
ssid = "xx" # this is the SSID of the WiFi network
wifiPWD = "xx" # this is the Password for WiFi
# okdo cloud
device_id = "" # this is the device identifier. Can be obtained from the OKDO cloud dashboard
device_token = "" # this is the device token. Can be obtained from the OKDO cloud dashboard
# RPi statistics
cpu_load_cur = 0
cpu_temp_main = 0
mem_info_free = 0
mem_swap_free = 0
# RPi processes
rpi_top_proc_1 = "-|-|-"
rpi_top_proc_2 = "-|-|-"
rpi_top_proc_3 = "-|-|-"
rpi_top_proc_4 = "-|-|-"
# RPi sensors
rpi_temp_1 = 0
rpi_temp_2 = 0
rpi_temp_3 = 0
rpi_temp_4 = 0
# screen layouts/stages:
# 1 - mainmenu - sensors
# 2 - mainmenu - statistics
# 3 - mainmenu - processes
# open serial channel to display debug messages
streams.serial()
# pwm buzzer
pinMode(D23.PWM,OUTPUT)
# short beep
def beep():
pwm.write(D23.PWM,1000,1000//2,MICROS)
sleep(50)
pwm.write(D23.PWM,0,0)
# okDO handlers
def okdo_cb(asset,value, previous_value):
global cpu_load_cur
global cpu_temp_main
global mem_info_free
global mem_swap_free
global rpi_top_proc_1
global rpi_top_proc_2
global rpi_top_proc_3
global rpi_top_proc_4
global rpi_temp_1
global rpi_temp_2
global rpi_temp_3
global rpi_temp_4
if (asset == 'rpi_cpu_load_cur'):
cpu_load_cur = value
elif (asset == 'rpi_cpu_temp_main'):
cpu_temp_main = value
elif (asset == 'rpi_mem_info_free'):
mem_info_free = value
elif (asset == 'rpi_mem_swap_free'):
mem_swap_free = value
elif (asset == 'rpi_top_proc_1'):
rpi_top_proc_1 = value
elif (asset == 'rpi_top_proc_2'):
rpi_top_proc_2 = value
elif (asset == 'rpi_top_proc_3'):
rpi_top_proc_3 = value
elif (asset == 'rpi_top_proc_4'):
rpi_top_proc_4 = value
elif (asset == 'rpi_temp_1'):
rpi_temp_1 = value
elif (asset == 'rpi_temp_2'):
rpi_temp_2 = value
elif (asset == 'rpi_temp_3'):
rpi_temp_3 = value
elif (asset == 'rpi_temp_4'):
rpi_temp_4 = value
# buttons handler
def pressed(tag, tracked, tp):
global screenLayout
if ((tag > 0) and (tag < 3)):
screenLayout = tag
# init display
bt81x.init(SPI0, D4, D33, D34)
# one callback for all tags
bt81x.touch_loop(((-1, pressed), ))
# [0] show logo
gui.loadImage('gui_riverdi_logo.png')
gui.showLogo()
sleep(4000)
# [1] show spinner - connecting with predefined WiFi network
gui.showSpinner("Connecting with predefined WiFi network...")
# [2] init wifi driver
wifi_driver.auto_init()
# [3] connect to predefined wifi network
for _ in range(0,5):
try:
wifi.link(ssid,wifi.WIFI_WPA2,wifiPWD)
break
except:
gui.showSpinner("Trying to reconnect...")
else:
gui.showSpinner("Connection Error - restarting...")
mcu.reset()
# [4] connect and setup connection with OKdo cloud
device = iot.Device(device_id,device_token,mqtt_client.MqttClient)
device.connect()
# [5] define the callbacks to call when an OKdo asset command is received
device.watch_command("rpi_mem_swap_free", okdo_cb)
device.watch_command("rpi_mem_info_free", okdo_cb)
device.watch_command("rpi_cpu_temp_main", okdo_cb)
device.watch_command("rpi_cpu_load_cur", okdo_cb)
device.watch_command("rpi_top_proc_1", okdo_cb)
device.watch_command("rpi_top_proc_2", okdo_cb)
device.watch_command("rpi_top_proc_3", okdo_cb)
device.watch_command("rpi_top_proc_4", okdo_cb)
device.watch_command("rpi_temp_1", okdo_cb)
device.watch_command("rpi_temp_2", okdo_cb)
device.watch_command("rpi_temp_3", okdo_cb)
device.watch_command("rpi_temp_4", okdo_cb)
device.run()
# [6] mainloop
screenLayout = 1
while True:
if (screenLayout == 1):
gui.showSensorsScreen(rpi_temp_1, rpi_temp_2, rpi_temp_3, rpi_temp_4)
elif (screenLayout == 2):
gui.showStatisticsScreen(cpu_load_cur, cpu_temp_main, mem_info_free, mem_swap_free)
elif (screenLayout == 3):
gui.showProcessMonitor(rpi_top_proc_1,rpi_top_proc_2,rpi_top_proc_3,rpi_top_proc_4)
sleep(10)