Project tutorial
WiFiLamp with FastLED and Windows Remote Arduino

WiFiLamp with FastLED and Windows Remote Arduino © MIT

Driving a LED Strip with Windows Remote Arduino over WiFi

  • 5,255 views
  • 1 comment
  • 16 respects

Components and supplies

Ard yun
Arduino Yun
×1
TM1803 LED Strip
×1

Apps and online services

Microsoft Windows Remote Arduino

About this project

In an earlier post I enabled an Arduino Yun to be controlled by Windows Remote Arduino over WiFi. This project is a further extension of it in exploring what is possible with Windows Remote Arduino. This time i connected a LED strip (TM1803 based three wire LED strip from Radioshack) to my Yun and controlled its color through an Universal Windows App that uses Windows Remote Arduino. Here is the lamp in action.

(I realize that there is a lag in the video between the phone overlay and the lamp. Something wrong with my video encoder. Couldn't figure it out. Job for another day..)

Key Components

Aside from Yun and Windows Remote Arduino, the key components are:

1. LED Strip:

Most of the LED strips have dedicated IC chips on them to control a fixed set of LEDs and communicate the data downstream to next IC chip on the strip.  I used a Radioshack LED strip that uses TM1803. This is a small 1 meter strip with 10 chips controlling 30 LEDs.

2. Power Source:

Most of the LED's strips would need a separate power source. Mine required a 12v power supply. Since the Yun is only a 5V device, the power source and Yun had to have a common ground. Even thought there are ways to use the same 12v power supply to drive both the LED and Yun, i chose to run them with separate power source and common ground.

3. FastLed Library:

This is an excellent Arduino library developed and maintained by Daniel Garcia and Mark Kriegsman. There is no other library that i know of which supports such a vast array of LED strips as this.

Wiring

Simple wiring steps:

1. 12V power adapter +V to RGB Led Strp +Vcc

2. 12v Power Adapter GND to Arduino Yun Gnd pin.

3. RGB Led Strip GND to Arduino Yun Gnd pin

4.RGB Led String Data Pin to Arduino Digital 3 ( ~ pwm)

Arduino can be powered with a battery or wall wart, independent of the LED strip power. Here is my wired up Yun connected to a power bank, before installing it in the lamp. (I am aware that voltage dividers can be used to power both the Arduino and the LED with the same power supply. This simple wiring is sufficient for temporary stuff)

Software

All source code is available at the following Github url.


The LED strips will be controlled by an Arduino Sketch using the FastLED library. The sketch will receive the color information from the Windows Universal App over WiFi through the Windows Remote Arduino's Firmata interface.

Modified Firmata

​Instead of using the StandardFirmataYun as mentioned in the earlier project, i am using a stripped down version of the firmata sketch as i am interested only in handling the string message call back. The call back will accept a string with the format of "R,G,B" and parse it to provide the color information to the FastLed library.

See "Modified Firmata" code below.

Univeral Windows App

1. Create a new Universal Windows App in Visual studio

2. Prepare the UI. XAML can be found in GitHub repository.

3. The universal app establishes a Network Serial connection to the Yun's Linux Serial port. (Note: This works only if the Yun was prepared as per the instructions in the earlier project.). There is a slight variation in the initialization of the Windows Remote Arduino in this code as the "sendString" function is available only on the UwpFirmata class. So an instance of UwpFirmata should be initialized first and passed to the constructor of RemoteDevice. Then a network serial connection should be prepared and passed as parameter to firmata.begin.. Then the network serial begin() should be called to open up connection to Arduino. The WRA initialization code is listed below: ​

4. The app displays an UI element with a color gradient. The app uses the WriteableBitmapEx NuGet package to add some extension methods that makes it easier to pick the color at the screen location where the user touches. Initialize the writeableBitmap so that it can be reused. Code:5. On Click/Touch event, picks the color at the touched point and sends it to Arduino Yun by using the firmata.sendString method. Making a "flush()" call after every send to ensure that the string is processed cleanly. I had issues without calling flush().

All source code is available at the following Github url.

Code

UWA: Init Windows Remote Arduino and Firmata ClientC#
Code fragment that initializes WRA
        RemoteDevice arduino;
        NetworkSerial netWorkSerial;
        UwpFirmata firmata;
        private async void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            ushort port = System.Convert.ToUInt16(this.txtPort.Text);

            firmata = new UwpFirmata();
            arduino = new RemoteDevice(firmata);

            netWorkSerial = new NetworkSerial(new Windows.Networking.HostName(this.txtIpAddress.Text), port);


            netWorkSerial.ConnectionEstablished += NetWorkSerial_ConnectionEstablished; ;
            netWorkSerial.ConnectionFailed += NetWorkSerial_ConnectionFailed; ;

            firmata.begin(netWorkSerial);
            netWorkSerial.begin(115200, SerialConfig.SERIAL_8N1);

            this.txtStatus.Text = "Connecting..";

        }
