Mohan Palanisamy
Published © MIT

WiFiLamp with FastLED and Windows Remote Arduino

Driving a LED Strip with Windows Remote Arduino over WiFi

BeginnerFull instructions provided7,204
WiFiLamp with FastLED and Windows Remote Arduino

Things used in this project

Hardware components

Arduino Yun
Arduino Yun
×1
TM1803 LED Strip
×1

Software apps and online services

Microsoft Windows Remote Arduino

Story

Read more

Code

UWA: Init Windows Remote Arduino and Firmata Client

C#
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 color

C#
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.sendstring

C#
        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;
        }

RGBFirmata

Arduino
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();
  }
}

Github

Credits

Mohan Palanisamy

Mohan Palanisamy

6 projects • 7 followers

Comments