protected virtual void OnDataReceived(SensorDataEventArgs e) { if (DataReceived != null) { DataReceived(this, e); } }
/** * This method records on a timer the data measured by the temperature, brightness, and soil moisture sensor, * then organizes all of the information collected. * */ private async void timerCallback(object state) { //ensures that the temperature sensor is initialized before it is measured from if (BMP280 == null) { Debug.WriteLine("BMP280 is null"); } else { //receives the value from the temperature sensor and saves //the data in the SensorDataEventArgs class, which holds //the sensor name, the data point, and the time the value was measured. //this data is then sent back to the main page and the UI is adjusted based //off of the measurement. //float currentTemperature = (float) rand.NextDouble() * 10; float currentTemperature = await BMP280.ReadTemperature(); var tempArgs = new SensorDataEventArgs() { SensorName = "Temperature", SensorValue = currentTemperature, Timestamp = DateTime.Now }; OnDataReceived(tempArgs); } //MCP3008 is an ADC and checks to see if this is initialized. //the soil moisture sensor and the photocell are on different channels of the ADC if (mcp3008 == null) { Debug.WriteLine("mcp3008 is null"); } else { //The first line reads a value from the ADC from the photo cell sensor usually between 0 and 1023. //then the second line maps this number to a voltage that represents this number int cdsReadVal = mcp3008.ReadADC(CDSADCChannel); float cdsVoltage = mcp3008.ADCToVoltage(cdsReadVal); //float currentBrightness = (float)rand.NextDouble() * 10; float currentBrightness = cdsVoltage; var brightnessArgs = new SensorDataEventArgs() { SensorName = "Brightness", SensorValue = currentBrightness, Timestamp = DateTime.Now }; OnDataReceived(brightnessArgs); //float currentSoilMoisture = (float)rand.NextDouble() * 10; float currentSoilMoisture = mcp3008.ReadADC(SoilMoistureChannel); Debug.WriteLine(currentSoilMoisture); var soilmoistureArgs = new SensorDataEventArgs() { SensorName = "SoilMoisture", SensorValue = currentSoilMoisture, Timestamp = DateTime.Now }; OnDataReceived(soilmoistureArgs); } }
/** * updates the UI when the sensors make a new reading * */ private async void SensorProvider_DataReceived(object sender, SensorDataEventArgs e) { String format = formatOfSensorValue(e.SensorValue); String nextValue = e.SensorValue + "," + DateTime.Now + Environment.NewLine; SolidColorBrush ellipseFill; switch (e.SensorName) { case "Brightness": await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { currentBrightness = e.SensorValue; float suggestionBrightness = idealBrightness - e.SensorValue; CurrentBrightnessNumber.Text = e.SensorValue.ToString(format); OurSuggestionNumberBrightness.Text = suggestionBrightness.ToString(format); ellipseFill = FigureOutFill(suggestionBrightness); BrightnessOutsideEllipse.Fill = ellipseFill; BrightnessUnitsMain.Foreground = ellipseFill; IdealBrightnessText.Foreground = ellipseFill; IdealBrightnessNumber.Foreground = ellipseFill; CurrentBrightnessNumber.Foreground = ellipseFill; CurrentBrightnessText.Foreground = ellipseFill; OurSuggestionTextBrightness.Foreground = ellipseFill; OurSuggestionNumberBrightness.Foreground = ellipseFill; App.BrightnessList.Add(nextValue); }); break; case "Temperature": await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { currentTemperature = e.SensorValue; float suggestionTemperature = idealTemperature - e.SensorValue; CurrentTemperatureNumber.Text = e.SensorValue.ToString(format); OurSuggestionNumberTemperature.Text = suggestionTemperature.ToString(format); ellipseFill = FigureOutFill(suggestionTemperature); TemperatureOutsideEllipse.Fill = ellipseFill; TemperatureUnitsMain.Foreground = ellipseFill; IdealTemperatureText.Foreground = ellipseFill; IdealTemperatureNumber.Foreground = ellipseFill; CurrentTemperatureNumber.Foreground = ellipseFill; CurrentTemperatureText.Foreground = ellipseFill; OurSuggestionTextTemperature.Foreground = ellipseFill; OurSuggestionNumberTemperature.Foreground = ellipseFill; App.TemperatureList.Add(nextValue); }); break; case "SoilMoisture": await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { currentSoilMoisture = e.SensorValue; float suggestionSoilMoisture = idealSoilMoisture - e.SensorValue; CurrentSoilMoistureNumber.Text = e.SensorValue.ToString(format); OurSuggestionNumberSoilMoisture.Text = suggestionSoilMoisture.ToString(format); ellipseFill = FigureOutFill(suggestionSoilMoisture); SoilMoistureOutsideEllipse.Fill = ellipseFill; SoilMoistureUnitsMain.Foreground = ellipseFill; IdealSoilMoistureText.Foreground = ellipseFill; IdealSoilMoistureNumber.Foreground = ellipseFill; CurrentSoilMoistureNumber.Foreground = ellipseFill; CurrentSoilMoistureText.Foreground = ellipseFill; OurSuggestionTextSoilMoisture.Foreground = ellipseFill; OurSuggestionNumberSoilMoisture.Foreground = ellipseFill; App.SoilMoistureList.Add(nextValue); }); break; } }