示例#1
0
        private UltrasonicReadEventArgs RetrieveSensorData()
        {
            try
            {
                // Send trigger pulse
                _triggerPin.Write(GpioPinValue.Low);
                Pi.Timing.SleepMicroseconds(2);
                _triggerPin.Write(GpioPinValue.High);
                Pi.Timing.SleepMicroseconds(12);
                _triggerPin.Write(GpioPinValue.Low);

                if (!_echoPin.WaitForValue(GpioPinValue.High, 50))
                {
                    throw new TimeoutException();
                }

                _measurementTimer.Start();
                if (!_echoPin.WaitForValue(GpioPinValue.Low, 50))
                {
                    throw new TimeoutException();
                }

                _measurementTimer.Stop();
                var elapsedTime = _measurementTimer.ElapsedMicroseconds;
                _measurementTimer.Reset();

                var distance = elapsedTime / 58.0;
                if (elapsedTime > NoObstaclePulseMicroseconds)
                {
                    distance = NoObstacleDistance;
                }

                return(new UltrasonicReadEventArgs(distance));
            }
            catch
            {
                return(UltrasonicReadEventArgs.CreateInvalidReading());
            }
        }
示例#2
0
        /// <summary>
        /// Retrieves the sensor data.
        /// </summary>
        /// <returns>The event arguments that will be read from the sensor.</returns>
        public DHT11Data RetrieveSensorData()
        {
            // Prepare buffer to store measure and checksum
            var data = new byte[5];

            // Start to communicate with sensor
            // Inform sensor that must finish last execution and put it's state in idle
            _dataPin.PinMode = GpioPinDriveMode.Output;

            // Send request to transmission from board to sensor
            _dataPin.Write(GpioPinValue.Low);
            Pi.Timing.SleepMicroseconds(PullDownMicroseconds);
            _dataPin.Write(GpioPinValue.High);

            // Wait for sensor response
            _dataPin.PinMode = GpioPinDriveMode.Input;

            try
            {
                // Read acknowledgement from sensor
                _dataPin.WaitForValue(GpioPinValue.Low, 50);

                _dataPin.WaitForValue(GpioPinValue.High, 50);

                // Begins data transmission
                _dataPin.WaitForValue(GpioPinValue.Low, 50);


                // Read 40 bits to acquire:
                //   16 bit -> Humidity
                //   16 bit -> Temperature
                //   8 bit -> Checksum
                var stopwatch = new HighResolutionTimer();

                for (var i = 0; i < 40; i++)
                {
                    stopwatch.Reset();
                    _dataPin.WaitForValue(GpioPinValue.High, 50);

                    stopwatch.Start();
                    _dataPin.WaitForValue(GpioPinValue.Low, 50);

                    stopwatch.Stop();

                    data[i / 8] <<= 1;

                    // Check if signal is 1 or 0
                    if (stopwatch.ElapsedMicroseconds > BitPulseMidMicroseconds)
                    {
                        data[i / 8] |= 1;
                    }
                }

                // End transmission
                _dataPin.WaitForValue(GpioPinValue.High, 50);
                var _validData = IsDataValid(data)
                    ? new DHT11Data(_humid: ((data[0] + (data[1] * 0.1)) / 100.0), _temp: (data[2] + ((data[3] & 0x0f) * 0.1)))
                    : new DHT11Data();

                if (_validData.IsInitialized)
                {
                    _lastResult = _validData;
                }

                // Compute the checksum
                return(_lastResult);
            }
            catch
            {
                return(new DHT11Data()
                {
                    Humidity = 0.0, Temperature = 0.0
                });
            }
        }
