-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHello.py
More file actions
130 lines (100 loc) · 3.99 KB
/
Copy pathHello.py
File metadata and controls
130 lines (100 loc) · 3.99 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
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
#!/usr/bin/python
from Tkinter import *
import tkMessageBox
import smbus
import sys
import time
import os
bus = smbus.SMBus(1)
class App():
def __init__(self):
while(True):
try:
#Since we are running this as soon as the OS boots the Display may not be ready yet.
#We will try to initialize the TKinter object in a loop until it succeeds due to display being ready.
self.root = Tk()
time.sleep(2)
break
except:
time.sleep(1)
frame = Frame()
frame.pack()
#Temperature Label
self.tempLabel = Label(frame, text="...", padx=20, pady=20)
self.tempLabel.pack(side=TOP)
#All Relays On Button
self.on_button = Button(frame, text="ON", command=self.turn_on, pady=20)
self.on_button.pack(anchor=W, fill=X)
#All Relays Off Button
self.off_button = Button(frame, text="OFF", command=self.turn_off, pady=20)
self.off_button.pack(anchor=W, fill=X)
#Quit Button
self.button = Button(frame, text="QUIT", fg="red", command=frame.quit, pady=20)
self.button.pack(side=BOTTOM, fill=X)
#Call function to set Temperature label and register timer to update Temperature Reading every 1000 milliseconds
self.updateTemp()
#Try sending initialization command to MCP23008 chip on relay board.
try:
#Set all channels to outputs
bus.write_byte_data(0x20, 0x00, 0x00)
time.sleep(0.5)
#Pull all channels down.
bus.write_byte_data(0x20, 0x06, 0x00)
time.sleep(0.5)
except:
#if initialization of MCP23008 chip on relay board fails then disable on/off buttons and notify user.
tkMessageBox.showerror("Error","Relay not Detected")
self.on_button.config(state="disabled")
self.off_button.config(state="disabled")
#register function to run every 1.5 seconds
self.root.after(1500, self.updateRelays)
self.root.mainloop()
def turn_on(self):
try:
bus.write_byte_data(0x20, 0x09, 0xFF)
except:
tkMessageBox.showerror("Error","Relay not Detected")
print 'Relay Coms failed'
self.on_button.config(state="disabled")
self.off_button.config(state="disabled")
print 'Buttons disabled'
self.root.after(1500, self.updateRelays)
pass
def turn_off(self):
try:
bus.write_byte_data(0x20, 0x09, 0x00)
except:
tkMessageBox.showerror("Error","Relay not Detected")
print 'Relay Coms failed'
self.on_button.config(state="disabled")
self.off_button.config(state="disabled")
print 'Buttons disabled'
self.root.after(1500, self.updateRelays)
pass
def updateTemp(self):
try:
bus.write_byte(0x40, 0xF3)
time.sleep(0.5)
data0 = bus.read_byte(0x40)
data1 = bus.read_byte(0x40)
cTemp = (175.72 * (data0 * 256.0 + data1) / 65536.0) - 46.85
fTemp = cTemp * 1.8 + 32
self.tempLabel['text'] = 'Temperature: %.2f' % fTemp
self.root.after(2000, self.updateTemp)
except:
self.tempLabel['text'] = 'Temp Sensor not Detected'
self.root.after(2000, self.updateTemp)
pass
def updateRelays(self):
print 'relay recovery attempt'
try:
bus.write_byte_data(0x20, 0x00, 0x00)
time.sleep(0.5)
bus.write_byte_data(0x20, 0x06, 0x00)
time.sleep(0.5)
self.on_button.config(state="normal")
self.off_button.config(state="normal")
except:
self.root.after(1500, self.updateRelays)
#initialize App
app = App()