Qwiic Kit for Raspberry Pi Hookup Guide
Reading the Data
Now that we have everything physically hooked up and ready to go, we can set up our sensors and start reading data. First, the code will need to run through the condition statement to give the sensor values some time to take samples from the environment. At the top of the code, we set up a flag (i.e.
initialize) and counter (i.e.
n) to keep track of whether or not we have just started the Python script. Further down in the main code under the
for loop, we'll take a few readings over a certain period of time. Once we have taken a few values, we'll update the flag so that we can take the reading once through the for loop.
language:python #These values are used to give BME280 and CCS811 some time to take samples initialize=True n=2 . . . if initialize==True: print ("Initializing: BME280 and CCS811 are taking samples before printing and publishing data!") print (" ") else: #print ("Finished initializing") n=1 #set n back to 1 to read sensor data once in loop for n in range (0,n): #print ("n = ", n) #used for debugging for loop . . . #Give some time for the BME280 and CCS811 to initialize when starting up if initialize==True: time.sleep(10) initialize=False
Reading the Sensors Values
Python does not require you to initialize and type your variables, we just get to go ahead and use them. We've highlighted most of the user functions in the main code under the
for loop below as well as a few of the configuration functions. You'll also notice that the CCS811 has a few extra functions that read and calculate the necessary values before we actually grab them.
language:python #Proximity Sensor variables - these are the available read functions proximity = prox.get_proximity() ambient = prox.get_ambient() white = prox.get_white() close = prox.is_close() . . . #BME280 sensor variables pressure = bme.get_reference_pressure() #in Pa altitudef = bme.get_altitude_meters() humidity = bme.read_humidity() tempf = bme.get_temperature_fahrenheit() . . . #CCS811 sensor variables ccs.read_algorithm_results() #updates the TVOC and CO2 values tvoc = ccs.get_tvoc() co2 = ccs.get_co2() . . .
Now that we've read all our data, let's figure out what we want to do with it all. Our different outputs will each display a different set of variables based on the application. Feel free to comment out any variables you are not using in your code using a "
#" or choose to display different variables. The actual code has a lot more variables and functions listed that you probably won't need.