Jorge RamírezAdrian Stevensbryan costanich
Published © Apache-2.0

Remote Control an RGB LED via Netduino and Xamarin!

Get familiar with Maple, a RESTful web server running on network capable Netduinos and control a rainbow LED remotely with a Xamarin app!

BeginnerFull instructions provided1 hour2,034
Remote Control an RGB LED via Netduino and Xamarin!

Things used in this project

Hardware components

Netduino
Wilderness Labs Netduino
×1
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1
RGB Diffused Common Anode
RGB Diffused Common Anode
×1

Software apps and online services

Visual Studio 2015
Microsoft Visual Studio 2015
Xamarin
Xamarin

Story

Read more

Code

RequeshHandler Class

C#
This class is in charge on handling all clients requests.
using Maple;
using Microsoft.SPOT;

namespace RgbLedHost
{
    public class RequestHandler : RequestHandlerBase
    {
        public event EventHandler TurnOn = delegate { };
        public event EventHandler TurnOff = delegate { };
        public event EventHandler StartBlink = delegate { };
        public event EventHandler StartPulse = delegate { };
        public event EventHandler StartRunningColors = delegate { };

        public RequestHandler() { }

        public void postTurnOn()
        {
            TurnOn(this, EventArgs.Empty);
            StatusResponse();
        }

        public void postTurnOff()
        {
            TurnOff(this, EventArgs.Empty);
            StatusResponse();
        }

        public void postStartBlink()
        {
            StartBlink(this, EventArgs.Empty);
            StatusResponse();
        }

        public void postStartPulse()
        {
            StartPulse(this, EventArgs.Empty);
            StatusResponse();
        }

        public void postStartRunningColors()
        {
            this.StartRunningColors(this, EventArgs.Empty);
            StatusResponse();
        }

        private void StatusResponse()
        {
            Context.Response.ContentType = "application/json";
            Context.Response.StatusCode = 200;
            Send();
        }
    }
}

RgbLedController Class

C#
Controller class that contains all the logic of the RgbPwmLed
using Netduino.Foundation.LEDs;
using System;
using System.Collections;

namespace RgbLedHost
{
    public class RgbLedController
    {
        protected RgbPwmLed _rgbPwmLed;

        public RgbLedController(RgbPwmLed rgbPwmLed)
        {
            _rgbPwmLed = rgbPwmLed;
            _rgbPwmLed.SetColor(Netduino.Foundation.Color.Red);
        }

        public void TurnOn()
        {
            _rgbPwmLed.Stop();
            _rgbPwmLed.SetColor(GetRandomColor());
        }

        public void TurnOff()
        {
            _rgbPwmLed.Stop();
            _rgbPwmLed.SetColor(Netduino.Foundation.Color.FromHsba(0, 0, 0));
        }

        public void StartBlink()
        {
            _rgbPwmLed.Stop();
            _rgbPwmLed.StartBlink(GetRandomColor());
        }

        public void StartPulse()
        {
            _rgbPwmLed.Stop();
            _rgbPwmLed.StartPulse(GetRandomColor());
        }

        public void StartRunningColors()
        {
            var arrayColors = new ArrayList();
            for (int i = 0; i < 360; i = i + 5)
            {
                var hue = ((double)i / 360F);
                arrayColors.Add(Netduino.Foundation.Color.FromHsba(((double)i / 360F), 1, 1));
            }

            int[] intervals = new int[arrayColors.Count];
            for (int i = 0; i < intervals.Length; i++)
            {
                intervals[i] = 100;
            }

            _rgbPwmLed.Stop();
            _rgbPwmLed.StartRunningColors(arrayColors, intervals);
        }

        public void NetworkConnected()
        {
            _rgbPwmLed.Stop();
            _rgbPwmLed.SetColor(Netduino.Foundation.Color.Green);
        }

        protected Netduino.Foundation.Color GetRandomColor()
        {
            var random = new Random();
            return Netduino.Foundation.Color.FromHsba(random.NextDouble(), 1, 1);
        }
    }
}

App Class

C#
Main class that manages Maple server and RgbLedController
using Maple;
using Microsoft.SPOT;
using Netduino.Foundation.LEDs;
using Netduino.Foundation.Network;
using N = SecretLabs.NETMF.Hardware.Netduino;

namespace RgbLedHost
{
    public class App
    {
        static int _blinkDuration = 100;
        protected MapleServer _server;
        protected RgbLedController _rgbController;

        public App()
        {
            InitializePeripherals();
            InitializeWebServer();
        }

        protected void InitializePeripherals()
        {
            var rgbPwmLed = new RgbPwmLed
            (
                N.PWMChannels.PWM_PIN_D11,
                N.PWMChannels.PWM_PIN_D10,
                N.PWMChannels.PWM_PIN_D9,
                1.05f,
                1.5f,
                1.5f,
                false // Common anode or cathode?
            );

            _rgbController = new RgbLedController(rgbPwmLed);
        }

        protected void InitializeWebServer()
        {
            var handler = new RequestHandler();

            handler.TurnOn += (s, e) => { _rgbController.TurnOn(); };
            handler.TurnOff += (s, e) => { _rgbController.TurnOff(); };
            handler.StartBlink += (s, e) => { _rgbController.StartBlink(); };
            handler.StartPulse += (s, e) => { _rgbController.StartPulse(); };
            handler.StartRunningColors += (s, e) => { _rgbController.StartRunningColors(); };

            _server = new MapleServer();
            _server.AddHandler(handler);
        }

        public void Run()
        {
            Initializer.InitializeNetwork();
            
            Debug.Print("InitializeNetwork()");

            while (Initializer.CurrentNetworkInterface == null) { }

            _server.Start("RgbLedHost", Initializer.CurrentNetworkInterface.IPAddress);
            _rgbController.NetworkConnected();
        }
    }
}

Program Class

C#
Main method of the RgbLedHost project
using System.Threading;

namespace RgbLedHost
{
    public class Program
    {
        public static void Main()
        {
            App app = new App();
            app.Run();

            Thread.Sleep(Timeout.Infinite);
        }
    }
}

Credits

Jorge Ramírez

Jorge Ramírez

74 projects • 74 followers
Developer advocate for Wilderness Labs.
Adrian Stevens

Adrian Stevens

70 projects • 44 followers
bryan costanich

bryan costanich

70 projects • 54 followers

Comments