示例#3
0
        /// <summary>
        /// Retrieves the sensor data.
        /// </summary>
        /// <returns>The event arguments that will be read from the sensor.</returns>
        private DhtReadEventArgs RetrieveSensorData()
        {
            // Prepare buffer to store measure and checksum
            var data = new byte[5];

            // Start to comunicate with sensor
            // Inform sensor that must finish last execution and put it's state in idle
            _dataPin.PinMode = GpioPinDriveMode.Output;

            // Send request to trasmission from board to sensor
            _dataPin.Write(GpioPinValue.Low);
            Pi.Timing.SleepMicroseconds(PullDownMicroseconds);
            _dataPin.Write(GpioPinValue.High);

            // Wait for sensor response
            _dataPin.PinMode = GpioPinDriveMode.Input;

            try
            {
                // Read acknowledgement from sensor
                if (!_dataPin.WaitForValue(GpioPinValue.Low, 50))
                {
                    throw new TimeoutException();
                }

                if (!_dataPin.WaitForValue(GpioPinValue.High, 50))
                {
                    throw new TimeoutException();
                }

                // Begins data transmission
                if (!_dataPin.WaitForValue(GpioPinValue.Low, 50))
                {
                    throw new TimeoutException();
                }

                // Read 40 bits to acquire:
                //   16 bit -> Humidity
                //   16 bit -> Temperature
                //   8 bit -> Checksum
                var stopwatch = new HighResolutionTimer();

                for (var i = 0; i < 40; i++)
                {
                    stopwatch.Reset();
                    if (!_dataPin.WaitForValue(GpioPinValue.High, 50))
                    {
                        throw new TimeoutException();
                    }

                    stopwatch.Start();
                    if (!_dataPin.WaitForValue(GpioPinValue.Low, 50))
                    {
                        throw new TimeoutException();
                    }

                    stopwatch.Stop();

                    data[i / 8] <<= 1;

                    // Check if signal is 1 or 0
                    if (stopwatch.ElapsedMicroseconds > BitPulseMidMicroseconds)
                    {
                        data[i / 8] |= 1;
                    }
                }

                // End transmission
                if (!_dataPin.WaitForValue(GpioPinValue.High, 50))
                {
                    throw new TimeoutException();
                }

                // Compute the checksum
                return(IsDataValid(data) ?
                       new DhtReadEventArgs(
                           humidityPercentage: DecodeHumidity(data),
                           temperatureCelsius: DecodeTemperature(data)) :
                       DhtReadEventArgs.CreateInvalidReading());
            }
            catch
            {
                return(DhtReadEventArgs.CreateInvalidReading());
            }
        }
示例#4
0
        /// <summary>
        /// 返回传感器数据.
        /// </summary>
        /// <returns>从传感器读取的事件参数.</returns>
        private DhtReadEventArgs RetrieveSensorData()
        {
            // 准备缓冲区以存储度量和校验和
            var data = new byte[5];

            // 开始与传感器通信
            // 通知传感器必须完成最后一次执行并将其状态置于空闲状态
            _dataPin.PinMode = GpioPinDriveMode.Output;

            // 发送请求以将传输从板传输到传感器
            _dataPin.Write(GpioPinValue.Low);
            Pi.Timing.SleepMicroseconds(PullDownMicroseconds);
            _dataPin.Write(GpioPinValue.High);

            // 等待传感器响应
            _dataPin.PinMode = GpioPinDriveMode.Input;

            try
            {
                // 读取传感器的确认
                if (!_dataPin.WaitForValue(GpioPinValue.Low, 50))
                {
                    throw new TimeoutException();
                }

                if (!_dataPin.WaitForValue(GpioPinValue.High, 50))
                {
                    throw new TimeoutException();
                }

                // 开始数据传输
                if (!_dataPin.WaitForValue(GpioPinValue.Low, 50))
                {
                    throw new TimeoutException();
                }

                // 读取40位以获取:
                //   16 bit -> Humidity (湿度)
                //   16 bit -> Temperature (温度)
                //   8 bit -> Checksum (校验和)
                var stopwatch = new HighResolutionTimer();

                for (var i = 0; i < 40; i++)
                {
                    stopwatch.Reset();
                    if (!_dataPin.WaitForValue(GpioPinValue.High, 50))
                    {
                        throw new TimeoutException();
                    }

                    stopwatch.Start();
                    if (!_dataPin.WaitForValue(GpioPinValue.Low, 50))
                    {
                        throw new TimeoutException();
                    }

                    stopwatch.Stop();

                    data[i / 8] <<= 1;

                    // 检查信号是1还是0
                    if (stopwatch.ElapsedMicroseconds > BitPulseMidMicroseconds)
                    {
                        data[i / 8] |= 1;
                    }
                }

                // 结束传输数据
                if (!_dataPin.WaitForValue(GpioPinValue.High, 50))
                {
                    throw new TimeoutException();
                }

                // 完成校验
                return(IsDataValid(data) ?
                       new DhtReadEventArgs(
                           humidityPercentage: DecodeHumidity(data),
                           temperatureCelsius: DecodeTemperature(data)) :
                       DhtReadEventArgs.CreateInvalidReading());
            }
            catch
            {
                return(DhtReadEventArgs.CreateInvalidReading());
            }
        }