Skip to content

Implementing a New Sensor

Dave Glover edited this page Jun 2, 2015 · 1 revision

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

Inheriting from SensorBase

Your sensor class must inherit from SensorBase.

using Glovebox.IoT.Base;

namespace Glovebox.IO.Components.Sensors
{
    public class SensorLight : SensorBase
    {
    }
}

Implement the Abstract Class

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

using System;
using Glovebox.IoT.Base;

namespace Glovebox.IO.Components.Sensors
{
    public class SensorLight : SensorBase
    {
        public override double Current
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        protected override string GeoLocation()
        {
            throw new NotImplementedException();
        }

        protected override void Measure(double[] value)
        {
            throw new NotImplementedException();
        }

        protected override void SensorCleanup()
        {
            throw new NotImplementedException();
        }
    }
}

Initialise the Sensor Base Constructor

Add a Sensor Constructor and initialise the SensorBase Base constructor.

The SensorBase base constructor requires

  1. Sensor Type - arbitrary/sensible type for the sensor. The value is published alongside the sensor reading to provide some type information.

  2. Sensor Unit - arbitrary/sensible measurement unit for the sensor. Example p for percentage, n for numeric etc. The unit is published alongside the sensor reading to provide some unit information.

  3. Values Per Sample - Defines how many values will be collected per reading. Most sensors generate only one value per sample. But there are sensors that generate more data. For example a sensor that sensors both temperature and humidity.

  4. Sample Rate in Milliseconds - how often to take a sensor reading.

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

    using Glovebox.IO.Components.Converters; using Glovebox.IO.Components.Drivers; using Glovebox.IoT.Base; using System; using UnitsNet; using UnitsNet.Units;

    namespace Glovebox.IO.Components.Sensors { public class SensorLight : SensorBase { LDR ldr;

         public SensorLightV1(ADS1015 adc, int SampleRateMilliseconds, string name)
             : base("light", "p", ValuesPerSample.One, SampleRateMilliseconds, name)
         {
             ldr = new LDR(adc, ADS1015.Channel.A3, ADS1015.Gain.Volt5, ElectricPotential.From(5, ElectricPotentialUnit.Volt));
    
             StartMeasuring();
         }
    
         public override double Current
         {
             get
             {
                 throw new NotImplementedException();
             }
         }
    
         protected override string GeoLocation()
         {
             throw new NotImplementedException();
         }
    
         protected override void Measure(double[] value)
         {
             throw new NotImplementedException();
         }
    
         protected override void SensorCleanup()
         {
             throw new NotImplementedException();
         }
     }
    

    }

Implement the sensor logic

Implement the abstract methods and properties for the Sensor class.

using Glovebox.IO.Components.Converters;
using Glovebox.IO.Components.Drivers;
using Glovebox.IoT.Base;
using System;
using UnitsNet;
using UnitsNet.Units;

namespace Glovebox.IO.Components.Sensors
{
    public class SensorLight : SensorBase
    {
        LDR ldr;

        public SensorLight(ADS1015 adc, int SampleRateMilliseconds, string name)
            : base("light", "p", ValuesPerSample.One, SampleRateMilliseconds, name)
        {
            ldr = new LDR(adc, ADS1015.Channel.A3, ADS1015.Gain.Volt5, ElectricPotential.From(5, ElectricPotentialUnit.Volt));

            StartMeasuring();
        }

        public override double Current
        {
            get
            {
                return ldr.Measure();
            }
        }

        protected override string GeoLocation()
        {
            return string.Empty;
        }

        protected override void Measure(double[] value)
        {
            value[0] = ldr.Measure();
        }

        protected override void SensorCleanup()
        {
        }
    }
}

Using your newly created sensor

// MakerDen.cs
using Glovebox.IO.Components.Sensors;
using Glovebox.IoT;
using System.Threading;

namespace MakerDen
{
    public class MakerDen : IoTServices
    {
        public MakerDen() : base(true) { } // if Explorer Hat installed set : base(true)

        public void Main()
        {
            using (SensorLight light = new SensorLight(adc, 1000, "light01"))
            {
                light.OnBeforeMeasurement += OnBeforeMeasure;
                light.OnAfterMeasurement += OnAfterMeasurement;

                Util.Delay(Timeout.Infinite);
            }
        }  //  End of Main()
    }
}

Or

// MakerDen.cs
using Glovebox.IO.Components.Sensors;
using Glovebox.IoT;

namespace MakerDen
{
    public class MakerDen : IoTServices
    {
        public MakerDen() : base(true) { } // if Explorer Hat installed set : base(true)

        public void Main()
        {
            using (SensorLight light = new SensorLight(adc, 1000, "light01"))
            {
                while (true)
                {
                    if (light.Current < 60)
                    {
                        // do something...
                    }
                    else
                    {
                        // do something...
                    }
                    Util.Delay(100);
                }
            }
        }  //  End of Main()
    }
}