Qwiic Kit for Raspberry Pi V2 Hookup Guide

Pages
Contributors: M-Short, bboyho
Favorited Favorite 4

Reading the Sensor 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. The variable u is used as a counter for logging data with Cayenne after a certain period of time later on in the code.

language:python
#These values are used to give BME280 and SGP40 some time to take samples and log data to Cayenne
initialize=True
n=2
u=0
.
.
.

        if initialize==True:
            print ("Initializing: BME280 and SGP40 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 SGP40 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.

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()
            .
            .
            .


            #SGP40 sensor variable
            voc_index = my_sgp40.get_VOC_index()
            .
            .
            .

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.