Ejemplo n.º 1
0
        /// <summary>
        /// </summary>
        /// <param name="spi"></param>
        static void ADC_MCP3008Demo(ISPI spi)
        {
            var adc  = new MCP3008(spi);
            var done = false;

            System.Console.Clear();
            const double referenceVoltage = 5;

            while (!done)
            {
                for (var adcPort = 0; adcPort < 1; adcPort++)
                {
                    var adcValue     = adc.Read(adcPort);
                    var voltageValue = adc.ComputeVoltage(referenceVoltage, adcValue);
                    System.Console.WriteLine($"ADC [{adcPort}] = {adcValue}, voltage:{voltageValue}");
                }

                if (System.Console.KeyAvailable)
                {
                    if (System.Console.ReadKey().Key == ConsoleKey.Q)
                    {
                        done = true;
                        break;
                    }
                }
                Thread.Sleep(500);
            }
        }
Ejemplo n.º 2
0
 public JoyStick(Nusbio nusbio, MCP3008 adc, int xAdPort, int yAdPort, int buttonAdPort)
 {
     this._nusbio   = nusbio;
     this._adc      = adc;
     this.XSensor   = new Sensor.AnalogSensor(nusbio, xAdPort);
     this.YSensor   = new Sensor.AnalogSensor(nusbio, yAdPort);
     this.ButSensor = new Sensor.AnalogSensor(nusbio, buttonAdPort);
 }
Ejemplo n.º 3
0
        private async void MainPageLoaded(object sender, RoutedEventArgs e)
        {
            //Initialize objects
            gpioController = GpioController.GetDefault();
            IAdcControllerProvider MCP3008_SPI0 = new MCP3008();

            adcManager   = new AdcProviderManager();
            lightVals    = new List <double>();
            deviceClient = DeviceClient
                           .CreateFromConnectionString(IoTHubConnString,
                                                       TransportType.Http1);
            timer          = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(500);
            timer.Tick    += BroadcastTelemetry;
            timer.Start();
            //Insert 10 dummy values to initialize.
            for (int i = 0; i < 30; i++)
            {
                lightVals.Add(0);
            }

            tempVals = new List <double>();
            for (int i = 0; i < 30; i++)
            {
                tempVals.Add(0);
            }
            #region ADC Provider Stuff
            //Add ADC Provider to list of providers
            adcManager.Providers.Add(MCP3008_SPI0);
            //Get all ADC Controllers.
            var adcControllers = await adcManager.GetControllersAsync();

            //This is just how its done.
            #endregion ADC Provider Stuff
            var lightSensor = new AnalogSensor()
            {
                //Notice access via controller index.
                //You will need to keep tabs on your
                //ADC providers locations in the list
                //Channel 7 as this is where we wired the photo resistor to.
                AdcChannel = adcControllers[0].OpenChannel(7),
                //every 500 milliseconds, grab read the value.
                ReportInterval = 250
            };
            //Attach a function to the event we fire every 500 milliseconds.
            lightSensor.ReadingChanged += LightSensor_ReadingChanged;

            #region Temp Sensor Cheat Codes
            var tempSensor = new AnalogSensor()
            {
                AdcChannel     = adcControllers[0].OpenChannel(6),
                ReportInterval = 250
            };
            tempSensor.ReadingChanged += TempSensor_ReadingChanged;
            #endregion Temp Sensor Cheat Codes
        }
Ejemplo n.º 4
0
        public double readRawData()
        {
            GPIOMem SPICLK    = new GPIOMem(GPIOPins.Pin_P1_18, GPIODirection.Out);
            GPIOMem SPIMISO   = new GPIOMem(GPIOPins.Pin_P1_23, GPIODirection.In);
            GPIOMem SPIMOSI   = new GPIOMem(GPIOPins.Pin_P1_24, GPIODirection.Out);
            GPIOMem SPICS     = new GPIOMem(GPIOPins.Pin_P1_22, GPIODirection.Out);
            int     adcnum    = 0;
            double  read_adc0 = 0.0;
            MCP3008 MCP3008   = new MCP3008(adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS);

            read_adc0 = MCP3008.AnalogToDigital;
            return(read_adc0);
        }
        public PlantMonitoringService()
        {
            _mcp3008             = new MCP3008();
            _dhtTemeratureSensor = new DhtTemeratureSensor();

            initAnalogDevice().ContinueWith(y =>
            {
                InitPlantMonitoringSystem()
                .ContinueWith(x =>
                {
                    Debug.WriteLine($"{nameof(PlantMonitoringService)} Initialize and starting telemtry!");
                    Task.Factory.StartNew(() => SensorLoopAsync(500));
                });
            });
        }
