示例#1
0
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            // Connect the Gas Sensor MQ2 to analog port 0
            IGasSensorMQ2 sensor = DeviceFactory.Build.GasSensorMQ2(Pin.AnalogPin0);

            // Loop endlessly
            while (true)
            {
                try
                {
                    // Check the value of the button, turn it into a string.
                    string sensorvalue = sensor.SensorValue().ToString();
                    System.Diagnostics.Debug.WriteLine("Gas Sensor value is " + sensorvalue);
                }
                catch (Exception ex)
                {
                    // NOTE: There are frequent exceptions of the following:
                    // WinRT information: Unexpected number of bytes was transferred. Expected: '. Actual: '.
                    // This appears to be caused by the rapid frequency of writes to the GPIO
                    // These are being swallowed here/

                    // If you want to see the exceptions uncomment the following:
                    // System.Diagnostics.Debug.WriteLine(ex.ToString());
                }
            }
        }
        //Read gas value from Gas Sensor
        private void ReadGasValue()
        {
            int tempGasValue = _gasSensor.SensorValue();

            if (tempGasValue >= 0 && tempGasValue <= 1023)
            {
                _gasValue = tempGasValue * 100 / 1023;
            }
            else
            {
                Debug.WriteLine("WARNING: Gas value ignored, out of range: " + tempGasValue);
            }
        }
        public GroveMessage GetSensorValue()
        {
            GroveMessage message = new GroveMessage();

            try
            {
                temphumiSensor.Measure();
                message.Temp      = temphumiSensor.TemperatureInCelsius;
                message.Hum       = temphumiSensor.Humidity;
                message.Sound     = soundSensor.SensorValue();
                message.Light     = lightSensor.SensorValue();
                message.GasSO     = gasSensor.SensorValue();
                message.PIR       = pirMotion.IsPeopleDetected();
                message.Timestamp = DateTime.Now.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(message);
        }