Windows Remote Arduino WiFi Lamp with Arduino Yun

Universal Windows App and the Wifi Lamp

In an earlier post i explained how i exposed Arduino Yun Serial Port over TCP.   This allowed me to control an Arduino Yun with 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.

Note: 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 though 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 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 before installing it in the lamp.

Picture

Software

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.

/*
* 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() &amp;gt; 0) 
        {
           Serial1.read();
        }
    delay(1000);
  } while (Serial1.available()&amp;gt;0);
  
  Firmata.begin(Serial1);

  FastLED.addLeds&amp;lt;TM1803, DATA_PIN, RBG&amp;gt;(leds, NUM_LEDS);
}

void loop()
{
  while (Firmata.available()) {
    Firmata.processInput();
  }
}
Universal Windows App with Windows Remote Arduino
  1. Create a new Universal Windows App in Visual studio
  2. Add Windows Remote Arduino package from Nuget using the Package manager UI or console.

    Windows Remote Arduino Nuget Package Install
    Windows Remote Arduino Nuget Package Install
  3. Prepare the UI. The XAML for the UI can be found in WRAWifiLight GitHub repository.
  4. 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: ​
            RemoteDevice arduino;
            NetworkSerial netWorkSerial;
            UwpFirmata firmata;
            private 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..";
            }
    
            private void NetWorkSerial_ConnectionFailed(string message)
            {
                this.txtStatus.Text = string.Format("Connection Failed. {0}", message);
            }
    
            private void NetWorkSerial_ConnectionEstablished()
            {
                this.txtStatus.Text = "Connected";
            }
    
  5. 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.
            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);
            }
    
  6. 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().
            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;
            }
    

All source code is available at the following Github url.

Leave a Reply

Your email address will not be published. Required fields are marked *