|
| 1 | +// Adafruit IO House: Lights and Temperature |
| 2 | +// |
| 3 | +// Learn Guide: https://learn.adafruit.com/adafruit-io-house-lights-and-temperature |
| 4 | +// |
| 5 | +// Adafruit invests time and resources providing this open source code. |
| 6 | +// Please support Adafruit and open source hardware by purchasing |
| 7 | +// products from Adafruit! |
| 8 | +// |
| 9 | +// Written by Brent Rubell for Adafruit Industries |
| 10 | +// Copyright (c) 2018 Adafruit Industries |
| 11 | +// Licensed under the MIT license. |
| 12 | +// |
| 13 | +// All text above must be included in any redistribution. |
| 14 | + |
| 15 | +/************************** Configuration ***********************************/ |
| 16 | + |
| 17 | +// edit the config.h tab and enter your Adafruit IO credentials |
| 18 | +// and any additional configuration needed for WiFi, cellular, |
| 19 | +// or ethernet clients. |
| 20 | +#include "config.h" |
| 21 | + |
| 22 | +// include the NeoPixel library |
| 23 | +#include "Adafruit_NeoPixel.h" |
| 24 | + |
| 25 | +// include the si7021 library |
| 26 | +#include "Adafruit_Si7021.h" |
| 27 | +/************************ Example Starts Here *******************************/ |
| 28 | + |
| 29 | +// pin the NeoPixel strip is connected to |
| 30 | +#define STRIP_PIN 12 |
| 31 | +// pin the NeoPixel Jewel is connected to |
| 32 | +#define JEWEL_PIN 2 |
| 33 | + |
| 34 | +// amount of neopixels on the NeoPixel strip |
| 35 | +#define STRIP_PIXEL_COUNT 34 |
| 36 | +// amount of neopixels on the NeoPixel jewel |
| 37 | +#define JEWEL_PIXEL_COUNT 7 |
| 38 | + |
| 39 | +// type of neopixels used by the NeoPixel strip and jewel. |
| 40 | +#define PIXEL_TYPE NEO_GRB + NEO_KHZ800 |
| 41 | + |
| 42 | +// main loop() delay, in seconds |
| 43 | +#define TEMP_DELAY 7 |
| 44 | + |
| 45 | +// Temperature and Humidity: Si7021 Sensor |
| 46 | +int temperatureData; |
| 47 | +int humidityData; |
| 48 | + |
| 49 | +// initalize neopixel strip |
| 50 | +Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIP_PIXEL_COUNT, STRIP_PIN, PIXEL_TYPE); |
| 51 | +// initalize neopixel jewel |
| 52 | +Adafruit_NeoPixel jewel = Adafruit_NeoPixel(JEWEL_PIXEL_COUNT, JEWEL_PIN, PIXEL_TYPE); |
| 53 | + |
| 54 | +// initalize the sensor object |
| 55 | +Adafruit_Si7021 sensor = Adafruit_Si7021(); |
| 56 | + |
| 57 | +// set up the Adafruit IO House Group |
| 58 | +AdafruitIO_Group *houseGroup = io.group("io-house"); |
| 59 | + |
| 60 | +void setup() { |
| 61 | + |
| 62 | + // start the serial connection |
| 63 | + Serial.begin(115200); |
| 64 | + |
| 65 | + // wait for serial monitor to open |
| 66 | + while(! Serial); |
| 67 | + |
| 68 | + // connect to io.adafruit.com |
| 69 | + Serial.print("Connecting to Adafruit IO"); |
| 70 | + io.connect(); |
| 71 | + |
| 72 | + // subscribe to lighting feeds and register message handlers |
| 73 | + houseGroup->onMessage("indoor-lights", indoorLightHandler); |
| 74 | + houseGroup->onMessage("outside-lights", outdoorLightHandler); |
| 75 | + |
| 76 | + // wait for a connection |
| 77 | + while(io.status() < AIO_CONNECTED) { |
| 78 | + Serial.print("."); |
| 79 | + delay(500); |
| 80 | + } |
| 81 | + |
| 82 | + // we are connected |
| 83 | + Serial.println(); |
| 84 | + Serial.println(io.statusText()); |
| 85 | + |
| 86 | + // initalize the Si7021 sensor |
| 87 | + if (!sensor.begin()) { |
| 88 | + Serial.println("Did not find Si7021 sensor!"); |
| 89 | + while (true); |
| 90 | + } |
| 91 | + Serial.println("Si7021 sensor set up!"); |
| 92 | + |
| 93 | + // initalize the neopixel strip and jewel. |
| 94 | + strip.begin(); |
| 95 | + jewel.begin(); |
| 96 | + |
| 97 | + // set all neopixels on the strip and jewel to `off`. |
| 98 | + strip.show(); |
| 99 | + jewel.show(); |
| 100 | +} |
| 101 | + |
| 102 | +void loop() { |
| 103 | + // io.run(); is required for all sketches. |
| 104 | + // it should always be present at the top of your loop |
| 105 | + // function. it keeps the client connected to |
| 106 | + // io.adafruit.com, and processes any incoming data. |
| 107 | + io.run(); |
| 108 | + |
| 109 | + temperatureData = sensor.readTemperature() * 1.8 + 32; |
| 110 | + humidityData = sensor.readHumidity(); |
| 111 | + |
| 112 | + |
| 113 | + Serial.print("-> Sending Temperature to Adafruit IO: "); |
| 114 | + Serial.println(temperatureData); |
| 115 | + Serial.print("-> Sending Humidity to Adafruit IO: "); |
| 116 | + Serial.println(humidityData); |
| 117 | + |
| 118 | + // set feeds to temperature and humidity data |
| 119 | + houseGroup->set("humidity", temperatureData); |
| 120 | + houseGroup->set("temperature", humidityData); |
| 121 | + // send these values to Adafruit IO |
| 122 | + houseGroup->save(); |
| 123 | + |
| 124 | + // delay the loop to avoid flooding Adafruit IO |
| 125 | + delay(1000*TEMP_DELAY); |
| 126 | + |
| 127 | +} |
| 128 | + |
| 129 | +void indoorLightHandler(AdafruitIO_Data *data) { |
| 130 | + Serial.print("-> indoor light HEX: "); |
| 131 | + Serial.println(data->value()); |
| 132 | + |
| 133 | + long color = data->toNeoPixel(); |
| 134 | + |
| 135 | + // set the color of each NeoPixel in the jewel |
| 136 | + for(int i=0; i<JEWEL_PIXEL_COUNT; ++i) { |
| 137 | + jewel.setPixelColor(i, color); |
| 138 | + } |
| 139 | + // 'set' the neopixel jewel to the new color |
| 140 | + jewel.show(); |
| 141 | +} |
| 142 | + |
| 143 | +void outdoorLightHandler(AdafruitIO_Data *data) { |
| 144 | + Serial.print("-> outdoor light HEX: "); |
| 145 | + Serial.println(data->value()); |
| 146 | + |
| 147 | + long color = data->toNeoPixel(); |
| 148 | + |
| 149 | + // set the color of each NeoPixel in the strip |
| 150 | + for(int i=0; i<STRIP_PIXEL_COUNT; ++i) { |
| 151 | + strip.setPixelColor(i, color); |
| 152 | + } |
| 153 | + // 'set' the neopixel strip to the new color |
| 154 | + strip.show(); |
| 155 | +} |
| 156 | + |
| 157 | + |
0 commit comments