Ejemplo n.º 6
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Nusbio initialization");
            var serialNumber = Nusbio.Detect();

            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("Nusbio not detected");
                return;
            }

            using (var nusbio = new Nusbio(serialNumber))
            {
                Cls(nusbio);
                var halfSeconds = new TimeOut(111);
                var adc         = new MCP3008(nusbio,
                                              selectGpio: NusbioGpio.Gpio3,
                                              mosiGpio: NusbioGpio.Gpio1,
                                              misoGpio: NusbioGpio.Gpio2,
                                              clockGpio: NusbioGpio.Gpio0);
                adc.Begin();
                var joyStick = new JoyStick(nusbio, adc, 2, 3, 4);

                while (nusbio.Loop())
                {
                    if (halfSeconds.IsTimeOut())
                    {
                        ConsoleEx.WriteLine(0, 4, string.Format(
                                                "JoyStick X:{0,4:000} {1,4}, Y:{2,4:000} {3,5}, ButtonDown:{4}  ",
                                                joyStick.X, joyStick.XDir,
                                                joyStick.Y, joyStick.YDir,
                                                joyStick.ButtonPressed), ConsoleColor.Cyan);
                    }

                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;
                        if (k == ConsoleKey.Q)
                        {
                            break;
                        }
                        Cls(nusbio);
                    }
                }
            }
            Console.Clear();
        }
Ejemplo n.º 7
0
        public void StartMe()
        {
            Console.WriteLine("Tempertur:");
            //# set up the SPI interface pins
            //# SPI port on the ADC to the Cobbler
            GPIOMem SPICLK  = new GPIOMem(GPIOPins.GPIO_11, GPIODirection.Out);
            GPIOMem SPIMISO = new GPIOMem(GPIOPins.GPIO_09, GPIODirection.In);
            GPIOMem SPIMOSI = new GPIOMem(GPIOPins.GPIO_10, GPIODirection.Out);
            GPIOMem SPICS   = new GPIOMem(GPIOPins.GPIO_08, GPIODirection.Out);

            int    adcum     = 1;
            double read_adc0 = 1;

            while (true)
            {
                MCP3008 ADChip = new MCP3008(adcum, SPICLK, SPIMOSI, SPIMISO, SPICS);
                read_adc0 = ADChip.AnalogToDigital;
                double millivolts = Convert.ToDouble(read_adc0) * (3300 / 1024);
                double temp_C     = ((millivolts - 100) / 10) - 40;
                Console.WriteLine("read_adc0:{0}", read_adc0);
                Thread.Sleep(3000);
            }
        }
