public async Task <string> GetSensorData() { HumiditySensor hum = new HumiditySensor(); IRTemperatureSensor temp = new IRTemperatureSensor(); //hum.SensorValueChanged += SensorValueChanged; //temp.SensorValueChanged += SensorValueChanged; await hum.Initialize(); await hum.EnableSensor(); //await hum.EnableNotifications(); await temp.Initialize(); await temp.EnableSensor(); //await temp.EnableNotifications(); hum.SensorValueChanged += SensorValueChanged; temp.SensorValueChanged += SensorValueChanged; await hum.EnableNotifications(); await temp.EnableNotifications(); return("done"); }
private async void btnReadData_Click(object sender, RoutedEventArgs e) { byte[] tempValue = await tempSen.ReadValue(); double ambientTemp = IRTemperatureSensor.CalculateAmbientTemperature(tempValue, TemperatureScale.Celsius); double targetTemp = IRTemperatureSensor.CalculateTargetTemperature(tempValue, ambientTemp, TemperatureScale.Celsius); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { tbTemperature.Text = ambientTemp.ToString("0.00"); tbTargetTemperature.Text = targetTemp.ToString("0.00"); }); byte[] accValue = await acc.ReadValue(); double[] accAxis = Accelerometer.CalculateCoordinates(accValue, 1 / 64.0); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { tbAccelerometer.Text = "X: " + accAxis[0].ToString("0.00") + " Y: " + accAxis[1].ToString("0.00") + " Z: " + accAxis[2].ToString("0.00"); }); byte[] humValue = await hum.ReadValue(); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { tbHumidity.Text = HumiditySensor.CalculateHumidityInPercent(humValue).ToString("0.00") + "%"; }); }
async void SensorValueChanged(object sender, X2CodingLab.SensorTag.SensorValueChangedEventArgs e) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { switch (e.Origin) { case SensorName.Accelerometer: double[] accValues = Accelerometer.CalculateCoordinates(e.RawData, 1 / 64.0); tbAccelerometer.Text = "X: " + accValues[0].ToString("0.00") + " Y: " + accValues[1].ToString("0.00") + " Z: " + accValues[2].ToString("0.00"); break; case SensorName.Gyroscope: float[] axisValues = Gyroscope.CalculateAxisValue(e.RawData, GyroscopeAxis.XYZ); tbGyroscope.Text = "X: " + axisValues[0].ToString("0.00") + " Y: " + axisValues[1].ToString("0.00") + " Z: " + axisValues[2].ToString("0.00"); break; case SensorName.HumiditySensor: tbHumidity.Text = HumiditySensor.CalculateHumidityInPercent(e.RawData).ToString("0.00") + "%"; break; case SensorName.Magnetometer: float[] magnetValues = Magnetometer.CalculateCoordinates(e.RawData); tbMagnetometer.Text = "X: " + magnetValues[0].ToString("0.00") + " Y: " + magnetValues[1].ToString("0.00") + " Z: " + magnetValues[2].ToString("0.00"); break; case SensorName.PressureSensor: try { tbPressure.Text = (PressureSensor.CalculatePressure(e.RawData, ps.CalibrationData) / 100).ToString("0.00"); } catch (NullReferenceException) { // in case another(!) setup is executed, so ps is null } break; case SensorName.SimpleKeyService: if (SimpleKeyService.LeftKeyHit(e.RawData)) { tbLeftKey.Text = "hit!"; await Task.Delay(200); tbLeftKey.Text = ""; } else if (SimpleKeyService.RightKeyHit(e.RawData)) { tbRightKey.Text = "hit!"; await Task.Delay(200); tbRightKey.Text = ""; } break; case SensorName.TemperatureSensor: double ambient = IRTemperatureSensor.CalculateAmbientTemperature(e.RawData, TemperatureScale.Celsius); double target = IRTemperatureSensor.CalculateTargetTemperature(e.RawData, ambient, TemperatureScale.Celsius); tbTemperature.Text = ambient.ToString("0.00"); tbTargetTemperature.Text = target.ToString("0.00"); break; } }); }
public async Task <string> GetSensorDataNew() { byte[] x = await hum.ReadValue(); SensorValues.Humidity = HumiditySensor.CalculateHumidityInPercent(x).ToString("0.00");; x = await temp.ReadValue(); SensorValues.AmbientTemperature = IRTemperatureSensor.CalculateAmbientTemperature(x, TemperatureScale.Farenheit); SensorValues.TargetTemperature = IRTemperatureSensor.CalculateTargetTemperature(x, TemperatureScale.Farenheit); return("done"); }
private async void InitializeDevice() { Exception exc = null; try { tempSen = new IRTemperatureSensor(); await tempSen.Initialize(); await tempSen.EnableSensor(); await tempSen.EnableNotifications(); tempSen.SensorValueChanged += SensorValueChanged; humidSen = new HumiditySensor(); await humidSen.Initialize(); await humidSen.EnableSensor(); await humidSen.EnableNotifications(); humidSen.SensorValueChanged += SensorValueChanged; //await new MessageDialog("Device Connected").ShowAsync(); //ReadDeviceData(); ReadData.IsEnabled = true; ConnectIoTHub.IsEnabled = true; } catch (Exception ex) { exc = ex; await new MessageDialog(exc.Message).ShowAsync(); } if (exc != null) { await new MessageDialog(exc.Message).ShowAsync(); } }
private async void ReadDeviceData() { Sensor S = new Sensor(); // deviceClient = DeviceClient.Create(iotHubUri, new DeviceAuthenticationWithRegistrySymmetricKey("SensorTag", deviceKey)); Exception exc = null; try { byte[] tempValue = await tempSen.ReadValue(); double ambientTemp = IRTemperatureSensor.CalculateAmbientTemperature(tempValue, TemperatureScale.Farenheit); double targetTemp = IRTemperatureSensor.CalculateTargetTemperature(tempValue, ambientTemp, TemperatureScale.Farenheit); byte[] tempValue1 = await humidSen.ReadValue(); string humidity = HumiditySensor.CalculateHumidityInPercent(tempValue1).ToString("0.00") + "%"; humidityRes.Text = humidity; humidityBlock.Visibility = Visibility.Visible; humidityRes.Visibility = Visibility.Visible; await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { AmbientRes.Text = ambientTemp.ToString("0.00"); TargetRes.Text = targetTemp.ToString("0.00"); AmbientRes.Visibility = Visibility.Visible; TargetRes.Visibility = Visibility.Visible; temp1_Copy1.Visibility = Visibility.Visible; temp2_Copy.Visibility = Visibility.Visible; }); } catch (Exception ex) { exc = ex; await new MessageDialog(exc.Message).ShowAsync(); } }
private async void SendIoT_click(object sender, RoutedEventArgs e) { if (IoTHubUri.Text == "" && IoTDeviceKey.Text == "") { await new MessageDialog("Please Enter Iothub URI and Device key").ShowAsync(); } else { iotHubUri = IoTHubUri.Text; deviceKey = IoTDeviceKey.Text; StopIoTHub.IsEnabled = true; IoTHubSentMsgText.Visibility = Visibility; Sensor S = new Sensor(); Exception exc = null; isIoTHubEnabled = true; try { deviceClient = DeviceClient.Create(iotHubUri, new DeviceAuthenticationWithRegistrySymmetricKey("SensorTag", deviceKey)); while (true) { if (isIoTHubEnabled == false) { break; } byte[] tempValue = await tempSen.ReadValue(); double ambientTemp = IRTemperatureSensor.CalculateAmbientTemperature(tempValue, TemperatureScale.Farenheit); double targetTemp = IRTemperatureSensor.CalculateTargetTemperature(tempValue, ambientTemp, TemperatureScale.Farenheit); byte[] tempValue1 = await humidSen.ReadValue(); double humidity = HumiditySensor.CalculateHumidityInPercent(tempValue1); /// double targetTemp = IRTemperatureSensor.CalculateTargetTemperature(tempValue, ambientTemp, TemperatureScale.Celsius); humidityRes.Text = humidity.ToString("0.00") + "%"; humidityBlock.Visibility = Visibility.Visible; await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { AmbientRes.Text = ambientTemp.ToString("0.00"); TargetRes.Text = targetTemp.ToString("0.00"); temp1_Copy1.Visibility = Visibility.Visible; temp2_Copy.Visibility = Visibility.Visible; }); var telemetryDataPoint = S.Generate(modelNumber.Text, ambientTemp, humidity, city); var messageString = JsonConvert.SerializeObject(telemetryDataPoint); var message = new Microsoft.Azure.Devices.Client.Message(Encoding.ASCII.GetBytes(messageString)); await deviceClient.SendEventAsync(message); IoTHubSentMsgText.Text = messageString; // LinearGraph.x // List<FinancialStuff> financialStuffList = new List<FinancialStuff>(); // financialStuffList.Add(new FinancialStuff() { Name = "Sensor", Amount = ambientTemp}); // (LineChart.Series[0] as LineSeries).ItemsSource = financialStuffList; // await deviceClient.SendEventAsync(message); //Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString); await Task.Delay(TimeSpan.FromSeconds(2)); } } catch (Exception ex) { exc = ex; await new MessageDialog(exc.Message).ShowAsync(); } } }
async void SensorValueChanged(object sender, X2CodingLab.SensorTag.SensorValueChangedEventArgs e) { switch (e.Origin) { case SensorName.Accelerometer: double[] accValues = Accelerometer.CalculateCoordinates(e.RawData, 1 / 64.0); //tbAccelerometer.Text = "X: " + accValues[0].ToString("0.00") + " Y: " + accValues[1].ToString("0.00") + " Z: " + accValues[2].ToString("0.00"); break; case SensorName.Gyroscope: float[] axisValues = Gyroscope.CalculateAxisValue(e.RawData, GyroscopeAxis.XYZ); //tbGyroscope.Text = "X: " + axisValues[0].ToString("0.00") + " Y: " + axisValues[1].ToString("0.00") + " Z: " + axisValues[2].ToString("0.00"); break; case SensorName.HumiditySensor: humidity = HumiditySensor.CalculateHumidityInPercent(e.RawData).ToString("0.00"); SensorValues.Humidity = humidity; //tbHumidity.Text = humidity + "%"; break; case SensorName.TemperatureSensor: double ambient = IRTemperatureSensor.CalculateAmbientTemperature(e.RawData, TemperatureScale.Farenheit); double target = IRTemperatureSensor.CalculateTargetTemperature(e.RawData, ambient, TemperatureScale.Farenheit); SensorValues.AmbientTemperature = ambient; SensorValues.TargetTemperature = target; //temp = ambient; break; } try { //SendDataToEventhub.serviceNamespace = "tisensordemosh"; //SendDataToEventhub.hubName = "tisensordemoeh"; //SendDataToEventhub.sharedAccessPolicyName = "all"; //SendDataToEventhub.sharedAccessKey = "mcxBEocF6f0KHQGhP2MtT7A44tUC+zhqyDcetP/Jt0o="; //SendDataToEventhub.deviceName = "sudhesh"; string body = ""; body = "{ \"from\":\"" + SendDataToEventhub.deviceName + "\""; body += ", \"dspl\":\"" + SendDataToEventhub.deviceName + "\""; body += ", \"time\":\"" + DateTime.UtcNow.ToString("O") + "\""; body += ", \"Subject\":\"wthr\""; if (!String.IsNullOrEmpty(SensorValues.Humidity)) { body += ", \"hmdt\":" + SensorValues.Humidity; } body += ", \"temp\":" + Math.Round(SensorValues.AmbientTemperature, 3); body += "}"; bool code = await SendDataToEventhub.SendMessage(body); SendDataToEventhub.status = body; //txtStatus.Text = code.ToString(); //this.Cursor = Cursors.Default; } catch (Exception ex) { //Debug.WriteLine(ex.ToString()); SendDataToEventhub.status = "error in sending data to eventhub"; } }
private async void SetupSensors(bool serviceAsParameter) { pbar.Visibility = Windows.UI.Xaml.Visibility.Visible; ClearSensors(); btnSetup.IsEnabled = false; btnSetupParam.IsEnabled = false; spTestButtons.Visibility = Windows.UI.Xaml.Visibility.Collapsed; Exception ex = null; try { if (serviceAsParameter) { acc = new Accelerometer(); acc.SensorValueChanged += SensorValueChanged; await acc.Initialize((await GattUtils.GetDevicesOfService(acc.SensorServiceUuid))[0]); await acc.EnableSensor(); gyro = new Gyroscope(); gyro.SensorValueChanged += SensorValueChanged; await gyro.Initialize((await GattUtils.GetDevicesOfService(gyro.SensorServiceUuid))[0]); await gyro.EnableSensor(); hum = new HumiditySensor(); hum.SensorValueChanged += SensorValueChanged; await hum.Initialize((await GattUtils.GetDevicesOfService(hum.SensorServiceUuid))[0]); await hum.EnableSensor(); ks = new SimpleKeyService(); ks.SensorValueChanged += SensorValueChanged; await ks.Initialize((await GattUtils.GetDevicesOfService(ks.SensorServiceUuid))[0]); await ks.EnableSensor(); mg = new Magnetometer(); mg.SensorValueChanged += SensorValueChanged; await mg.Initialize((await GattUtils.GetDevicesOfService(mg.SensorServiceUuid))[0]); await mg.EnableSensor(); ps = new PressureSensor(); ps.SensorValueChanged += SensorValueChanged; await ps.Initialize((await GattUtils.GetDevicesOfService(ps.SensorServiceUuid))[0]); await ps.EnableSensor(); tempSen = new IRTemperatureSensor(); tempSen.SensorValueChanged += SensorValueChanged; await tempSen.Initialize((await GattUtils.GetDevicesOfService(tempSen.SensorServiceUuid))[0]); await tempSen.EnableSensor(); spTestButtons.Visibility = Windows.UI.Xaml.Visibility.Visible; } else { acc = new Accelerometer(); acc.SensorValueChanged += SensorValueChanged; await acc.Initialize(); await acc.EnableSensor(); gyro = new Gyroscope(); gyro.SensorValueChanged += SensorValueChanged; await gyro.Initialize(); await gyro.EnableSensor(); hum = new HumiditySensor(); hum.SensorValueChanged += SensorValueChanged; await hum.Initialize(); await hum.EnableSensor(); ks = new SimpleKeyService(); ks.SensorValueChanged += SensorValueChanged; await ks.Initialize(); await ks.EnableSensor(); mg = new Magnetometer(); mg.SensorValueChanged += SensorValueChanged; await mg.Initialize(); await mg.EnableSensor(); ps = new PressureSensor(); ps.SensorValueChanged += SensorValueChanged; await ps.Initialize(); await ps.EnableSensor(); tempSen = new IRTemperatureSensor(); tempSen.SensorValueChanged += SensorValueChanged; await tempSen.Initialize(); await tempSen.EnableSensor(); spTestButtons.Visibility = Windows.UI.Xaml.Visibility.Visible; } } catch (Exception exc) { ex = exc; } if (ex != null) { await new MessageDialog(ex.Message).ShowAsync(); } pbar.Visibility = Windows.UI.Xaml.Visibility.Collapsed; btnSetup.IsEnabled = true; btnSetupParam.IsEnabled = true; }