Skip to content

Commit d45a9b4

Browse files
committed
add rgb led example
1 parent 84bb778 commit d45a9b4

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Adafruit IO RGB LED Output Example
2+
//
3+
// Adafruit invests time and resources providing this open source code.
4+
// Please support Adafruit and open source hardware by purchasing
5+
// products from Adafruit!
6+
//
7+
// Written by Todd Treece for Adafruit Industries
8+
// Copyright (c) 2016-2017 Adafruit Industries
9+
// Licensed under the MIT license.
10+
//
11+
// All text above must be included in any redistribution.
12+
13+
/************************** Configuration ***********************************/
14+
15+
// edit the config.h tab and enter your Adafruit IO credentials
16+
// and any additional configuration needed for WiFi, cellular,
17+
// or ethernet clients.
18+
#include "config.h"
19+
20+
/************************ Example Starts Here *******************************/
21+
22+
// default PWM pins for ESP8266.
23+
// you should change these to match PWM pins on other platforms.
24+
#define RED_PIN 4
25+
#define GREEN_PIN 5
26+
#define BLUE_PIN 2
27+
28+
// set up the 'color' feed
29+
AdafruitIO_Feed *color = io.feed("color");
30+
31+
void setup() {
32+
33+
// start the serial connection
34+
Serial.begin(115200);
35+
36+
// wait for serial monitor to open
37+
while(! Serial);
38+
39+
// connect to io.adafruit.com
40+
Serial.print("Connecting to Adafruit IO");
41+
io.connect();
42+
43+
// set up a message handler for the 'color' feed.
44+
// the handleMessage function (defined below)
45+
// will be called whenever a message is
46+
// received from adafruit io.
47+
color->onMessage(handleMessage);
48+
49+
// wait for a connection
50+
while(io.status() < AIO_CONNECTED) {
51+
Serial.print(".");
52+
delay(500);
53+
}
54+
55+
// we are connected
56+
Serial.println();
57+
Serial.println(io.statusText());
58+
59+
// set analogWrite range for ESP8266
60+
#ifdef ESP8266
61+
analogWriteRange(255);
62+
#endif
63+
64+
}
65+
66+
void loop() {
67+
68+
// io.run(); is required for all sketches.
69+
// it should always be present at the top of your loop
70+
// function. it keeps the client connected to
71+
// io.adafruit.com, and processes any incoming data.
72+
io.run();
73+
74+
}
75+
76+
// this function is called whenever a 'color' message
77+
// is received from Adafruit IO. it was attached to
78+
// the color feed in the setup() function above.
79+
void handleMessage(AdafruitIO_Data *data) {
80+
81+
// print RGB values and hex value
82+
Serial.println("Received:");
83+
Serial.print(" - R: ");
84+
Serial.println(data->toRed());
85+
Serial.print(" - G: ");
86+
Serial.println(data->toGreen());
87+
Serial.print(" - B: ");
88+
Serial.println(data->toBlue());
89+
Serial.print(" - HEX: ");
90+
Serial.println(data->value());
91+
92+
// invert RGB values for common anode LEDs
93+
analogWrite(RED_PIN, 255 - data->toRed());
94+
analogWrite(GREEN_PIN, 255 - data->toGreen());
95+
analogWrite(BLUE_PIN, 255 - data->toBlue());
96+
97+
}

examples/adafruitio_13_rgb/config.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/************************ Adafruit IO Config *******************************/
2+
3+
// visit io.adafruit.com if you need to create an account,
4+
// or if you need your Adafruit IO key.
5+
#define IO_USERNAME "your_username"
6+
#define IO_KEY "your_key"
7+
8+
/******************************* WIFI **************************************/
9+
10+
// the AdafruitIO_WiFi client will work with the following boards:
11+
// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
12+
// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
13+
// - Feather M0 WiFi -> https://www.adafruit.com/products/3010
14+
// - Feather WICED -> https://www.adafruit.com/products/3056
15+
16+
#define WIFI_SSID "your_ssid"
17+
#define WIFI_PASS "your_pass"
18+
19+
// comment out the following two lines if you are using fona or ethernet
20+
#include "AdafruitIO_WiFi.h"
21+
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
22+
23+
24+
/******************************* FONA **************************************/
25+
26+
// the AdafruitIO_FONA client will work with the following boards:
27+
// - Feather 32u4 FONA -> https://www.adafruit.com/product/3027
28+
29+
// uncomment the following two lines for 32u4 FONA,
30+
// and comment out the AdafruitIO_WiFi client in the WIFI section
31+
// #include "AdafruitIO_FONA.h"
32+
// AdafruitIO_FONA io(IO_USERNAME, IO_KEY);
33+
34+
35+
/**************************** ETHERNET ************************************/
36+
37+
// the AdafruitIO_Ethernet client will work with the following boards:
38+
// - Ethernet FeatherWing -> https://www.adafruit.com/products/3201
39+
40+
// uncomment the following two lines for ethernet,
41+
// and comment out the AdafruitIO_WiFi client in the WIFI section
42+
// #include "AdafruitIO_Ethernet.h"
43+
// AdafruitIO_Ethernet io(IO_USERNAME, IO_KEY);

0 commit comments

Comments
 (0)