UWA: Init the writableBitmap to get the colorC#
Init the writableBitmap
       WriteableBitmap rgbGradient;
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            //Setup the WriteableBitmap object from which the color at the touched point will be retrieved.
            RenderTargetBitmap rgbGradientTarget = new RenderTargetBitmap();
            
            await rgbGradientTarget.RenderAsync(rgbSelector, rgbGradientTarget.PixelWidth, rgbGradientTarget.PixelHeight);
            IBuffer pixelBuffer = await rgbGradientTarget.GetPixelsAsync();

            var width = rgbGradientTarget.PixelWidth;
            var height = rgbGradientTarget.PixelHeight;

            rgbGradient = await new WriteableBitmap(width, height).FromPixelBuffer(pixelBuffer, width, height);

        }
UWA: Code that sends the color to Arduino through firmata.sendstringC#
        private void rgbSelector_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            var touchedPoint=e.GetCurrentPoint(this.rgbSelector);

            var scaledX = ((int)touchedPoint.Position.X / this.rgbSelector.Width) * this.rgbGradient.PixelWidth;
            var scaledY = ((int)touchedPoint.Position.Y / this.rgbSelector.Height) * this.rgbGradient.PixelHeight;

            Color color = rgbGradient.GetPixel((int)scaledX, (int)scaledY);

            var colorData = string.Format("{0},{1},{2}", color.R, color.G, color.B);

            if (firmata != null)
            {
                firmata.sendString(colorData);
                firmata.flush();
            }

            this.rgbText.Text = colorData;
            e.Handled = true;
        }
RGBFirmataArduino
Modified Firmata File specific handling just the String Data callback
/*
* Sketch for Arduino Yun to change color of RGB LED strip.
* Color is recieved from any Firmata Client as a string.
* Created by Mohan Palanisamy on Dec 24, 2015 as a simple sketch to  
* use with Windows Remote Arduino library
* Sketch should work with any Firmata Client
 */
#include <Firmata.h>
#include "FastLED.h"

#define NUM_LEDS 10
#define DATA_PIN 3

// Define the array of leds
CRGB leds[NUM_LEDS];

void stringCallback(char *myString)
{
  String rgbString=String(myString);
  int rIndex=rgbString.indexOf(',');
  int gIndex=rgbString.indexOf(',', rIndex+1);

  int r= rgbString.substring(0,rIndex).toInt();
  int g= rgbString.substring(rIndex+1,gIndex).toInt();
  int b= rgbString.substring(gIndex+1).toInt();
  
  for(int i=0;i<NUM_LEDS;i++)
  {
    leds[i]=CRGB(r,g,b);
    FastLED.show();
  }
}

void setup()
{
  Firmata.setFirmwareVersion(FIRMATA_MAJOR_VERSION, FIRMATA_MINOR_VERSION);
  
  //Interested only in the String_DATA callback. Ignore all other
  Firmata.attach(STRING_DATA, stringCallback);

  //This is Yun Specific.. Connects to Serail1 avaialble in Yun
  Serial1.begin(115200); 
  //this code is to delay the firmata begin process to avoid interference with the boot process when arduino linux side is restarted.
    do {
     while (Serial1.available() > 0) {
        Serial1.read();
        }
    delay(1000);
  } while (Serial1.available()>0);
  
  Firmata.begin(Serial1);

  FastLED.addLeds<TM1803, DATA_PIN, RBG>(leds, NUM_LEDS);

}

void loop()
{
  while (Firmata.available()) {
    Firmata.processInput();
  }
}

Comments

Similar projects you might like

Windows 8.1 IoT: Windows Remote Arduino and Universal apps

Project tutorial by David Jones

  • 2,914 views
  • 1 comment
  • 9 respects

Windows Remote Arduino

Project tutorial by Windows IoT

  • 179,946 views
  • 168 comments
  • 355 respects

IoT: Windows Remote Arduino and Universal Apps

Project tutorial by David Jones

  • 18,935 views
  • 12 comments
  • 44 respects

Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Project tutorial by Anurag S. Vasanwala

  • 318,631 views
  • 101 comments
  • 767 respects
Add projectSign up / Login