Hardware Reference
In-Depth Information
Inside this class, there will be several functions to create different elements of the inter-
face. We are not going to look at the details of every single function, but the most import-
ant one is the function to send the measurement order to the board:
def measure(self):
Inside this function, we simply have to send the m command to the serial port to trigger
the measurement on the Arduino board. Then, we immediately read the data that will be
reflected on the serial port:
ser.write("m")
data = ser.readline()
After that, we have to check whether the received data is valid. We simply check that it is
not empty:
if (data != ""):
If that's not the case, it means we received a message from the board. Remember that we
are going to receive data in the form of temperature,humidity . That's why we
need to use the split() function of Python to separate this data and store it into an ar-
ray:
processed_data = data.split(",")
This means that you now have an array where the temperature measurement is stored in-
side the first cell and the humidity in the second. We can now update the interface accord-
ingly. For example, the temperature field is updated with the following piece of code:
self.temp_data.set("Temperature: " + str(processed_data[0]))
self.temperature.pack()
The same is also done to display humidity. We also want to update the interface automat-
ically. That's why this function is repeated every second. This is done using the following
piece of code:
self.after(1000,self.measure)
Finally, we have to start the application at the end of the Python file. This is done using
the following piece of code:
Search WWH ::




Custom Search