Python Setup

If you're following along with a Raspberry Pi, Beaglebone or any other supported small linux computer, we'll use a special library called adafruit_blinka (named after Blinka, the CircuitPython mascot) to provide the layer that translates the CircuitPython hardware API to whatever library the Linux board provides. It's CircuitPython, on Pi!

If you haven't set up Blinka and/or the Adafruit IO Python Library yet on your Raspberry Pi, follow our guide before continuing with the steps on this page:

Enabling I2C

We "talk" to the Si7021 and SGP30 sensors over I2C. To do this, we'll need to set up our Pi's I2C interface. You only have to do this once per Raspberry Pi, the interface is disabled by default.

Once you're done with this and have rebooted, verify you have the I2C devices with the command:

sudo i2cdetect -y 1

The output from running this command should look like the following:

Installing the Adafruit_CircuitPython_SGP30 Sensor Library

We'll need libraries to communicate with the SGP30 sensor. Since we're using Adafruit Blinka (CircuitPython), we can install CircuitPython libraries straight to our Raspberry Pi by using PyPi.

Enter the following command into your terminal to install the Adafruit_CircuitPython_SGP30 library:

sudo pip3 install adafruit-circuitpython-sgp30

Installing the Adafruit_CircuitPython_NeoPixel Library

To control our NeoPixels, we'll use the NeoPixel Library for CircuitPython

Enter the following command into your terminal to install the Adafruit_CircuitPython_NeoPixel library:

sudo pip3 install Adafruit_CircuitPython_NeoPixel

Installing and Configuring picamera

Depending on what distro you are using, picamera may be installed by default. You can check this by entering the following into your terminal:

python3 -c "import picamera"

If you get an error, something like the following:

Download: file
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named 'picamera'

You'll need to install picamera. We're going to enter two commands into our terminal to install it:

sudo apt-get update
sudo apt-get install python-picamera python3-picamera

Once you've got it installed, or if you did not get an error earlier, we'll configure our Raspberry Pi Zero for use with the camera.

Type raspi-config into your terminal and press enter.

You'll be greeted with the following screen. Using your arrow keys, navigate to Interfacing Options and press the enter key.

The first option is to enable the camera, use your arrow keys to navigate to this option and press enter.

You'll be prompted to reboot the Pi. Use the arrow keys to select the Yes option and press enter.

After the Pi reboots, test that the camera works by using the raspistill command:

raspistill -o cam_test.jpg

If this command fails, go back and check that you have enabled the camera using raspi-config and the cable is firmly connected to both the Pi's connector and the camera board's connector. 

Typing ls into the terminal should return the cam_test.jpg file. You can copy this file off of the Pi and view it on your computer, too!

The Pi Camera is set up and ready to use!

Adafruit IO Security Camera Dashboard Setup

Since we're using a camera, we'll need to make a small modification to our dashboard to display the feed with the images captured. 

To do this, we'll create a new feed called picam.

Adafruit IO has a limit when it comes to image feeds - you can not store history for feeds containing large values (like base64-encoded images). 

To avoid this, we'll turn off the feed's history. Navigate to the feed page for the picam and click Feed History

From the popup, disable feed history by setting it to OFF

Python Code Setup

The code for this guide is located on the Adafruit Learning Guides repository on GitHub under the IO House Series

If you'd like to directly load this code on your Pi, type the following in your terminal and press enter:

git clone https://github.com/adafruit/Adafruit_Learning_System_Guides.git

Then, navigate to the directory where the code for this tutorial is located by typing the following into your terminal:

cd Adafruit_Learning_System_Guides/IO_House_Series/Security

Let's check to make sure the Python code is within that directory. To do this, type ls into your terminal and hit enter. You should see io_home_security.py in this directory:

Before running the code, we need to set our Adafruit IO Key(ADAFRUIT_IO_KEYand Adafruit IO Username(ADAFRUIT_IO_USERNAME).

To do this, open the code in your text editor of choice (I'm using nano for this example) by entering the following into your terminal:

nano io_home_security.py

The code should open in the nano editor. Scroll down to the Adafruit_IO_KEY variable and set it to the your Adafruit IO Key. Then, set the Adafruit_IO_USERNAME to your Adafruit IO Username.

When you're done editing the values, save the file by pressing control + x.

When prompted to save the modified buffer, type and press enter.

At the File Name to Write prompt, press enter and you should be directed back to the terminal.

Next, we're going to learn how the code works and run it from our Pi.

This guide was first published on Sep 05, 2018. It was last updated on Sep 05, 2018. This page (Python Setup) was last updated on Dec 14, 2019.