Skip to content

Implementing a New Actuator

gloveboxes edited this page Apr 10, 2015 · 1 revision

Implementing a new Actuator

Be sure to check out the sensors and actuators that have been implemented in the Glovebox.Netduino project. See the Sensors and Actuators folders, all the code is provided to help you understand how things work.

The following example creates a relay switch with on/off command and control support.

Your actuator class must inherit from ActuatorBase.

using Glovebox.MicroFramework.Base;

namespace Glovebox.Netduino.Actuators {
    public class Relay : ActuatorBase {
    }
}

Implement the Abstract Class

Next right mouse click on ActuatorBase to Implement the Abstract Class.

using Glovebox.MicroFramework.Base;

namespace Glovebox.Netduino.Actuators {
    public class Relay : ActuatorBase {

        protected override void ActuatorCleanup() {
            throw new System.NotImplementedException();
        }

        public override void Action(MicroFramework.IoT.IotAction action) {
            throw new System.NotImplementedException();
        }
    }
}

Implement the class constructor

The ActuatorBase base constructor requires

  1. Name - This is a unique name that you can use to identify a actuator from the command and control service.

  2. Actuator Type - arbitrary/sensible type for the actuator. For now not used.

     public Relay(Cpu.Pin pin, string name)
         : base(name, "relay") {
         relay = new OutputPort(pin, false);
     }
    

Implement the Actuator Cleanup

    protected override void ActuatorCleanup() {
        relay.Dispose();
    }

Implement Command and Control

See the Lab Guide Appendix for information on sending a command via MQTT.

    public override void Action(Glovebox.MicroFramework.IoT.IotAction action) {
        switch (action.cmd) {
            case "on":
                relay.Write(true);
                break;
            case "off":
                relay.Write(false);
                break;
        }
    }

The Completed Relay Class

using Glovebox.MicroFramework.Base;
using Microsoft.SPOT.Hardware;

namespace Glovebox.Netduino.Actuators {
    public class Relay : ActuatorBase {

        private OutputPort relay;

        /// <summary>
        /// Create a relay control
        /// </summary>
        /// <param name="pin">From the SecretLabs.NETMF.Hardware.NetduinoPlus.Pins namespace</param>
        /// <param name="name">Unique identifying name for command and control</param>
        public Relay(Cpu.Pin pin, string name)
            : base(name, "relay") {
            relay = new OutputPort(pin, false);
        }

        protected override void ActuatorCleanup() {
            relay.Dispose();
        }

        public override void Action(Glovebox.MicroFramework.IoT.IotAction action) {
            switch (action.cmd) {
                case "on":
                    relay.Write(true);
                    break;
                case "off":
                    relay.Write(false);
                    break;
            }
        }
    }
}

Coder Friendly Relay Class

This version adds an Actions enumeration to make it more developer friendly.

using Glovebox.MicroFramework.Base;
using Microsoft.SPOT.Hardware;

namespace Glovebox.Netduino.Actuators {
    public class Relay : ActuatorBase {

        public enum Actions { On, Off }

        private OutputPort relay;

        /// <summary>
        /// Create a relay control
        /// </summary>
        /// <param name="pin">From the SecretLabs.NETMF.Hardware.NetduinoPlus.Pins namespace</param>
        /// <param name="name">Unique identifying name for command and control</param>
		public Relay(Cpu.Pin pin, string name)
            : base(name, "relay") {
            relay = new OutputPort(pin, false);
        }

        protected override void ActuatorCleanup() {
            relay.Dispose();
        }

        public override void Action(Glovebox.MicroFramework.IoT.IotAction action) {
            switch (action.cmd) {
                case "on":
                    TurnOn();
                    break;
                case "off":
                    TurnOff();
                    break;
            }
        }

        public void Action(Actions action) {
            switch (action) {
                case Actions.On:
                    TurnOn();
                    break;
                case Actions.Off:
                    TurnOff();
                    break;
            }
        }

        public void TurnOn() {
            relay.Write(true);
        }

        public void TurnOff() {
            relay.Write(false);
        }
    }
}

Using your newly created actuator

This example uses the Light Dependent Resistor Sensor to determine the light levels. Depending on the light level, the Relay will be turned on or off. The relay could be controlling a light.

When you reference a pin on the Netduino in your code it is important to use the SecretLabs.NETMF.Hardware.NetduinoPlus namespace not the Microsoft.SPOT.Hardware.Cpu.Pin which (unfortunately) is the Visual Studio default.

// program.cs    
using Glovebox.Netduino.Actuators;
using Glovebox.Netduino.Sensors;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using System.Threading;

namespace MakerDen {
    public class Program : MakerBaseIoT {

        public static void Main() {

            using (Sensorldr ldr = new Sensorldr(AnalogChannels.ANALOG_PIN_A0, -1, "ldr01")) 
            using (Relay relay = new Relay(Pins.GPIO_PIN_D0, "relay01")) {

                while (true) {
                    if (ldr.Current < 60) {
                        relay.Action(Relay.Actions.On);
                    }
                    else {
                        relay.Action(Relay.Actions.Off);
                    }
                    // good practice not to put your netduino in to a hard loop, so add a thread sleep
                    Thread.Sleep(100);
                }
            }
        }
    }
}

Bringing it all together

The following example brings it all together and uses all the sensors and actuators defined in the IoT Solution Accelerator.

// program.cs
using Glovebox.MicroFramework.Sensors;
using Glovebox.Netduino.Actuators;
using Glovebox.Netduino.Sensors;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using System.Threading;

namespace MakerDen {
    public class Program : MakerBaseIoT {

        public static void Main() {
            Piezo speaker = new Piezo(PWMChannels.PWM_PIN_D9, "speaker01");
            speaker.BeebStartup();

            // main code marker

            //Replace the "emul" which is the name of the device with a unique 3 to 5 character name
            //use your initials or something similar.  This code will be visible on the IoT Dashboard
            StartNetworkServices("test", true);

            using (SensorTemp temp = new SensorTemp(Pins.GPIO_PIN_D8, 10000, "temp01"))
            using (SensorLight light = new SensorLight(AnalogChannels.ANALOG_PIN_A0, 1000, "light01"))
            using (SensorSound sound = new SensorSound(AnalogChannels.ANALOG_PIN_A4, 1000, "sound01"))
            using (SensorMemory mem = new SensorMemory(5000, "mem01"))
            using (rgb = new RgbLed(Pins.GPIO_PIN_D3, Pins.GPIO_PIN_D5, Pins.GPIO_PIN_D6, "rgb01"))
            using (Relay relay = new Relay(Pins.GPIO_PIN_D7, "relay01")) {

                speaker.BeepOK();

                temp.OnBeforeMeasurement += OnBeforeMeasure;
                temp.OnAfterMeasurement += OnMeasureCompleted;

                light.OnBeforeMeasurement += OnBeforeMeasure;
                light.OnAfterMeasurement += OnMeasureCompleted;

                sound.OnBeforeMeasurement += OnBeforeMeasure;
                sound.OnAfterMeasurement += OnMeasureCompleted;

                mem.OnBeforeMeasurement += OnBeforeMeasure;
                mem.OnAfterMeasurement += OnMeasureCompleted;

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

This solution is publishing sensor data to the Mosquitto MQTT Service running on Microsoft Azure. The data is displayed using the IoT Dashboard.

IoT Dashboard