コード例 #1
0
        private async Task <bool> InitSensors()
        {
            Log.Info("Initializing sensors");
            OnStatusChanged("Initialising sensors");
            // Initialize the BMP180 Sensor
            try
            {
                OnStatusChanged("Initialising sensors..BMP180");
                bmp180 = new Bmp180Sensor();
                await bmp180.InitializeAsync();
            }
            catch (Exception ex)
            {
                Log.Info("BMP180 Init Error", ex);
                bmp180 = null;
            }

            try
            {
                OnStatusChanged("Initialising sensors..BME280");
                bme280 = new BME280Sensor();
                // Initialize BME280 Sensor
                await bme280.Initialize(0x76);

                if (bme280ForceMode)
                {
                    await bme280.SetSampling(SensorMode.MODE_FORCED,
                                             SensorSampling.SAMPLING_X1, // temperature
                                             SensorSampling.SAMPLING_X1, // pressure
                                             SensorSampling.SAMPLING_X1, // humidity
                                             SensorFilter.FILTER_OFF,
                                             StandbyDuration.STANDBY_MS_1000);
                }
            }
            catch (Exception ex)
            {
                Log.Info("BME280 Init Error", ex);
                bme280 = null;
            }

            try
            {
                OnStatusChanged("Initialising sensors..DHT22");
                GpioController controller = GpioController.GetDefault();
                dhtPin    = GpioController.GetDefault().OpenPin(DHT22_Pin, GpioSharingMode.Exclusive);
                dhtSensor = new Dht22(dhtPin, GpioPinDriveMode.InputPullUp);
            }
            catch (Exception ex)
            {
                Log.Info("DHT22 Init Error", ex);
                dhtSensor = null;
            }
            string status = "";

            if (bmp180 != null || bme280 != null || dhtSensor != null)
            {
                if (bmp180 != null)
                {
                    status += "BMP180";
                }
                if (bme280 != null)
                {
                    if (status.Length > 0)
                    {
                        status += ", ";
                    }
                    status += "BME280";
                }
                if (dhtSensor != null)
                {
                    if (status.Length > 0)
                    {
                        status += ", ";
                    }
                    status += "DHT22";
                }
                status = $"Found: {status} sensors";
            }
            else
            {
                status = "No sensor found";
            }
            OnStatusChanged(status);
            await Task.Delay(500);

            Log.Trace($"end");
            Log.Info(status);
            return(bmp180 != null || bme280 != null || dhtSensor != null);
        }