Ejemplo n.º 1
0
        /// <summary>
        ///     Get a reading from the sensor and set the Temperature and Humidity properties.
        /// </summary>
        public void Update()
        {
            var data = _sht31d.WriteRead(new byte[] { 0x2c, 0x06 }, 6);

            Humidity    = (100 * (float)((data[3] << 8) + data[4])) / 65535;
            Temperature = ((175 * (float)((data[0] << 8) + data[1])) / 65535) - 45;
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Create a new SI7021 temperature and humidity sensor.
        /// </summary>
        /// <param name="address">Sensor address (default to 0x40).</param>
        /// <param name="speed">Speed of the I2C interface (default to 100 KHz).</param>
        /// <param name="updateInterval">Number of milliseconds between samples (0 indicates polling to be used)</param>
        /// <param name="humidityChangeNotificationThreshold">Changes in humidity greater than this value will trigger an event when updatePeriod > 0.</param>
        /// <param name="temperatureChangeNotificationThreshold">Changes in temperature greater than this value will trigger an event when updatePeriod > 0.</param>
        public SI7021(byte address = 0x40, ushort speed = 100, ushort updateInterval = MinimumPollingPeriod,
                      float humidityChangeNotificationThreshold = 0.001F, float temperatureChangeNotificationThreshold = 0.001F)
        {
            if (speed > 1000)
            {
                throw new ArgumentOutOfRangeException(nameof(speed), "Speed should be between 0 and 1000 KHz");
            }
            if (humidityChangeNotificationThreshold < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(humidityChangeNotificationThreshold), "Humidity threshold should be >= 0");
            }
            if (temperatureChangeNotificationThreshold < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(temperatureChangeNotificationThreshold), "Temperature threshold should be >= 0");
            }
            if ((updateInterval != 0) && (updateInterval < MinimumPollingPeriod))
            {
                throw new ArgumentOutOfRangeException(nameof(updateInterval), "Update period should be 0 or >= than " + MinimumPollingPeriod);
            }

            TemperatureChangeNotificationThreshold = temperatureChangeNotificationThreshold;
            HumidityChangeNotificationThreshold    = humidityChangeNotificationThreshold;
            _updateInterval = updateInterval;
            _si7021         = new I2cBus(address, speed);
            //
            //  Get the device ID.
            //
            var part1 = _si7021.WriteRead(new[]
            {
                Registers.ReadIDFirstBytePart1,
                Registers.ReadIDFirstBytePart2
            }, 8);
            var part2 = _si7021.WriteRead(new[]
            {
                Registers.ReadIDSecondBytePart1,
                Registers.ReadIDSecondBytePart2
            }, 6);

            SerialNumber = 0;
            for (var index = 0; index < 4; index++)
            {
                SerialNumber <<= 8;
                SerialNumber  += part1[index * 2];
            }
            SerialNumber <<= 8;
            SerialNumber  += part2[0];
            SerialNumber <<= 8;
            SerialNumber  += part2[1];
            SerialNumber <<= 8;
            SerialNumber  += part2[3];
            SerialNumber <<= 8;
            SerialNumber  += part2[4];
            if ((part2[0] == 0) || (part2[0] == 0xff))
            {
                SensorType = DeviceType.EngineeringSample;
            }
            else
            {
                SensorType = (DeviceType)part2[0];
            }
            //
            //  Update the firmware revision.
            //
            var firmware = _si7021.WriteRead(new[]
            {
                Registers.ReadFirmwareRevisionPart1,
                Registers.ReadFirmwareRevisionPart2
            }, 1);

            FirmwareRevision = firmware[0];
            if (updateInterval > 0)
            {
                StartUpdating();
            }
            else
            {
                Update();
            }
        }