Beispiel #1
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Nusbio initialization");
            var serialNumber = Nusbio.Detect();
            //var serialNumber = "LD2Ub9pAg";
            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("nusbio not detected");
                return;
            }
            
            var clockPin           = NusbioGpio.Gpio6; // White, Arduino A5
            var dataOutPin         = NusbioGpio.Gpio5; // Green, Arduino A4

            using (var nusbio = new Nusbio(serialNumber))
            {
                _MCP9808_TemperatureSensor = new MCP9808_TemperatureSensor(nusbio, dataOutPin, clockPin);
                if (!_MCP9808_TemperatureSensor.Begin())
                {
                    Console.WriteLine("MCP9808 not detected on I2C bus");
                    return;
                }

                Cls(nusbio);

                var everySecond = new TimeOut(1000);

                while(nusbio.Loop())
                {
                    if (everySecond.IsTimeOut())
                    {
                        double celsius = 0;
                        for (var i = 0; i < 3; i++)
                        {
                            celsius = _MCP9808_TemperatureSensor.GetTemperature(MCP9808_TemperatureSensor.TemperatureType.Celsius);
                        }

                        ConsoleEx.WriteLine(1, 2, 
                            string.Format("Temperature Celsius:{0:000.00}, Fahrenheit:{1:000.00}, Kelvin:{2:00000.00}", 
                            celsius,
                            _MCP9808_TemperatureSensor.CelsiusToFahrenheit(celsius),
                            _MCP9808_TemperatureSensor.CelsiusToKelvin(celsius)
                            ), ConsoleColor.Cyan);
                    }
                    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();
        }
Beispiel #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();
        }