Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Tkinter import *
- import threading
- from bluetooth import *
- class GUI():
- def __init__(self):
- PAD_Y = 5
- PAD_X = 5
- self.bToggle = True
- self.window = Tk()
- self.window.title("Check Pi Status")
- self.window.protocol("WM_DELETE_WINDOW", self.endApplication)
- self.lToggle = Label(self.window, text="True", bg="dark turquoise", fg="black")
- self.lToggle.grid(row=0, column=0, padx=PAD_X, pady=PAD_Y, sticky=N + S + E + W)
- self.btnToggle = Button(self.window, text="Toggle", command=self.tapToggle, state='normal')
- self.btnToggle.grid(row=1, column=0, padx=PAD_X, pady=PAD_Y, sticky=N + S + E + W)
- self.ldummy = Label(self.window, text='')
- self.ldummy.grid(row=2, column=0)
- self.lCountdown = Label(self.window, text='Countdown = 0', bg="blue", fg="white")
- self.lCountdown.grid(row=3, column=0, padx=PAD_X, pady=PAD_Y, sticky=N + S + E + W)
- self.bKillThread = False
- self.read_counter()
- self.window.mainloop()
- def read_counter(self):
- global s1
- s1.send('Are you alive?')
- rec_data = s1.recv(1024)
- data = rec_data.decode('utf-8')
- print('Received', data)
- if data:
- self.lToggle.configure(text="Connected", bg="green", fg="white")
- self.window.after(1000, self.read_counter)
- def tapToggle(self):
- self.bToggle = not self.bToggle
- if self.bToggle:
- self.lToggle.configure(text="True", bg="dark turquoise", fg="black")
- else:
- self.lToggle.configure(text="False", bg="red", fg="white")
- def endApplication(self):
- self.bKillThread = True
- self.window.quit()
- def listen_loop(self):
- global bKillThread
- while True:
- if bKillThread: break
- while not self.bConnected: #Loop for connection
- if bKillThread: break
- try:
- HOST1 = "00:15:83:E5:10:C6"
- PORT1 = 1 # Slave port
- s1 = BluetoothSocket(RFCOMM)
- s1.connect((HOST1, PORT1))
- self.bConnected = True
- print("Accepted Connection from Client")
- except:
- time.sleep(1)
- bt_status = False # False is off
- while self.bConnected:
- if bKillThread: break
- try:
- data = client_socket.recv(1024)
- print("Received " + data)
- self.silence_counter = 0 # comm is alive
- if (data == "ALIVE"):
- self.lToggle.configure(text="ONLINE", bg="Green", fg="white")
- else:
- self.lToggle.configure(text="OFFLINE", bg="red", fg="white")
- '''
- bt_status = not bt_status # toggle Bluetooth status
- if (bt_status == True):
- print ("Connected")
- print ("Sending ALIVE to master!")
- client_socket.send("ALIVE")
- else:
- print("Not connected")
- #data='Alive!'
- #self.response(client_socket, data)
- '''
- except:
- self.bConnected = False
- client_socket.close()
- self.lToggle.configure(text="OFFLINE", bg="red", fg="white")
- time.sleep(0.2)
- '''
- #HOST1 = "00:15:83:E5:0D:4F" # bd_address of the slave
- HOST1 = "00:15:83:E5:10:C6"
- PORT1 = 1 # Slave port
- s1 = BluetoothSocket(RFCOMM)
- s1.connect((HOST1, PORT1))
- '''
- app = GUI()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement