Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. from Tkinter import *
  2. import threading
  3. from bluetooth import *
  4.  
  5. class GUI():
  6. def __init__(self):
  7. PAD_Y = 5
  8. PAD_X = 5
  9. self.bToggle = True
  10. self.window = Tk()
  11. self.window.title("Check Pi Status")
  12. self.window.protocol("WM_DELETE_WINDOW", self.endApplication)
  13. self.lToggle = Label(self.window, text="True", bg="dark turquoise", fg="black")
  14. self.lToggle.grid(row=0, column=0, padx=PAD_X, pady=PAD_Y, sticky=N + S + E + W)
  15. self.btnToggle = Button(self.window, text="Toggle", command=self.tapToggle, state='normal')
  16. self.btnToggle.grid(row=1, column=0, padx=PAD_X, pady=PAD_Y, sticky=N + S + E + W)
  17. self.ldummy = Label(self.window, text='')
  18. self.ldummy.grid(row=2, column=0)
  19. self.lCountdown = Label(self.window, text='Countdown = 0', bg="blue", fg="white")
  20. self.lCountdown.grid(row=3, column=0, padx=PAD_X, pady=PAD_Y, sticky=N + S + E + W)
  21. self.bKillThread = False
  22. self.read_counter()
  23. self.window.mainloop()
  24.  
  25. def read_counter(self):
  26. global s1
  27. s1.send('Are you alive?')
  28. rec_data = s1.recv(1024)
  29. data = rec_data.decode('utf-8')
  30. print('Received', data)
  31. if data:
  32. self.lToggle.configure(text="Connected", bg="green", fg="white")
  33. self.window.after(1000, self.read_counter)
  34.  
  35. def tapToggle(self):
  36. self.bToggle = not self.bToggle
  37. if self.bToggle:
  38. self.lToggle.configure(text="True", bg="dark turquoise", fg="black")
  39. else:
  40. self.lToggle.configure(text="False", bg="red", fg="white")
  41.  
  42. def endApplication(self):
  43. self.bKillThread = True
  44. self.window.quit()
  45.  
  46. def listen_loop(self):
  47. global bKillThread
  48. while True:
  49. if bKillThread: break
  50. while not self.bConnected: #Loop for connection
  51. if bKillThread: break
  52. try:
  53. HOST1 = "00:15:83:E5:10:C6"
  54. PORT1 = 1 # Slave port
  55.  
  56. s1 = BluetoothSocket(RFCOMM)
  57. s1.connect((HOST1, PORT1))
  58. self.bConnected = True
  59. print("Accepted Connection from Client")
  60. except:
  61. time.sleep(1)
  62.  
  63. bt_status = False # False is off
  64. while self.bConnected:
  65. if bKillThread: break
  66. try:
  67. data = client_socket.recv(1024)
  68. print("Received " + data)
  69. self.silence_counter = 0 # comm is alive
  70. if (data == "ALIVE"):
  71. self.lToggle.configure(text="ONLINE", bg="Green", fg="white")
  72. else:
  73. self.lToggle.configure(text="OFFLINE", bg="red", fg="white")
  74. '''
  75. bt_status = not bt_status # toggle Bluetooth status
  76. if (bt_status == True):
  77. print ("Connected")
  78. print ("Sending ALIVE to master!")
  79. client_socket.send("ALIVE")
  80. else:
  81. print("Not connected")
  82. #data='Alive!'
  83. #self.response(client_socket, data)
  84. '''
  85. except:
  86. self.bConnected = False
  87. client_socket.close()
  88. self.lToggle.configure(text="OFFLINE", bg="red", fg="white")
  89. time.sleep(0.2)
  90.  
  91. '''
  92. #HOST1 = "00:15:83:E5:0D:4F" # bd_address of the slave
  93. HOST1 = "00:15:83:E5:10:C6"
  94. PORT1 = 1 # Slave port
  95.  
  96. s1 = BluetoothSocket(RFCOMM)
  97. s1.connect((HOST1, PORT1))
  98. '''
  99.  
  100. app = GUI()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement