The adafruitio_13_rgb example uses pins 4, 5, & 2 for red, green, and blue by default. These pins can be modified by changing the RED_PIN, GREEN_PIN, and BLUE_PIN defines. You will need to choose pins on your board that support PWM output.
/************************ Example Starts Here *******************************/ // default PWM pins for ESP8266. // you should change these to match PWM pins on other platforms. #define RED_PIN 4 #define GREEN_PIN 5 #define BLUE_PIN 2
The next chunk of code sets up an instance of the color feed.
// set up the 'color' feed AdafruitIO_Feed *color = io.feed("color");
In the setup function, we connect to Adafruit IO, and attach a function called handleMessage to the color feed. This function will be called whenever your device receives messages for that feed.
We also use a special function for the ESP8266 platform that sets the analogWrite range to 0-255. This will be ignored on all other platforms.
The code will wait until you have a valid connection to Adafruit IO before continuing with the sketch. If you have any issues connecting, check config.h for any typos in your username or key.
void setup() { // start the serial connection Serial.begin(115200); // wait for serial monitor to open while(! Serial); // connect to io.adafruit.com Serial.print("Connecting to Adafruit IO"); io.connect(); // set up a message handler for the 'color' feed. // the handleMessage function (defined below) // will be called whenever a message is // received from adafruit io. color->onMessage(handleMessage); // wait for a connection while(io.status() < AIO_CONNECTED) { Serial.print("."); delay(500); } // we are connected Serial.println(); Serial.println(io.statusText()); // set analogWrite range for ESP8266 #ifdef ESP8266 analogWriteRange(255); #endif }
Next, we have the main loop()
function. The first line of the loop function calls io.run();
this line will need to be present at the top of your loop in every sketch. It helps keep your device connected to Adafruit IO, and processes any incoming data.
void loop() { // io.run(); is required for all sketches. // it should always be present at the top of your loop // function. it keeps the client connected to // io.adafruit.com, and processes any incoming data. io.run(); }
The final chunk of code is the handleMessage function. This is the function that is called whenever the color feed gets a message.
We use the data->toRed(), data->toGreen(), and data->toBlue() functions to convert the incoming hex color values to integers that will be compatible with analogWrite.
Because we are using a common anode RGB LED, we will need to flip the incoming RGB values. We do this by subtracting the values from 255 before sending the values to analogWrite.
If you are using a common cathode RGB LED, you can send the values directly to analogWrite from data->toRed(), data->toGreen(), and data->toBlue().
// this function is called whenever a 'color' message // is received from Adafruit IO. it was attached to // the color feed in the setup() function above. void handleMessage(AdafruitIO_Data *data) { // print RGB values and hex value Serial.println("Received:"); Serial.print(" - R: "); Serial.println(data->toRed()); Serial.print(" - G: "); Serial.println(data->toGreen()); Serial.print(" - B: "); Serial.println(data->toBlue()); Serial.print(" - HEX: "); Serial.println(data->value()); // invert RGB values for common anode LEDs analogWrite(RED_PIN, 255 - data->toRed()); analogWrite(GREEN_PIN, 255 - data->toGreen()); analogWrite(BLUE_PIN, 255 - data->toBlue()); }
Upload the sketch to your board, and open the Arduino Serial Monitor. Your board should now connect to Adafruit IO.
Connecting to Adafruit IO.... Adafruit IO connected.
You can now use the color block on Adafruit IO to set a color, and you should see something resembling the following in the Arduino Serial Monitor.
Received: - R: 0 - G: 0 - B: 0 - HEX: #000000 Received: - R: 210 - G: 31 - B: 31 - HEX: #d21f1f
You should also see the RGB LED update with the color you picked in the color block.
Page last edited March 08, 2024
Text editor powered by tinymce.