Instructions
Here's the code for the project. The code makes use of the GUI Zero library which
you can read more about in Appendix B.
import threading
import time
from guizero import App, Text
from aq import AQ
aq = AQ()
app = App(title="Air Quality", width=550, height=300,
layout="grid")
def update_readings(): # update fields with new
# temp and eCO2 readings
while True:
temp_c_field.value = str(aq.get_temp())
eco2_field.value = str(aq.get_eco2())
time.sleep(0.5)
t1 = threading.Thread(target=update_readings)
t1.start() # start the thread that updates the readings
aq.leds_automatic()
# define the user interface
Text(app, text="Temp (C)", grid=[0,0], size=20)
temp_c_field = Text(app, text="-", grid=[1,0], size=100)
Text(app, text="eCO2 (ppm)", grid=[0,1], size=20)
eco2_field = Text(app, text="-", grid=[1,1], size=100)
app.display()
To allow readings of temperature and light to take place without interrupting the
workings of the user interface, the threading library is imported. The function
update_readings will loop forever, taking readings every half second and updating
the fields in the window.
The rest of the code provides the user interface fields needed to display the
temperature and eCO2 level. These are laid out as a grid, so that the fields line up.
So, each field is defined with a grid attribute that represent the column and row
positions. So, the field that displays the text Temp (C) is at column 0, row 0 and the
corresponding temperature value (temp_c_field) is at column 1, row 0.
Page 14