code.py
  1  # SPDX-FileCopyrightText: 2018 Brent Rubell for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  """
  6  'io_house_light_temp.py'
  7  =======================================
  8  Control lights and monitor temperature
  9  and humidity in a model smart house.
 10  
 11  Learning System Guide: https://learn.adafruit.com/adafruit-io-house-lights-and-temperature
 12  
 13  Author(s): Brent Rubell for Adafruit Industries, 2018.
 14  
 15  Dependencies:
 16      - Adafruit_Blinka
 17          (https://github.com/adafruit/Adafruit_Blinka)
 18      - Adafruit_CircuitPython_SI7021
 19          (https://github.com/adafruit/Adafruit_CircuitPython_SI7021)
 20      - Adafruit_CircuitPython_NeoPixel
 21          (https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel)
 22  """
 23  # Import standard python modules
 24  import time
 25  
 26  # import Adafruit IO REST client
 27  from Adafruit_IO import Client
 28  
 29  # import Adafruit Blinka
 30  from busio import I2C
 31  from board import SCL, SDA, D18
 32  
 33  # import Adafruit_CircuitPython_Si7021 Library
 34  import adafruit_si7021
 35  
 36  # import neopixel library
 37  import neopixel
 38  
 39  # `while True` loop delay, in seconds
 40  DELAY_TIME = 5
 41  
 42  # number of LED pixels on the NeoPixel Strip
 43  STRIP_LED_COUNT = 34
 44  # number of LED pixels on the NeoPixel Jewel
 45  JEWEL_PIXEL_COUNT = 7
 46  
 47  # Set to your Adafruit IO key.
 48  # Remember, your key is a secret,
 49  # so make sure not to publish it when you publish this code!
 50  ADAFRUIT_IO_KEY = 'YOUR_IO_KEY'
 51  
 52  # Set to your Adafruit IO username.
 53  # (go to https://accounts.adafruit.com to find your username)
 54  ADAFRUIT_IO_USERNAME = 'YOUR_IO_USERNAME'
 55  
 56  # Create an instance of the REST client
 57  aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
 58  
 59  # set up Adafruit IO feeds
 60  temperature = aio.feeds('temperature')
 61  humidity = aio.feeds('humidity')
 62  outdoor_lights = aio.feeds('outdoor-lights')
 63  indoor_lights = aio.feeds('indoor-lights')
 64  
 65  # create an i2c interface object
 66  i2c = I2C(SCL, SDA)
 67  
 68  # instanciate the sensor object
 69  sensor = adafruit_si7021.SI7021(i2c)
 70  
 71  # set up the NeoPixel strip
 72  pixels = neopixel.NeoPixel(D18, STRIP_LED_COUNT)
 73  pixels.fill((0,0,0))
 74  pixels.show()
 75  
 76  print('Adafruit IO Home: Lights and Climate Control')
 77  
 78  while True:
 79      # get data from the si7021 sensor
 80      temperature_data = sensor.temperature
 81      humidity_data = sensor.relative_humidity
 82  
 83      # send data to adafruit io
 84      print('> Temperature: ', int((temperature_data * 1.8)+32))
 85      aio.send(temperature.key, int(temperature_data * 1.8)+32)
 86      print('> Humidity :', int(humidity_data))
 87      aio.send(temperature.key, int(humidity_data))
 88  
 89      # get the indoor light color picker feed
 90      indoor_light_data = aio.receive(indoor_lights.key)
 91      print('< Indoor Light HEX: ', indoor_light_data.value)
 92      # convert the hex values to RGB values
 93      red = aio.toRed(indoor_light_data.value)
 94      green = aio.toGreen(indoor_light_data.value)
 95      blue = aio.toBlue(indoor_light_data.value)
 96  
 97      # set the jewel's color
 98      for i in range(JEWEL_PIXEL_COUNT):
 99          pixels[i] = (red, green, blue)
100          pixels.show()
101  
102      # get the outdoor light color picker feed
103      outdoor_light_data = aio.receive(outdoor_lights.key)
104      print('< Outdoor Light HEX: ', outdoor_light_data.value)
105  
106      # convert the hex values to RGB values
107      red = aio.toRed(outdoor_light_data.value)
108      green = aio.toGreen(outdoor_light_data.value)
109      blue = aio.toBlue(outdoor_light_data.value)
110  
111      # set the strip color
112      for j in range(JEWEL_PIXEL_COUNT, STRIP_LED_COUNT + JEWEL_PIXEL_COUNT):
113          pixels[j] = (red, green, blue)
114          pixels.show()
115  
116      # delay the loop to avoid timeout from Adafruit IO.
117      time.sleep(DELAY_TIME)