I am trying to create a GUI python interface to display the value from sensors which are connected to an arduino via serial communication. For this interface I am using a class named application().
I first created the GUI application using Tkinter which is functional. That is to say that the different elements (frame, widgets, etc.) are displayed correctly.
Then I created a function (get_data), to retrieve the data from the arduino. Each byte I retrieve the value and store it in an array with the associated key (the name of the sensors). Then I retrieve the array for the parse and assign the values in variables (integer or float) so that I can retrieve them in the GUI via the function (update_data).
To avoid having to worry between displaying and looping data, I decided to use the library threading to run the get_data() and update_data() functions continuously in other threads.
when I launch the application, the code loops on get_data() (see the print below), but the Tkinter interface does not launch.
Wait, sending data
Wait, sending data
Wait, sending data
Wait, sending data
Wait, sending data
Wait, sending data
Wait, sending data
Wait, sending data
Wait, sending data
Wait, sending data
Wait, sending data
Wait, sending data
Wait, sending data
Wait, sending data
Wait, sending data
Wait, sending data
Here are the two functions to retrieve data:
def get_data(self):
ser = serial.Serial('/dev/cu.usbmodem14201', 9600)
ser.flushInput()
index=0
self.currentDataArray = np.array(np.zeros([index]))
while True:
try:
for c in ser.readline():
current_decoded_bytes = float(ser_bytes[0:len(ser_bytes)-2].decode("utf-8"))
print(current_decoded_bytes)
self.currentDataArray = np.append(self.currentDataArray,current_decoded_bytes)
if c == '\n':
return self.currentDataArray
self.currentDataArray = np.array(np.zeros([index]))
except:
print("Wait, sending data")
pass
def Update_value(self, currentDataArray,update_period):
print("updating data")
new = time.time()
print(self.currentDataArray)
analogValue=float()
VoltageEcSensor=float()
tempWaterSensor=float()
TemperatureB=float()
HumidityB=float()
pHvalue=float()
EcSensorValue=float()
extractorStatement=int()
ligthStatement=int()
intractorStatement=int()
fanStatement=int()
while(1):
currentDataArray.all()
try:
self.analogValue=self.currentDataArray[0]
self.VoltageEcSensor=self.currentDataArray[1]
self.tempWaterSensor=self.currentDataArray[2]
self.TemperatureB=self.currentDataArray[3]
self.HumidityB=self.currentDataArray[4]
self.pHvalue=self.currentDataArray[5]
self.EcSensorValue=self.currentDataArray[6]
self.extractorStatement=self.currentDataArray[7]
self.ligthStatement=self.currentDataArray[8]
self.intractorStatement=self.currentDataArray[9]
self.fanStatement=self.currentDataArray[10]
except:
pass
if time.time() - new >= update_period:
self.analogValue=0
self.VoltageEcSensor=0
self.tempWaterSensor=0
self.TemperatureB=0
self.HumidityB=0
self.pHvalue=0
self.EcSensorValue=0
self.extractorStatement=0
self.ligthStatement=0
self.intractorStatement=0
self.fanStatement=0
new = time.time()
pass
Here is a function to acquire data continuously using other threads:
def threading(self):
multi = threading.Thread(target = self.get_data())
# multi.deamon = True
multi.start()
multi2 = threading.Thread(target = self.Update_value(self.currentDataArray, self.update_period))
# multi2.deamon = True
multi2.start()
This is how I initialize in the tkinter interface:
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
update_period = 5
tempBox=self.TemperatureB
tempExtBox=23
humidityBox=self.HumidityB
TempSolution=self.tempWaterSensor
pH_solution=self.pHvalue
Ec_solution=self.EcSensorValue
Global_conso=110
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
# set all width and height for each Pane
width_MODEpane=screen_width/3
height_MODEpane=screen_height/4
self.MainWindow(screen_height, screen_height)
if __name__ == "__main__":
app = Application()
app.threading()
app.title("Automate GUI app")
app.mainloop()
I think I'm not using correctly the threading
library, do you have any suggestion to solve this issue?