コード例 #1
0
        private async Task ReadData()
        {
            string status = "";

            double avgTemp   = 0;
            double avgHum    = 0;
            double avgPres   = 0;
            int    tempCount = 0;
            int    humCount  = 0;
            int    presCount = 0;

            Log.Info("ReadData");
            // Read and format Sensor data
            try
            {
                if (bmp180 != null)
                {
                    var sensorData = await bmp180.GetSensorDataAsync(Bmp180AccuracyMode.UltraHighResolution);

                    detailData.Bmp180Temperature = sensorData.Temperature;
                    detailData.Bmp180Pressure    = sensorData.Pressure;

                    avgTemp += sensorData.Temperature;
                    ++tempCount;

                    avgPres += sensorData.Pressure;
                    ++presCount;
                    Log.Debug($"BMP180: {detailData.Bmp180Temperature},N/A,{detailData.Bmp180Pressure}");
                }
            }
            catch (Exception ex)
            {
                status = "Bmp180 Error: " + ex.Message;
                Log.Error("Bmp180 read Error", ex);
                detailData.Bmp180Temperature = double.NaN;
                detailData.Bmp180Pressure    = double.NaN;
            }


            try
            {
                if (bme280 != null)
                {
                    if (bme280ForceMode)
                    {
                        await bme280.TakeForcedMeasurement();
                    }

                    // Read Temperature
                    detailData.Bme280Temperature = await bme280.ReadTemperature();

                    avgTemp += detailData.Bme280Temperature;
                    ++tempCount;
                    // Read Humidity
                    detailData.Bme280Humidity = await bme280.ReadHumidity();

                    avgHum += detailData.Bme280Humidity;
                    ++humCount;

                    // Read Barometric Pressure
                    detailData.Bme280Pressure = await bme280.ReadPressure() / 100.0;

                    avgPres += detailData.Bme280Pressure;
                    ++presCount;
                    Log.Debug($"BME280: {detailData.Bme280Temperature},{detailData.Bme280Humidity},{detailData.Bme280Pressure}");
                }
            }
            catch (Exception ex)
            {
                status = "Bme280 Error: " + ex.Message;
                Log.Error("Bme280 read Error", ex);
                detailData.Bme280Temperature = double.NaN;
                detailData.Bme280Humidity    = double.NaN;
                detailData.Bme280Pressure    = double.NaN;
            }


            try
            {
                if (dhtSensor != null)
                {
                    DhtReading reading = new DhtReading();
                    reading = await dhtSensor.GetReadingAsync();

                    if (reading.IsValid)
                    {
                        // ***
                        // *** Get the values from the reading.
                        // ***
                        detailData.DhtTemperature = reading.Temperature;
                        detailData.DhtHumidity    = reading.Humidity;

                        avgTemp += reading.Temperature;
                        ++tempCount;
                        avgHum += reading.Humidity;
                        ++humCount;
                        Log.Debug($"DHT22: {detailData.DhtTemperature},{detailData.DhtHumidity},N/A,");
                    }
                    else
                    {
                        Log.Debug($"DHT22: invalid reading");
                    }
                }
            }
            catch (Exception ex)
            {
                status = "DHT22 Error: " + ex.Message;
                Log.Error("DHT22 read Error", ex);
                detailData.DhtTemperature = double.NaN;
                detailData.DhtHumidity    = double.NaN;
            }


            //calculate average
            avgTemp /= tempCount;
            avgHum  /= humCount;
            avgPres /= presCount;

            basicData.CurrentTemperature = avgTemp;
            basicData.CurrentHumidity    = avgHum;
            basicData.CurrentPressure    = avgPres;


            if (status.Length > 0)
            {
                OnStatusChanged(status);
            }
        }