[189] | 1 | /*
|
---|
| 2 | CheerLights
|
---|
| 3 |
|
---|
| 4 | Reads the latest CheerLights color on ThingSpeak, and sets a common anode RGB LED on digital pins 5, 6, and 9.
|
---|
| 5 | On Spark core, the built in RGB LED is used
|
---|
| 6 | Visit http://www.cheerlights.com for more info.
|
---|
| 7 |
|
---|
| 8 | ThingSpeak ( https://www.thingspeak.com ) is a free IoT service for prototyping
|
---|
| 9 | systems that collect, analyze, and react to their environments.
|
---|
| 10 |
|
---|
| 11 | Copyright 2015, The MathWorks, Inc.
|
---|
| 12 |
|
---|
| 13 | Documentation for the ThingSpeak Communication Library for Arduino is in the extras/documentation folder where the library was installed.
|
---|
| 14 | See the accompaning licence file for licensing information.
|
---|
| 15 | */
|
---|
| 16 |
|
---|
| 17 | #ifdef SPARK
|
---|
| 18 | #include "ThingSpeak/ThingSpeak.h"
|
---|
| 19 | #else
|
---|
| 20 | #include "ThingSpeak.h"
|
---|
| 21 | #endif
|
---|
| 22 |
|
---|
| 23 | // ***********************************************************************************************************
|
---|
| 24 | // This example selects the correct library to use based on the board selected under the Tools menu in the IDE.
|
---|
| 25 | // Yun, Wired Ethernet shield, wi-fi shield, esp8266, and Spark are all supported.
|
---|
| 26 | // With Uno and Mega, the default is that you're using a wired ethernet shield (http://www.arduino.cc/en/Main/ArduinoEthernetShield)
|
---|
| 27 | // If you're using a wi-fi shield (http://www.arduino.cc/en/Main/ArduinoWiFiShield), uncomment the line below
|
---|
| 28 | // ***********************************************************************************************************
|
---|
| 29 | //#define USE_WIFI_SHIELD
|
---|
| 30 |
|
---|
| 31 | // Make sure that you put a 330 ohm resistor between the arduino
|
---|
| 32 | // pins and each of the color pins on the LED.
|
---|
| 33 | int pinRed = 9;
|
---|
| 34 | int pinGreen = 6;
|
---|
| 35 | int pinBlue = 5;
|
---|
| 36 |
|
---|
| 37 | #ifdef ARDUINO_ARCH_AVR
|
---|
| 38 | #ifdef ARDUINO_AVR_YUN
|
---|
| 39 | #include "YunClient.h"
|
---|
| 40 | YunClient client;
|
---|
| 41 | #else
|
---|
| 42 |
|
---|
| 43 | #ifdef USE_WIFI_SHIELD
|
---|
| 44 | #include <SPI.h>
|
---|
| 45 | // ESP8266 USERS -- YOU MUST COMMENT OUT THE LINE BELOW. There's a bug in the Arduino IDE that causes it to not respect #ifdef when it comes to #includes
|
---|
| 46 | // If you get "multiple definition of `WiFi'" -- comment out the line below.
|
---|
| 47 | #include <WiFi.h>
|
---|
| 48 | char ssid[] = "<YOURNETWORK>"; // your network SSID (name)
|
---|
| 49 | char pass[] = "<YOURPASSWORD>"; // your network password
|
---|
| 50 | int status = WL_IDLE_STATUS;
|
---|
| 51 | WiFiClient client;
|
---|
| 52 | #else
|
---|
| 53 | // Use wired ethernet shield
|
---|
| 54 | #include <SPI.h>
|
---|
| 55 | #include <Ethernet.h>
|
---|
| 56 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
---|
| 57 | EthernetClient client;
|
---|
| 58 | #endif
|
---|
| 59 | #endif
|
---|
| 60 | #endif
|
---|
| 61 |
|
---|
| 62 | #ifdef ARDUINO_ARCH_ESP8266
|
---|
| 63 | #include <ESP8266WiFi.h>
|
---|
| 64 | char ssid[] = "<YOURNETWORK>"; // your network SSID (name)
|
---|
| 65 | char pass[] = "<YOURPASSWORD>"; // your network password
|
---|
| 66 | int status = WL_IDLE_STATUS;
|
---|
| 67 | WiFiClient client;
|
---|
| 68 | #endif
|
---|
| 69 |
|
---|
| 70 | #ifdef SPARK
|
---|
| 71 | TCPClient client;
|
---|
| 72 | #endif
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | /*
|
---|
| 76 | This is the ThingSpeak channel number for CheerLights
|
---|
| 77 | https://thingspeak.com/channels/1417. Field 1 contains a string with
|
---|
| 78 | the latest CheerLights color.
|
---|
| 79 | */
|
---|
| 80 | unsigned long cheerLightsChannelNumber = 1417;
|
---|
| 81 |
|
---|
| 82 | void setup() {
|
---|
| 83 | #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ESP8266)
|
---|
| 84 | #ifdef ARDUINO_AVR_YUN
|
---|
| 85 | Bridge.begin();
|
---|
| 86 | #else
|
---|
| 87 | #if defined(USE_WIFI_SHIELD) || defined(ARDUINO_ARCH_ESP8266)
|
---|
| 88 | WiFi.begin(ssid, pass);
|
---|
| 89 | #else
|
---|
| 90 | Ethernet.begin(mac);
|
---|
| 91 | #endif
|
---|
| 92 | #endif
|
---|
| 93 | #endif
|
---|
| 94 |
|
---|
| 95 | ThingSpeak.begin(client);
|
---|
| 96 |
|
---|
| 97 | #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ESP8266)
|
---|
| 98 | pinMode(pinRed,OUTPUT);
|
---|
| 99 | pinMode(pinGreen,OUTPUT);
|
---|
| 100 | pinMode(pinBlue,OUTPUT);
|
---|
| 101 | #endif
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | void loop() {
|
---|
| 105 | // Read the latest value from field 1 of channel 1417
|
---|
| 106 | String color = ThingSpeak.readStringField(cheerLightsChannelNumber, 1);
|
---|
| 107 | setColor(color);
|
---|
| 108 |
|
---|
| 109 | // Check again in 5 seconds
|
---|
| 110 | delay(5000);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | // List of CheerLights color names
|
---|
| 114 | String colorName[] = {"none","red","pink","green","blue","cyan","white","warmwhite","oldlace","purple","magenta","yellow","orange"};
|
---|
| 115 |
|
---|
| 116 | // Map of RGB values for each of the Cheerlight color names
|
---|
| 117 | int colorRGB[][3] = { 0, 0, 0, // "none"
|
---|
| 118 | 255, 0, 0, // "red"
|
---|
| 119 | 255,192,203, // "pink"
|
---|
| 120 | 0,255, 0, // "green"
|
---|
| 121 | 0, 0,255, // "blue"
|
---|
| 122 | 0,255,255, // "cyan",
|
---|
| 123 | 255,255,255, // "white",
|
---|
| 124 | 255,223,223, // "warmwhite",
|
---|
| 125 | 255,223,223, // "oldlace",
|
---|
| 126 | 128, 0,128, // "purple",
|
---|
| 127 | 255, 0,255, // "magenta",
|
---|
| 128 | 255,255, 0, // "yellow",
|
---|
| 129 | 255,165, 0}; // "orange"};
|
---|
| 130 |
|
---|
| 131 | void setColor(String color)
|
---|
| 132 | {
|
---|
| 133 | // Look through the list of colors to find the one that was requested
|
---|
| 134 | for(int iColor = 0; iColor <= 12; iColor++)
|
---|
| 135 | {
|
---|
| 136 | if(color == colorName[iColor])
|
---|
| 137 | {
|
---|
| 138 | // When it matches, look up the RGB values for that color in the table,
|
---|
| 139 | // and write the red, green, and blue values.
|
---|
| 140 | #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ESP8266)
|
---|
| 141 | analogWrite(pinRed,colorRGB[iColor][0]);
|
---|
| 142 | analogWrite(pinGreen,colorRGB[iColor][1]);
|
---|
| 143 | analogWrite(pinBlue,colorRGB[iColor][2]);
|
---|
| 144 | #endif
|
---|
| 145 | #ifdef SPARK
|
---|
| 146 | RGB.control(true);
|
---|
| 147 | RGB.color(colorRGB[iColor][0], colorRGB[iColor][1], colorRGB[iColor][2]);
|
---|
| 148 | #endif
|
---|
| 149 | return;
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | }
|
---|