Esempio n. 1
0
        private static bool WaitForSensorsToBeReady(MCP9808_TemperatureSensor mcp9808_TemperatureSensor)
        {
            Console.Clear();
            ConsoleEx.TitleBar(0, GetAssemblyProduct(), ConsoleColor.Yellow, ConsoleColor.DarkBlue);
            ConsoleEx.TitleBar(ConsoleEx.WindowHeight-2, Nusbio.GetAssemblyCopyright(), ConsoleColor.White, ConsoleColor.DarkBlue);
            ConsoleEx.WriteMenu(-1, 4, "Q)uit");

            while (true)
            {
                if (mcp9808_TemperatureSensor.Begin())
                    return true;
                else
                    ConsoleEx.Write(0, 2, string.Format("[{0}]MCP9808 I2C Temperature sensor not connected to Nusbio", DateTime.Now), ConsoleColor.Cyan);

                TimePeriod.Sleep(2*1000);
                if (Console.KeyAvailable)
                {
                    var k = Console.ReadKey(true).Key;
                    if (k == ConsoleKey.Q)
                        return false;
                }
            }
        }
Esempio n. 2
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();
        }
Esempio n. 3
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();
        }