Python Code

The first large chunk of code creates an instance of the Adafruit IO REST client and sets up the feeds we created earlier in the guide. Then, it creates an I2C interface object and passes it to the sensor.

Download: file
# Create an instance of the REST client
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# set up Adafruit IO feeds
temperature = aio.feeds('temperature')
humidity = aio.feeds('humidity')
outdoor_lights = aio.feeds('outdoor-lights')
indoor_lights = aio.feeds('indoor-lights')

# create an i2c interface object
i2c = I2C(SCL, SDA)

# instanciate the sensor object
sensor = adafruit_si7021.SI7021(i2c)

In the while True loop, we obtain the temperature and humidity data from the sensor. Then, we print out and send this data to Adafruit IO using aio.send()

Download: file
# get data from the si7021 sensor
temperature_data = sensor.temperature
humidity_data = sensor.relative_humidity

# send data to adafruit io
print('> Temperature: ', int(temperature_data))
aio.send(temperature.key, int(temperature_data))
print('> Humidity :', int(humidity_data))
aio.send(temperature.key, int(humidity_data))

Next, we get the hex value from the color picker on the dashboard by using the aio.receive() method. The hex is then converted to individual RGB values using the io-python-client library's helper functions. 

Download: file
# get the indoor light color picker feed
indoor_light_data = aio.receive(indoor_lights.key)
# convert the hex values to RGB values
red = aio.toRed(indoor_light_data.value)
green = aio.toGreen(indoor_light_data.value)
blue = aio.toBlue(indoor_light_data.value)

To set the color of the NeoPixel Strip or Jewel, we use a loop to set the color of each pixel. 

Download: file
    # set the jewel's color
    for i in range(JEWEL_PIXEL_COUNT):
        pixels[i] = (red, green, blue)
        pixels.show()

Enter the following command into your terminal to run the code:

sudo python3 io_house_light_temp.py

Note: The NeoPixel library will not work without prepending the script with sudo, as peripherals need to be run as root on the Raspberry Pi.

You should see the code sending the humidity and temperature values to the Adafruit IO dashboard:

Download: file
Adafruit IO Home: Lights and Climate Control
> Temperature:  24
> Humidity : 57

Visit the IO-Home Dashboard you created on Adafruit IO. You should see the temperature and humidity gauges reflect the values sent from your code.

Change the color picker on the Adafruit IO dashboard and you should see the colors change!

Download: file
< Indoor Light HEX:  #52ff00
< Outdoor Light HEX:  #000000

Code

"""
'io_house_light_temp.py'
=======================================
Control lights and monitor temperature
and humidity in a model smart house.

Learning System Guide: https://learn.adafruit.com/adafruit-io-house-lights-and-temperature

Author(s): Brent Rubell for Adafruit Industries, 2018.

Dependencies:
    - Adafruit_Blinka
        (https://github.com/adafruit/Adafruit_Blinka)
    - Adafruit_CircuitPython_SI7021
        (https://github.com/adafruit/Adafruit_CircuitPython_SI7021)
    - Adafruit_CircuitPython_NeoPixel
        (https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel)
"""
# Import standard python modules
import time

# import Adafruit IO REST client
from Adafruit_IO import Client

# import Adafruit Blinka
from busio import I2C
from board import SCL, SDA, D18

# import Adafruit_CircuitPython_Si7021 Library
import adafruit_si7021

# import neopixel library
import neopixel

# `while True` loop delay, in seconds
DELAY_TIME = 5

# number of LED pixels on the NeoPixel Strip
STRIP_LED_COUNT = 34
# number of LED pixels on the NeoPixel Jewel
JEWEL_PIXEL_COUNT = 7

# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'YOUR_IO_KEY'

# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username)
ADAFRUIT_IO_USERNAME = 'YOUR_IO_USERNAME'

# Create an instance of the REST client
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# set up Adafruit IO feeds
temperature = aio.feeds('temperature')
humidity = aio.feeds('humidity')
outdoor_lights = aio.feeds('outdoor-lights')
indoor_lights = aio.feeds('indoor-lights')

# create an i2c interface object
i2c = I2C(SCL, SDA)

# instanciate the sensor object
sensor = adafruit_si7021.SI7021(i2c)

# set up the NeoPixel strip
pixels = neopixel.NeoPixel(D18, STRIP_LED_COUNT)
pixels.fill((0,0,0))
pixels.show()

print('Adafruit IO Home: Lights and Climate Control')

while True:
    # get data from the si7021 sensor
    temperature_data = sensor.temperature
    humidity_data = sensor.relative_humidity

    # send data to adafruit io
    print('> Temperature: ', int((temperature_data * 1.8)+32))
    aio.send(temperature.key, int(temperature_data * 1.8)+32)
    print('> Humidity :', int(humidity_data))
    aio.send(temperature.key, int(humidity_data))

    # get the indoor light color picker feed
    indoor_light_data = aio.receive(indoor_lights.key)
    print('< Indoor Light HEX: ', indoor_light_data.value)
    # convert the hex values to RGB values
    red = aio.toRed(indoor_light_data.value)
    green = aio.toGreen(indoor_light_data.value)
    blue = aio.toBlue(indoor_light_data.value)

    # set the jewel's color
    for i in range(JEWEL_PIXEL_COUNT):
        pixels[i] = (red, green, blue)
        pixels.show()

    # get the outdoor light color picker feed
    outdoor_light_data = aio.receive(outdoor_lights.key)
    print('< Outdoor Light HEX: ', outdoor_light_data.value)

    # convert the hex values to RGB values
    red = aio.toRed(outdoor_light_data.value)
    green = aio.toGreen(outdoor_light_data.value)
    blue = aio.toBlue(outdoor_light_data.value)

    # set the strip color
    for j in range(JEWEL_PIXEL_COUNT, STRIP_LED_COUNT + JEWEL_PIXEL_COUNT):
        pixels[j] = (red, green, blue)
        pixels.show()

    # delay the loop to avoid timeout from Adafruit IO.
    time.sleep(DELAY_TIME)
This guide was first published on Aug 27, 2018. It was last updated on Aug 27, 2018. This page (Python Code) was last updated on Aug 24, 2019.