Example #1
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Nusbio Initializing");
            var serialNumber = Nusbio.Detect();
            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("Nusbio not detected");
                return;
            }

            var motionSensorGpio = 0;
            var red              = NusbioGpio.Gpio1;
            var green            = NusbioGpio.Gpio2;

            using (var nusbio = new Nusbio(serialNumber))
            {
                var motionSensor = new MotionSensorPIR(nusbio, motionSensorGpio, 3);
                var redLed       = nusbio.GPIOS[red].AsLed;   // Blink fast for 3 seconds when motion is detected
                var greenLed     = nusbio.GPIOS[green].AsLed; // Blink every 1/2 second just to tell system is running and is ok
                greenLed.SetBlinkMode(500);

                Cls(nusbio);
                while (nusbio.Loop())
                {
                    var motionType = motionSensor.MotionDetected();
                    if (motionType == MotionSensorPIR.MotionDetectedType.MotionDetected)
                    {
                        ConsoleEx.Write(0, 8, string.Format("[{0}] MotionSensor:{1,-20}", DateTime.Now, motionType), ConsoleColor.DarkCyan);
                        redLed.SetBlinkMode(200);
                    }

                    else if (motionType == MotionSensorPIR.MotionDetectedType.None)
                    {
                        ConsoleEx.Write(0, 8, string.Format("[{0}] MotionSensor:{1,-20}", DateTime.Now, motionType), ConsoleColor.DarkCyan);
                        redLed.SetBlinkModeOff();
                    }

                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;
                        if (k == ConsoleKey.Q) break;
                    }
                }
            }
            Console.Clear();
        }
Example #2
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 clockPin           = NusbioGpio.Gpio5; 
            var dataOutPin         = NusbioGpio.Gpio6; 
            var motionSensorPin    = NusbioGpio.Gpio0; 

            using (var nusbio = new Nusbio(serialNumber))
            {
                _MCP9808_TemperatureSensor = new MCP9808_TemperatureSensor(nusbio, dataOutPin, clockPin);
                if (!WaitForSensorsToBeReady(_MCP9808_TemperatureSensor))
                    Environment.Exit(1);

                var motionSensor = new MotionSensorPIR(nusbio, motionSensorPin, 3);

                Cls(nusbio);
                var every5Seconds  = new TimeOut(1000*5);
                var everyHalfSecond = new TimeOut(500);

                ReceiveCommands(AzureIoTHubDevices.TemperatureDevice);

                while(nusbio.Loop())
                {
                    if (everyHalfSecond.IsTimeOut())
                    {
                        var motionType = motionSensor.MotionDetected();
                        if (motionType == MotionSensorPIR.MotionDetectedType.MotionDetected)
                        {
                            ConsoleEx.WriteLine(0, 4, string.Format("[{0}] MotionSensor:{1,-20}", DateTime.Now, motionType), ConsoleColor.DarkCyan);
                            AzureIoTHubDevices.MotionSensorDevice.Update(DateTime.UtcNow);
                        }
                        else if (motionType == MotionSensorPIR.MotionDetectedType.None)
                        {
                            ConsoleEx.Write(0, 4, string.Format("[{0}] MotionSensor:{1,-20}", DateTime.Now, motionType), ConsoleColor.DarkCyan);
                        }
                    }
                    if (every5Seconds.IsTimeOut(isFirstTime:true)) 
                    {
                        double celsius = _MCP9808_TemperatureSensor.GetTemperature(MCP9808_TemperatureSensor.TemperatureType.Celsius);
                        ConsoleEx.WriteLine(0, 5, string.Format("[{0}]Temperature {1:000.00}C, {2:000.00}F, {3:00000.00}K",  DateTime.Now, celsius, _MCP9808_TemperatureSensor.CelsiusToFahrenheit(celsius), _MCP9808_TemperatureSensor.CelsiusToKelvin(celsius) ), ConsoleColor.Cyan);
                        if(AzureIoTHubDevices.TemperatureDevice.ShouldUpdate(celsius))
                            AzureIoTHubDevices.TemperatureDevice.Update(celsius);
                    }
                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;
                        if (k == ConsoleKey.T)
                        {
                            Cls(nusbio);
                        }
                        if (k == ConsoleKey.D0)
                        {
                            Cls(nusbio);
                        }
                        if (k == ConsoleKey.C)
                        {
                            Cls(nusbio);
                        }
                        if (k == ConsoleKey.Q) break;
                        Cls(nusbio);
                    }
                }
            }
            Console.Clear();
        }