Exemple #1
0
        private async Task ReadSensorAsync()
        {
            GalaSoft.MvvmLight.Threading.DispatcherHelper.CheckBeginInvokeOnUI(async() =>
            {
                DateTime ReadingTime = DateTime.Now;

                //Get PhotoResistor
                byte[] readBuffer  = new byte[3]; /* Buffer to hold read data*/
                byte[] writeBuffer = new byte[3] {
                    StartByte, Channel0, 0x00
                };

                SpiADC.TransferFullDuplex(writeBuffer, readBuffer); /* Read data from the ADC                           */
                Light = convertToInt(readBuffer);                   /* Convert the returned bytes into an integer value */

                //Get Temperature
                readBuffer  = new byte[3]; /* Buffer to hold read data*/
                writeBuffer = new byte[3] {
                    StartByte, Channel1, 0x00
                };

                SpiADC.TransferFullDuplex(writeBuffer, readBuffer); /* Read data from the ADC                           */
                int adcTemp = convertToInt(readBuffer);             /* Convert the returned bytes into an integer value */
                                                                    // millivolts = value * (volts in millivolts / ADC steps)
                double millivolts = adcTemp * (3300.0 / 1024.0);
                Temperature       = (millivolts - 500) / 10.0;      //given equation from sensor documentation

                if (Temperature > AlertTemperature)
                {
                    string m       = string.Format("Temperature {0} exceeds Alert Temperature {1}", Temperature, AlertTemperature);
                    Alert alertMsg = new Alert {
                        AlertMsg = m
                    };

                    //turn on alert LED, only way to clear it is IoT Central Command)
                    ledAlertValue = GpioPinValue.High;
                    ledAlert.Write(ledAlertValue);
                    Alert = redBrush;
                }

                Reading reading = new Reading
                {
                    ReadingDateTime = ReadingTime,
                    Temperature     = Temperature,
                    Light           = Light,
                    Control         = ledControlValue == GpioPinValue.High ? true : false
                };

                //limit the amount of data we are charting
                while (Readings.Count > 60)
                {
                    Readings.RemoveAt(0);
                }

                Readings.Add(reading);
            });
        }
Exemple #2
0
        private void _sensor_ReadingComplete(object sender, ReadingArgs e)
        {
            DispatcherHelper.CheckBeginInvokeOnUI(() =>
            {
                ReadingDateTime = e.ReadingTime;

                PM1_0Concentration_CF1  = _sensor.PM1_0Concentration_CF1;
                PM2_5Concentration_CF1  = _sensor.PM2_5Concentration_CF1;
                PM10_0Concentration_CF1 = _sensor.PM10_0Concentration_CF1;

                PM1_0Concentration_amb  = _sensor.PM1_0Concentration_amb;
                PM2_5Concentration_amb  = _sensor.PM2_5Concentration_amb;
                PM10_0Concentration_amb = _sensor.PM10_0Concentration_amb;

                PM0_3Count  = _sensor.PM0_3Count;
                PM0_5Count  = _sensor.PM0_5Count;
                PM1_0Count  = _sensor.PM1_0Count;
                PM5_0Count  = _sensor.PM5_0Count;
                PM10_0Count = _sensor.PM10_0Count;

                ProductVersion = _sensor.ProductVersion;
                ErrorCodes     = _sensor.StatusCodes;

                while (Readings.Count > GraphReadings)
                {
                    Readings.RemoveAt(0);
                }

                Reading reading = new Reading
                {
                    PM1_0Concentration  = PM10_0Concentration_CF1,
                    PM2_5Concentration  = PM2_5Concentration_CF1,
                    PM10_0Concentration = PM10_0Concentration_CF1,
                    ReadingDateTime     = ReadingDateTime
                };

                Readings.Add(reading);

                while (CountsLow.Count > GraphReadings)
                {
                    CountsLow.RemoveAt(0);
                }

                CountsLow countsLow = new CountsLow
                {
                    PM2_5Count      = PM2_5Count,
                    PM5_0Count      = PM5_0Count,
                    PM10_0Count     = PM10_0Count,
                    ReadingDateTime = ReadingDateTime
                };

                CountsLow.Add(countsLow);

                while (CountsHigh.Count > GraphReadings)
                {
                    CountsHigh.RemoveAt(0);
                }

                CountsHigh countsHigh = new CountsHigh
                {
                    PM1_0Count      = PM1_0Count,
                    PM0_5Count      = PM0_5Count,
                    PM0_3Count      = PM0_3Count,
                    ReadingDateTime = ReadingDateTime
                };

                CountsHigh.Add(countsHigh);
            });
        }