Ejemplo n.º 8
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Nusbio initialization");
            var serialNumber = Nusbio.Detect();

            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("Nusbio not detected");
                return;
            }

            var lightSensorAnalogPort  = 2;
            var motionSensorAnalogPort = 0;
            var buttonSensorAnalogPort = 1;
            var ledGpio = NusbioGpio.Gpio5;

            using (var nusbio = new Nusbio(serialNumber))
            {
                Cls(nusbio);

                var halfSeconds = new TimeOut(333);

                // Mcp300X Analog To Digital - SPI Config
                ad = new MCP3008(nusbio,
                                 selectGpio: NusbioGpio.Gpio3,
                                 mosiGpio:   NusbioGpio.Gpio1,
                                 misoGpio:   NusbioGpio.Gpio2,
                                 clockGpio:  NusbioGpio.Gpio0);
                ad.Begin();

                var analogMotionSensor = new AnalogMotionSensor(nusbio, 4);
                analogMotionSensor.Begin();

                var button = new AnalogButton(nusbio);

                var lightSensor = CalibrateLightSensor(new AnalogLightSensor(nusbio), AnalogLightSensor.LightSensorType.CdsPhotoCell_3mm_45k_140k);
                lightSensor.Begin();

                // Analog Port 5, 6, 7 are only available in
                // Analog Extension PCBv2
                const int    multiButtonPort = 5;
                AnalogSensor multiButton     = null;
                //multiButton = new AnalogSensor(nusbio, multiButtonPort);
                //multiButton.Begin();

                // TC77 Temperature Sensor SPI
                var tc77 = new TC77(nusbio,
                                    clockGpio:  NusbioGpio.Gpio0,
                                    mosiGpio:   NusbioGpio.Gpio1,
                                    misoGpio:   NusbioGpio.Gpio2,
                                    selectGpio: NusbioGpio.Gpio4
                                    );
                tc77.Begin();

                if (nusbio.Type == NusbioType.NusbioType1_Light)
                {
                    tc77._spi.SoftwareBitBangingMode     = true;
                    ad._spiEngine.SoftwareBitBangingMode = true;
                }

                while (nusbio.Loop())
                {
                    if (halfSeconds.IsTimeOut())
                    {
                        nusbio[ledGpio].AsLed.ReverseSet();

                        ConsoleEx.WriteLine(0, 2, string.Format("{0,-15}", DateTime.Now), ConsoleColor.Cyan);

                        lightSensor.SetAnalogValue(ad.Read(lightSensorAnalogPort));
                        ConsoleEx.WriteLine(0, 4, string.Format("Light Sensor       : {0,-18} (ADValue:{1:000.000}, Volt:{2:0.00})       ",
                                                                lightSensor.CalibratedValue.PadRight(18), lightSensor.AnalogValue, lightSensor.Voltage), ConsoleColor.Cyan);

                        analogMotionSensor.SetAnalogValue(ad.Read(motionSensorAnalogPort));
                        var motionType = analogMotionSensor.MotionDetected();
                        if (motionType == DigitalMotionSensorPIR.MotionDetectedType.MotionDetected || motionType == DigitalMotionSensorPIR.MotionDetectedType.None)
                        {
                            ConsoleEx.Write(0, 6, string.Format("Motion Sensor      : {0,-18} (ADValue:{1:000.000}, Volt:{2:0.00})    ",
                                                                motionType, analogMotionSensor.AnalogValue, analogMotionSensor.Voltage), ConsoleColor.Cyan);
                        }

                        ConsoleEx.WriteLine(0, 8, string.Format("Temperature Sensor : {0:0.00}C {1:0.00}F    ", tc77.GetTemperature(),
                                                                tc77.GetTemperature(AnalogTemperatureSensor.TemperatureType.Fahrenheit)), ConsoleColor.Cyan);

                        button.SetAnalogValue(ad.Read(buttonSensorAnalogPort));
                        ConsoleEx.WriteLine(0, 10, string.Format("Button             : {0,-18} [{1:0000}, {2:0.00}V]   ",
                                                                 button.Down ? "Down" : "Up", button.AnalogValue, button.Voltage), ConsoleColor.Cyan);

                        if (multiButton != null)
                        {
                            multiButton.SetAnalogValue(ad.Read(multiButtonPort));
                            ConsoleEx.Write(0, 12, string.Format("Multi Button       : {0,-18} (ADValue:{1:000.000}, Volt:{2:000.000})",
                                                                 multiButton.AnalogValue > 2 ? "Down" : "Up  ",
                                                                 multiButton.AnalogValue,
                                                                 multiButton.Voltage), ConsoleColor.Cyan);
                        }
                    }

                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;

                        if (k == ConsoleKey.C)
                        {
                            Cls(nusbio);
                        }
                        if (k == ConsoleKey.Q)
                        {
                            break;
                        }
                        Cls(nusbio);
                    }
                }
            }
            Console.Clear();
        }
