Пример #1
0
        public Dht11Reading Read()
        {
            Dht11Reading reading    = null;
            int          retryCount = 0;
            bool         failed;

            do
            {
                failed = false;
                try
                {
                    reading          = dht11.Sample();
                    reading.Failures = retryCount;
                }
                catch (TimeoutException tex)
                {
                    statusText = $"Timed out waiting for sample. {tex.Message}";
                    failed     = true;
                }
                catch (InvalidOperationException)
                {
                    statusText = "Checksum validation failed";
                    failed     = true;
                }
                catch
                {
                    statusText = "Could not read data";
                    failed     = true;
                }
            } while (failed && (++retryCount < 20));

            if (!failed)
            {
                statusText = $"Succeeded ({retryCount} retries)";
            }

            System.Diagnostics.Debug.WriteLine(statusText);
            return(reading);
        }
Пример #2
0
        public Dht11Reading Sample()
        {
            BitVector bits = new BitVector(40);

            var oldLatency = System.Runtime.GCSettings.LatencyMode;

            System.Runtime.GCSettings.LatencyMode = System.Runtime.GCLatencyMode.LowLatency;
            try {
                pin.SetDriveMode(GpioPinDriveMode.Output);
                // Latch low value onto pin
                pin.Write(GpioPinValue.Low);

                // Wait for at least 18 ms
                Task.Delay(sampleHoldLowMillis).Wait();
                pin.Write(GpioPinValue.High);
                Task.Delay(TimeSpan.FromTicks(200)).Wait();
                var stopwatch = Stopwatch.StartNew();
                // Set pin back to input
                pin.SetDriveMode(inputDriveMode);
                GpioPinValue previousValue = pin.Read();
                // catch the first rising edge
                for (;;)
                {
                    if (stopwatch.ElapsedMilliseconds > initialRisingEdgeTimeoutMillis)
                    {
                        throw new TimeoutException("Initial Rising Timeout Exception");
                    }

                    GpioPinValue value = pin.Read();
                    if (value != previousValue)
                    {
                        // rising edge?
                        if (value == GpioPinValue.High)
                        {
                            break;
                        }
                        previousValue = value;
                    }
                }

                long prevTime = 0;


                stopwatch.Restart();
                // capture every falling edge until all bits are received or
                // timeout occurs
                for (uint i = 0; i < (bits.Length + 1);)
                {
                    if (stopwatch.ElapsedMilliseconds > sampleTimeoutMillis)
                    {
                        throw new TimeoutException("Bits reading Timeout Exception");
                    }

                    GpioPinValue value = pin.Read();
                    if ((previousValue == GpioPinValue.High) && (value == GpioPinValue.Low))
                    {
                        // A falling edge was detected

                        // Calculate the microseconds in a fractional second
                        long now = (long)(stopwatch.Elapsed.TotalSeconds * 1000000);

                        if (i != 0)
                        {
                            var difference = now - prevTime;
                            bits[bits.Length - i] =
                                difference > oneThresholdMicroseconds;
                        }

                        prevTime = now;
                        ++i;
                    }

                    previousValue = value;
                }

                var reading = new Dht11Reading(bits.ToULongValues()[0]);
                if (!reading.IsValid)
                {
                    // checksum mismatch
                    throw new InvalidOperationException("invalid checksum");
                }

                return(reading);
            }
            finally
            {
                System.Runtime.GCSettings.LatencyMode = oldLatency;
            }
        }
Пример #3
0
        private async void refreshData(Dht11Reading data)
        {
            {
                await textBlockTemp.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    if (data.IsValid)
                    {
                        this.textBlockTemp.Text = data.Temperature + "°C";
                        this.textBlockHum.Text = data.Humidity.ToString() + "%";
                        this.reading.Text = "";
                        if (data.Temperature >= 30)
                        {
                            rectangleTempDown.Fill = redBrush;
                        }
                        else
                        {
                            rectangleTempDown.Fill = orangeBrush;
                        }
                        if (data.Humidity >= 50)
                        {
                            rectangleHumDown.Fill = redBrush;
                        }
                        else
                        {
                            rectangleHumDown.Fill = orangeBrush;
                        }
                        rectangleTempUp.Height = 2 * (100 - data.Temperature);
                        rectangleTempDown.Height = 2 * data.Temperature;
                        rectangleHumUp.Height = 2 * (100 - data.Humidity);
                        rectangleHumDown.Height = 2 * data.Humidity;

                    }
                });

            }
        }
Пример #4
0
        public Dht11Reading Sample()
        {
            BitVector bits = new BitVector(40);

            // Latch low value onto pin
            pin.Write(GpioPinValue.Low);

            // Set pin as output
            pin.SetDriveMode(GpioPinDriveMode.Output);

            // Wait for at least 18 ms
            Task.Delay(sampleHoldLowMillis).Wait();

            // Set pin back to input
            pin.SetDriveMode(inputDriveMode);

            GpioPinValue previousValue = pin.Read();

            // catch the first rising edge
            var stopwatch = Stopwatch.StartNew();
            for (;;)
            {
                if (stopwatch.ElapsedMilliseconds > initialRisingEdgeTimeoutMillis)
                {
                    throw new TimeoutException("Initial Rising Timeout Exception");
                }

                GpioPinValue value = pin.Read();
                if (value != previousValue)
                {
                    // rising edge?
                    if (value == GpioPinValue.High)
                    {
                        break;
                    }
                    previousValue = value;
                }
            }

            long prevTime = 0;

            stopwatch.Restart();
            // capture every falling edge until all bits are received or
            // timeout occurs
            for (uint i = 0; i < (bits.Length + 1);)
            {
                if (stopwatch.ElapsedMilliseconds > sampleTimeoutMillis)
                {
                    throw new TimeoutException("Bits reading Timeout Exception");
                }

                GpioPinValue value = pin.Read();
                if ((previousValue == GpioPinValue.High) && (value == GpioPinValue.Low))
                {
                    // A falling edge was detected

                    // Calculate the microseconds in a fractional second
                    long now = (long)(stopwatch.Elapsed.TotalSeconds * 1000000);

                    if (i != 0)
                    {
                        var difference = now - prevTime;
                        bits[bits.Length - i] =
                            difference > oneThresholdMicroseconds;
                    }

                    prevTime = now;
                    ++i;
                }

                previousValue = value;
            }

            var reading = new Dht11Reading(bits.ToULongValues()[0]);
            if (!reading.IsValid)
            {
                // checksum mismatch
                throw new InvalidOperationException("invalid checksum");
            }

            return reading;
        }