Bary Nusz
Published © GPL3+

Controlling an RGB LED on a Photon with a UWA Color Picker

Hooking up a Universal Windows App color picker control with an RGB LED on a Photon using a Particle Cloud function.

BeginnerFull instructions provided4,671
Controlling an RGB LED on a Photon with a UWA Color Picker

Things used in this project

Hardware components

Photon
Particle Photon
×1
RGB LED, Common Cathode
×1
Resistor 1k ohm
Resistor 1k ohm
×3

Software apps and online services

Visual Studio 2015
Microsoft Visual Studio 2015

Story

Read more

Schematics

Photon Circuit

Photon Circuit

Code

UWA Photon Color

C#
This is a simple UWA. I used a ColorPicker control I found on Nugent. Make sure you update with your Photon device ID and your access token. I'm using a timer to ensure that the Particle function is called no more than once a sec (we don't want to overwhelm Particle cloud). After the correctly formatted URL is constructed, all you need to do is make the POST request and the Particle function is called.
    public sealed partial class MainPage : Page
    {
        const string PHOTONDEVICEID = "{YOURDEVICEID}";
        const string ACCESS_TOKEN = "{YOURACCESSTOKEN}";
        DispatcherTimer timer;
        SolidColorBrush currentColor;
        public MainPage()
        {
            this.InitializeComponent();
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += Timer_Tick;
        }
        private void Timer_Tick(object sender, object e)
        {
            if (WriteColor())
            {
                timer.Stop();
            }
        }
        private void ColorPicker_SelectedColorChanged(object sender, EventArgs e)
        {
            if (!timer.IsEnabled)
            {
                WriteColor();
                timer.Start();
            }
        }
        private bool WriteColor()
        {
            SetRGB(colorPicker.SelectedColor);
            bool result = currentColor == colorPicker.SelectedColor;
            currentColor = colorPicker.SelectedColor;
            return result;
        }
        private async void SetRGB(SolidColorBrush rgb)
        {
            string url = String.Format("https://api.particle.io/v1/devices/{0}/setRGB?access_token={1}", PHOTONDEVICEID, ACCESS_TOKEN);
            var request = WebRequest.Create(url);
            var postData = "value=" + string.Format("{0},{1},{2}", rgb.Color.R, rgb.Color.G, rgb.Color.B);
            var data = Encoding.ASCII.GetBytes(postData);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            using (var stream = await request.GetRequestStreamAsync())
            {
                stream.Write(data, 0, data.Length);
            }
            try
            {
                var response = await request.GetResponseAsync();
                var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            }
            catch { }
        }
    }

Photon RGB multicolor firmware

C/C++
This is the Photon firmware. I'm using the rgb-controls library to handle the RGB LED PWM color functions. The particle cloud function is defined as "setRGB". In the setRGB event handler, the command string parameter is expecting the RGB values to be in a comma separated RGB format. So we split the values apart, and pass in the new value using the Led.fadeOnce() function. This function transitions from one color to another color over the specified number of millisecs. In our case we'll transition from the old color to the new color over a 500 millisec timespan.
// This #include statement was automatically added by the Particle IDE.
#include "rgb-controls/rgb-controls.h"
#define REDPIN D2
#define GREENPIN D1
#define BLUEPIN D0
using namespace RGBControls;

Led led(REDPIN, GREENPIN, BLUEPIN);
Color currentColor(255, 0, 0);

void setup() {
    bool success = Particle.function("setRGB", setRGB);
}

void loop() {
}

int setRGB(String command)
{
    //Color comes in the format "{R},{G},{B}"
    int commaIndex = command.indexOf(',');
    //Strip the red color from the string
    String red = command.substring(0,commaIndex);
    
    command = command.substring(commaIndex+1);
    commaIndex = command.indexOf(',');
    //Strip the green color from the string
    String green = command.substring(0,commaIndex);
    
    //Strip the blue color from the string
    String blue = command.substring(commaIndex+1);
    
    Color newColor(red.toInt(), green.toInt(), blue.toInt());
    //Fade once from old color to new color over 500mS
    led.fadeOnce(currentColor, newColor, 500);
    //What's new is old now
    currentColor = newColor;
    
    return 1;
}

Photon Color Project

This is the full UWA and Photon firmware code.

Credits

Bary Nusz

Bary Nusz

8 projects • 32 followers
Nusz Labs “Tinkerer-in-Chief”.

Comments