Ejemplo n.º 9
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Nusbio initialization");
            var serialNumber = Nusbio.Detect();

            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("Nusbio not detected");
                return;
            }

            using (var nusbio = new Nusbio(serialNumber))
            {
                Cls(nusbio);

                const int motionSensorAnalogPort      = 0;
                const int temperatureSensorAnalogPort = 1;
                const int multiButtonPort             = 3;
                const int lightSensorAnalogPort       = 6; // Has a pull down resistor

                var halfSeconds = new TimeOut(200);

                /*
                 *  Mcp300X - SPI Config
                 *  gpio 0 - CLOCK
                 *  gpio 1 - MOSI
                 *  gpio 2 - MISO
                 *  gpio 3 - SELECT
                 */
                ad = new MCP3008(nusbio,
                                 selectGpio: NusbioGpio.Gpio3,
                                 mosiGpio: NusbioGpio.Gpio1,
                                 misoGpio: NusbioGpio.Gpio2,
                                 clockGpio: NusbioGpio.Gpio0);
                ad.Begin();

                var analogTempSensor = new Tmp36AnalogTemperatureSensor(nusbio, 5);
                analogTempSensor.Begin();

                var analogMotionSensor = new AnalogMotionSensor(nusbio, 4);
                analogMotionSensor.Begin();

                var lightSensor = CalibrateLightSensor(new AnalogLightSensor(nusbio), AnalogLightSensor.LightSensorType.CdsPhotoCell_3mm_45k_140k);
                lightSensor.Begin();

                AnalogSensor multiButton = null;
                //multiButton = new AnalogSensor(nusbio, multiButtonPort);
                //multiButton.Begin();

                ulong loopCounter = 0;

                while (nusbio.Loop())
                {
                    if (halfSeconds.IsTimeOut())
                    {
                        ConsoleEx.WriteLine(0, 2, string.Format("{0,-20}, {1}", DateTime.Now, loopCounter++), ConsoleColor.Cyan);

                        lightSensor.SetAnalogValue(ad.Read(lightSensorAnalogPort));
                        ConsoleEx.WriteLine(0, 4, string.Format("Light Sensor       : {0} (ADValue:{1:000.000}, Volt:{2:000.000})    ",
                                                                lightSensor.CalibratedValue.PadRight(18),
                                                                lightSensor.AnalogValue,
                                                                lightSensor.Voltage), ConsoleColor.Cyan);

                        // There is an issue with the TMP36 and the ADC MCP3008
                        // The correction is to add a 2k resistors between the out of the temperature sensor
                        // and the AD port and increase the value by 14%
                        const int TMP36_AccuracyCorrection = 14;
                        analogTempSensor.SetAnalogValue(ad.Read(temperatureSensorAnalogPort, TMP36_AccuracyCorrection));
                        ConsoleEx.WriteLine(0, 6, string.Format("Temperature Sensor : {0:00.00}C, {1:00.00}F     (ADValue:{2:0000}, Volt:{3:000.000})      ",
                                                                analogTempSensor.GetTemperature(AnalogTemperatureSensor.TemperatureType.Celsius),
                                                                analogTempSensor.GetTemperature(AnalogTemperatureSensor.TemperatureType.Fahrenheit),
                                                                analogTempSensor.AnalogValue,
                                                                analogTempSensor.Voltage), ConsoleColor.Cyan);

                        analogMotionSensor.SetAnalogValue(ad.Read(motionSensorAnalogPort));
                        var motionType = analogMotionSensor.MotionDetected();
                        if (motionType == DigitalMotionSensorPIR.MotionDetectedType.MotionDetected ||
                            motionType == DigitalMotionSensorPIR.MotionDetectedType.None ||
                            motionType == DigitalMotionSensorPIR.MotionDetectedType.SameMotionDetected)
                        {
                            ConsoleEx.Write(0, 8, string.Format("Motion Sensor      : {0,-18} (ADValue:{1:000.000}, Volt:{2:000.000})", motionType, analogMotionSensor.AnalogValue, analogMotionSensor.Voltage), ConsoleColor.Cyan);
                        }

                        if (multiButton != null)
                        {
                            multiButton.SetAnalogValue(ad.Read(multiButtonPort));
                            ConsoleEx.Write(0, 10, string.Format("Multi Button       : {0,-18} (ADValue:{1:000.000}, Volt:{2:000.000})",
                                                                 multiButton.AnalogValue > 2 ? "Down" : "Up  ",
                                                                 multiButton.AnalogValue,
                                                                 multiButton.Voltage), ConsoleColor.Cyan);
                        }
                    }

                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;

                        if (k == ConsoleKey.C)
                        {
                            Cls(nusbio);
                        }
                        if (k == ConsoleKey.Q)
                        {
                            break;
                        }
                        Cls(nusbio);
                    }
                }
            }
            Console.Clear();
        }
