Qwiic Kit for Raspberry Pi Hookup Guide
Display Data to Your Pi
Once we've read the data from the 3 sensors it is time to display that information on the Pi's console, the OLED screen, and send the information to be displayed by Cayenne. Let's dig into the code a bit deeper.
Comments and Libraries
Starting at the very first line you'll see a line of code that looks like a comment (comments start with
#). This line actually tells us that we will by using Python 3 which is what is used for the Qwiic Pi libraries. After the header comment is a line to import a few libraries to help us keep things clean between Python 2 and Python 3. Next, we are going to add in a few libraries including an mqtt client, our Qwiic library, the time library, and the system library.
If you scroll down a bit you'll, see our Qwiic board definitions. As we add more libraries you'll want to periodically download those updates, each sensor has its own *.py file or module. Inside that file you should find the class definition as well as all the functions. We can then setup that device in our code (the example code has already done this, but if you want to add new sensors from new libraries you'll need to do this yourself). Don't forget the
begin() call to get your sensor up and running.
Then we get to the main part of our code, which is in a
while() loop. This will loop forever (unless we exit out). Here is where we define and read the variables from the sensors as we talked about earlier. We do this every time through the loop so we always have new data. Next, we'll get into printing the data to the screen. We've selected some of the variables to print out as well as the time. When you run this code, this information will all display on the console.
language:python #printing time and some variables to the screen #https://docs.python.org/3/library/time.html #print (time.strftime("%a %b %d %Y %H:%M:%S", time.localtime())) #24-hour time print (time.strftime("%a %b %d %Y %I:%M:%S%p", time.localtime())) #12-hour time print ("BME Temperature %.1f F" %tempf) print ("Humidity %.1f" %humidity) print ("Pressure %.2f Pa" %pressure) print ("Altitude %.2f ft" %altitudef) #print ("CCS Temperature %.1f F" %ccstemp) print ("Distance %.2f " %proximity) print ("Ambient Light %.2f" %ambient) print ("TVOC %.2f" %tvoc) print ("CO2 %.2f" %co2) print (" ") #blank line for easier readability