Qwiic Kit for Raspberry Pi V2 Hookup Guide
Displaying 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. This section goes over how to send the serial data to your Raspberry Pi's terminal window. The output can be displayed through a Python Editor's console.
Running Demo via Command Line | Running Demo via Thonny Editor |
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.
Definitions
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 ("BME280 \t | Temperature: %.1f \xb0F" %tempf)
#print ("BME280 \t | Temperature: %.1f \xb0C" %tempc)
print ("BME280 \t | Humidity: %.1f %%RH" %humidity)
print ("BME280 \t | Pressure: %.2f hPa" %(pressure/100))
#print ("BME280 \t | Altitude: %.2f m" %altitudem)
print ("BME280 \t | Altitude: %.2f ft" %altitudef)
print ("VCNL4040 | Distance Value: %.2f " %proximity)
print ("VCNL4040 | Ambient Light: %.2f" %ambient)
print ("SGP40 \t | VOC Index: %.2f" %voc_index)
print (" ") #blank line for easier readability