Ejemplo n.º 10
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Nusbio initialization");
            var serialNumber = Nusbio.Detect();

            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("Nusbio not detected");
                return;
            }

            using (var nusbio = new Nusbio(serialNumber))
            {
                Cls(nusbio);
                var halfSeconds = new TimeOut(1000);

                /*
                 *  Mcp300X - SPI Config
                 *  gpio 0 - CLOCK
                 *  gpio 1 - MOSI
                 *  gpio 2 - MISO
                 *  gpio 3 - SELECT
                 */
                ad = new MCP3008(nusbio,
                                 selectGpio: NusbioGpio.Gpio3,
                                 mosiGpio:   NusbioGpio.Gpio1,
                                 misoGpio:   NusbioGpio.Gpio2,
                                 clockGpio:  NusbioGpio.Gpio0);
                ad.Begin();

                var analogTempSensor = new Tmp36AnalogTemperatureSensor(nusbio);
                analogTempSensor.Begin();
                analogTempSensor.ReferenceVoltage = 5.05; // If you can measure your voltage out of the Nusbio VCC

                var analogMotionSensor = new AnalogMotionSensor(nusbio, 4);
                analogMotionSensor.Begin();

                var lightSensor = CalibrateLightSensor(new AnalogLightSensor(nusbio), AnalogLightSensor.LightSensorType.CdsPhotoCell_5mm_5k_200k);

                lightSensor.Begin();

                while (nusbio.Loop())
                {
                    if (halfSeconds.IsTimeOut())
                    {
                        const int lightSensorAnalogPort       = 6;
                        const int motionSensorAnalogPort      = 2;
                        const int temperatureSensorAnalogPort = 0;

                        ConsoleEx.WriteLine(0, 2, string.Format("{0,-20}", DateTime.Now, lightSensor.AnalogValue), ConsoleColor.Cyan);

                        lightSensor.SetAnalogValue(ad.Read(lightSensorAnalogPort));
                        ConsoleEx.WriteLine(0, 4, string.Format("Light Sensor       : {0} (ADValue:{1:000.000}, Volt:{2:000.000})    ",
                                                                lightSensor.CalibratedValue.PadRight(18),
                                                                lightSensor.AnalogValue,
                                                                lightSensor.Voltage), ConsoleColor.Cyan);


                        analogTempSensor.SetAnalogValue(ad.Read(temperatureSensorAnalogPort));
                        ConsoleEx.WriteLine(0, 6, string.Format("Temperature Sensor : {0:00.00}C, {1:00.00}F     (ADValue:{2:0000}, Volt:{3:000.000})      ",
                                                                analogTempSensor.GetTemperature(AnalogTemperatureSensor.TemperatureType.Celsius),
                                                                analogTempSensor.GetTemperature(AnalogTemperatureSensor.TemperatureType.Fahrenheit),
                                                                analogTempSensor.AnalogValue,
                                                                analogTempSensor.Voltage), ConsoleColor.Cyan);

                        //analogMotionSensor.SetAnalogValue(ad.Read(motionSensorAnalogPort));
                        //var motionType = analogMotionSensor.MotionDetected();
                        //if (motionType == DigitalMotionSensorPIR.MotionDetectedType.MotionDetected || motionType == DigitalMotionSensorPIR.MotionDetectedType.None)
                        //{
                        //    ConsoleEx.Write(0, 8, string.Format("Motion Sensor     : {0,-20} (ADValue:{1:000.000}, Volt:{2:000.000})", motionType, analogMotionSensor.AnalogValue, analogMotionSensor.Voltage), ConsoleColor.Cyan);
                        //}
                    }

                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;

                        if (k == ConsoleKey.C)
                        {
                            Cls(nusbio);
                        }
                        if (k == ConsoleKey.Q)
                        {
                            break;
                        }
                        Cls(nusbio);
                    }
                }
            }
            Console.Clear();
        }