Beispiel #1
0
        public async Task <DHT11ReadResult> Read()
        {
            pin.PinMode = GpioPinDriveMode.Output;
            await pin.WriteAsync(GpioPinValue.High);

            await Task.Delay(TimeSpan.FromMilliseconds(25));

            await pin.WriteAsync(GpioPinValue.Low);

            await Task.Delay(TimeSpan.FromMilliseconds(11));

            pin.PinMode       = GpioPinDriveMode.Input;
            pin.InputPullMode = GpioPinResistorPullMode.PullUp;

            var data = CollectInput();

            var pullUpLengths = ParseDataPullUpLengths(data);

            if (pullUpLengths.Length != 40)
            {
                return(new DHT11ReadResult
                {
                    Error = DHT11ReadErrorState.MissingData,
                });
            }

            var bits = CalculateBits(pullUpLengths);

            var result = BitsToBytes(bits);

            var checksum = CalculateChecksum(result);

            if (result[4] != checksum)
            {
                return(new DHT11ReadResult
                {
                    Error = DHT11ReadErrorState.CRC
                });
            }
            return(new DHT11ReadResult
            {
                Temperature = result[2],
                Humidity = result[0],
                Error = DHT11ReadErrorState.None
            });
        }
Beispiel #2
0
 public async Task TurnOnAsync()
 {
     await _pin.WriteAsync(true);
 }