Beispiel #1
0
        public DhtData GetData()
        {
            // pull pin down for 18ms
            pin.Write(false);
            Timer.Sleep(18m);

            // pull pin up for 40µs
            pin.Write(true);
            Timer.Sleep(40m / 1000m);

            pin.Wait(true, 40m / 1000m);

            // Wait for pin to be down (at most 80µs)
            pin.Wait(false, 80m / 1000m);

            // Wait for pin to be up (at most 80µs)
            pin.Wait(true, 80m / 1000m);

            var data = new byte[] { 0, 0, 0, 0, 0 };

            for (var i = 0; i < 5; i++)
            {
                for (var j = 0; j < 8; j++)
                {
                    pin.Wait(false, 50m / 1000m);

                    var start = DateTime.UtcNow;
                    pin.Wait(true, 100m / 1000m);

                    //  bit "0" has 26-28us high-voltage length, bit "1" has 70us high-voltage length
                    var value = (DateTime.UtcNow - start).TotalMilliseconds > 40.0 / 1000 ? 1 : 0;

                    data[i] = (byte)(data[i] << 1 + value);
                }
            }

            Console.WriteLine(
                "{0:X2} {1:X2} {2:X2} {3:X2} {4:X2}",
                data[0],
                data[1],
                data[2],
                data[3],
                data[4]);

            var checkSum = data[0] + data[1] + data[2] + data[3];

            if ((checkSum & 0xff) != data[4])
            {
                throw new InvalidOperationException("Checksum is not valid");
            }

            pin.Write(true);

            return(new DhtData
            {
                Humidity = (data[0] << 8 + data[1]) / 10.0m,
                Temperature = ((data[2] & 0x80) == 0 ? 1 : -1) * ((data[2] & 0x7F) << 8 + data[3]) / 10.0m
            });
        }
        private DhtData TryGetData()
        {
            // Prepare buffer
            var data = new byte[5];

            for (var i = 0; i < 5; i++)
            {
                data[i] = 0;
            }

            var remainingSamplingInterval = SamplingInterval - (DateTime.UtcNow - previousRead);

            if (remainingSamplingInterval > TimeSpan.Zero)
            {
                HighResolutionTimer.Sleep((int)remainingSamplingInterval.TotalMilliseconds);
            }

            // Prepare for reading
            try
            {
                // Measure required by host : pull down then pull up
                pin.Write(false);
                HighResolutionTimer.Sleep((decimal)WakeupInterval.TotalMilliseconds);
                pin.Write(true);

                // Read acknowledgement from DHT
                pin.Wait(true, timeout);
                pin.Wait(false, timeout);

                // Read 40 bits output, or time-out
                var cnt = 7;
                var idx = 0;
                for (var i = 0; i < 40; i++)
                {
                    pin.Wait(true, timeout);
                    var start = DateTime.UtcNow;
                    pin.Wait(false, timeout);

                    // Determine whether bit is "1" or "0"
                    if (DateTime.UtcNow - start > bitSetUptime)
                    {
                        data[idx] |= (byte)(1 << cnt);
                    }

                    if (cnt == 0)
                    {
                        idx++;      // next byte
                        cnt = 7;    // restart at MSB
                    }
                    else
                    {
                        cnt--;
                    }
                }
            }
            finally
            {
                // Prepare for next reading
                previousRead = DateTime.UtcNow;
                pin.Write(true);
            }

            var checkSum = data[0] + data[1] + data[2] + data[3];

            if ((checkSum & 0xff) != data[4])
            {
                throw new InvalidChecksumException("Invalid checksum on DHT data", data[4], (checkSum & 0xff));
            }

            var sign = 1;

            if ((data[2] & 0x80) != 0) // negative temperature
            {
                data[2] = (byte)(data[2] & 0x7F);
                sign    = -1;
            }

            var humidity    = (data[0] << 8) + data[1];
            var temperature = sign * ((data[2] << 8) + data[3]);

            return(GetDhtData(temperature, humidity));
        }