|
| 1 | +""" |
| 2 | +'io_house_light_temp.py' |
| 3 | +======================================= |
| 4 | +Control lights and monitor temperature |
| 5 | +and humidity in a model smart house. |
| 6 | +
|
| 7 | +Learning System Guide: https://learn.adafruit.com/adafruit-io-house-lights-and-temperature |
| 8 | +
|
| 9 | +Author(s): Brent Rubell for Adafruit Industries, 2018. |
| 10 | +
|
| 11 | +Dependencies: |
| 12 | + - Adafruit_Blinka |
| 13 | + (https://github.com/adafruit/Adafruit_Blinka) |
| 14 | + - Adafruit_CircuitPython_SI7021 |
| 15 | + (https://github.com/adafruit/Adafruit_CircuitPython_SI7021) |
| 16 | + - RPi WS281x Python |
| 17 | + (https://github.com/rpi-ws281x/) |
| 18 | +""" |
| 19 | +# Import standard python modules |
| 20 | +import time |
| 21 | + |
| 22 | +# import Adafruit IO REST client |
| 23 | +from Adafruit_IO import Client, Feed, RequestError |
| 24 | + |
| 25 | +# import Adafruit Blinka |
| 26 | +from busio import I2C |
| 27 | +from board import SCL, SDA |
| 28 | + |
| 29 | +# import Adafruit_CircuitPython_Si7021 Library |
| 30 | +import adafruit_si7021 |
| 31 | + |
| 32 | +# import neopixel library |
| 33 | +import neopixel |
| 34 | + |
| 35 | +# while True loop delay, in seconds |
| 36 | +DELAY_TIME = 5 |
| 37 | + |
| 38 | +# LED strip configuration: |
| 39 | +STRIP_LED_COUNT = 34 # Number of LED pixels on the NeoPixel Strip |
| 40 | +JEWEL_PIXEL_COUNT = 7 # Number of LED pixels on the NeoPixel Jewel |
| 41 | +LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!). |
| 42 | +LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz) |
| 43 | +LED_DMA = 10 # DMA channel to use for generating signal (try 10) |
| 44 | +LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest |
| 45 | +LED_INVERT = False # True to invert the signal (when using NPN transistor level shift) |
| 46 | +LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53 |
| 47 | + |
| 48 | +# Create NeoPixel object with appropriate configuration. |
| 49 | +strip = neopixel.Adafruit_NeoPixel(STRIP_LED_COUNT, LED_PIN, LED_FREQ_HZ, |
| 50 | + LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL) |
| 51 | +# Intialize the library (must be called once before other functions). |
| 52 | +strip.begin() |
| 53 | + |
| 54 | +# Set to your Adafruit IO key. |
| 55 | +# Remember, your key is a secret, |
| 56 | +# so make sure not to publish it when you publish this code! |
| 57 | +ADAFRUIT_IO_KEY = 'YOUR_IO_KEY' |
| 58 | + |
| 59 | +# Set to your Adafruit IO username. |
| 60 | +# (go to https://accounts.adafruit.com to find your username) |
| 61 | +ADAFRUIT_IO_USERNAME = 'YOUR_IO_USERNAME' |
| 62 | + |
| 63 | +# Create an instance of the REST client |
| 64 | +aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) |
| 65 | + |
| 66 | +# set up Adafruit IO feeds |
| 67 | +try: |
| 68 | + temperature = aio.feeds('temperature') |
| 69 | + humidity = aio.feeds('humidity') |
| 70 | + outdoor_lights = aio.feeds('outdoor-lights') |
| 71 | + indoor_lights = aio.feeds('indoor-lights') |
| 72 | +except RequestError: |
| 73 | + temperature = aio.create_feed(Feed(name='temperature')) |
| 74 | + humidity = aio.create_feed(Feed(name='humidity')) |
| 75 | + outdoor_lights = aio.create_feed(Feed(name='outdoor-lights')) |
| 76 | + indoor_lights = aio.create_feed(Feed(name='indoor-lights')) |
| 77 | + |
| 78 | +# create an i2c interface object |
| 79 | +i2c = I2C(SCL, SDA) |
| 80 | + |
| 81 | +# instanciate the sensor object |
| 82 | +sensor = adafruit_si7021.SI7021(i2c) |
| 83 | + |
| 84 | +print('Adafruit IO Home: Lights and Climate Control') |
| 85 | + |
| 86 | +while True: |
| 87 | + # get data from the si7021 sensor |
| 88 | + temperature_data = sensor.temperature |
| 89 | + humidity_data = sensor.relative_humidity |
| 90 | + |
| 91 | + # send data to adafruit io |
| 92 | + print('> Temperature: ', int((temperature_data * 1.8)+32)) |
| 93 | + aio.send(temperature.key, int(temperature_data * 1.8)+32) |
| 94 | + print('> Humidity :', int(humidity_data)) |
| 95 | + aio.send(temperature.key, int(humidity_data)) |
| 96 | + |
| 97 | + # get the indoor light color picker feed |
| 98 | + indoor_light_data = aio.receive(indoor_lights.key) |
| 99 | + print('< Indoor Light HEX: ', indoor_light_data.value) |
| 100 | + # convert the hex values to RGB values |
| 101 | + red = aio.toRed(indoor_light_data.value) |
| 102 | + green = aio.toGreen(indoor_light_data.value) |
| 103 | + blue = aio.toBlue(indoor_light_data.value) |
| 104 | + |
| 105 | + # set the jewel's color |
| 106 | + for i in range(JEWEL_PIXEL_COUNT): |
| 107 | + strip.setPixelColorRGB(i, green, red, blue) |
| 108 | + strip.show() |
| 109 | + |
| 110 | + # get the outdoor light color picker feed |
| 111 | + outdoor_light_data = aio.receive(outdoor_lights.key) |
| 112 | + print('< Outdoor Light HEX: ', outdoor_light_data.value) |
| 113 | + |
| 114 | + # convert the hex values to RGB values |
| 115 | + red = aio.toRed(outdoor_light_data.value) |
| 116 | + green = aio.toGreen(outdoor_light_data.value) |
| 117 | + blue = aio.toBlue(outdoor_light_data.value) |
| 118 | + |
| 119 | + # set the strip color |
| 120 | + for j in range(JEWEL_PIXEL_COUNT, STRIP_LED_COUNT + JEWEL_PIXEL_COUNT): |
| 121 | + strip.setPixelColorRGB(j, green, red, blue) |
| 122 | + strip.show() |
| 123 | + |
| 124 | + # delay the loop to avoid timeout from Adafruit IO. |
| 125 | + time.sleep(DELAY_TIME) |
0 commit comments