static void Main(string[] args) { if(args.Length < 2) { HowTo(); return; } // Pick the logical LimpetID UInt32 logicalId = System.Convert.ToUInt32(args[0]); TpmDevice myLimpet = new TpmDevice(logicalId); // Decode the command if (args[1].ToUpper().Equals("-H")) { Console.WriteLine("Limpet[{0}] HWDeviceID = {1}", logicalId, myLimpet.GetHardwareDeviceId()); } else if ((args[1].ToUpper().Equals("-P")) && (args.Length > 4)) { if (args.Length > 4) { myLimpet.Provision(args[2], args[3], args[4]); } else { myLimpet.Provision(args[2], args[3]); } Console.WriteLine("Limpet[{0}] provisioned.", logicalId); } else if (args[1].ToUpper().Equals("-S")) { if (args.Length > 2) { UInt32 validity = System.Convert.ToUInt32(args[2]); Console.WriteLine(myLimpet.GetSASToken(validity)); } else { Console.WriteLine(myLimpet.GetSASToken()); } } else if (args[1].ToUpper().Equals("-D")) { myLimpet.Destroy(); Console.WriteLine("Limpet[{0}] destroyed.", logicalId); } else { HowTo(); return; } }
public static async Task<string> ReceiveCloudToDeviceMessageAsync() { TpmDevice myDevice = new TpmDevice(0); // Use logical device 0 on the TPM by default string hubUri = myDevice.GetHostName(); string deviceId = myDevice.GetDeviceId(); string sasToken = myDevice.GetSASToken(); var deviceClient = DeviceClient.Create( hubUri, AuthenticationMethodFactory. CreateAuthenticationWithToken(deviceId, sasToken), TransportType.Amqp); while (true) { var receivedMessage = await deviceClient.ReceiveAsync(); if (receivedMessage != null) { var messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes()); await deviceClient.CompleteAsync(receivedMessage); return messageData; } await Task.Delay(TimeSpan.FromSeconds(1)); } }
public static async Task SendDeviceToCloudMessageAsync(string txtSend) { try { Microsoft.Devices.Tpm.TpmDevice myDevice = new Microsoft.Devices.Tpm.TpmDevice(0); // Use logical device 0 on the TPM by default string hubUri = myDevice.GetHostName(); string deviceId = myDevice.GetDeviceId(); string sasToken = myDevice.GetSASToken(); var deviceClient = DeviceClient.Create( hubUri, AuthenticationMethodFactory. CreateAuthenticationWithToken(deviceId, sasToken), TransportType.Amqp); var str = txtSend + " from " + deviceId + " " + DateTime.Now.ToString(); var message = new Message(Encoding.ASCII.GetBytes(str)); await deviceClient.SendEventAsync(message); } catch (Exception ex) { throw ex; } }
public static async Task<string> ReceiveCloudToDeviceMessageAsync() { TpmDevice myDevice = new TpmDevice(logicalDeviceID); // Use logical device 0 on the TPM string hubUri = myDevice.GetHostName(); string deviceId = myDevice.GetDeviceId(); string sasToken = myDevice.GetSASToken(); var deviceClient = DeviceClient.Create( hubUri, AuthenticationMethodFactory. CreateAuthenticationWithToken(deviceId, sasToken), TransportType.Amqp); while (true) { var receivedMessage = await deviceClient.ReceiveAsync(); if (receivedMessage != null) { var messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes()); await deviceClient.CompleteAsync(receivedMessage); return messageData; } // Note: In this sample, the polling interval is set to // 10 seconds to enable you to see messages as they are sent. // To enable an IoT solution to scale, you should extend this // interval. For example, to scale to 1 million devices, set // the polling interval to 25 minutes. // For further information, see // https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging await Task.Delay(TimeSpan.FromSeconds(10)); } }
// // This sample assumes the device has been connected to Azure with the IoT Dashboard // // Refer to http://aka.ms/azure-iot-hub-vs-cs-wiki for more information on Connected Service for Azure IoT Hub public static async Task SendDeviceToCloudMessageAsync(string payload) { TpmDevice myDevice = new TpmDevice(0); // Use logical device 0 on the TPM string hubUri = myDevice.GetHostName(); string deviceId = myDevice.GetDeviceId(); string sasToken = myDevice.GetSASToken(); var deviceClient = DeviceClient.Create( hubUri, AuthenticationMethodFactory. CreateAuthenticationWithToken(deviceId, sasToken), TransportType.Amqp); var message = new Message(Encoding.ASCII.GetBytes(payload)); await deviceClient.SendEventAsync(message); }
public static async Task <string> ReceiveCloudToDeviceMessageAsync() { string retval = string.Empty; try { Microsoft.Devices.Tpm.TpmDevice myDevice = new Microsoft.Devices.Tpm.TpmDevice(0); // Use logical device 0 on the TPM by default string hubUri = myDevice.GetHostName(); string deviceId = myDevice.GetDeviceId(); string sasToken = myDevice.GetSASToken(); var deviceClient = DeviceClient.Create( hubUri, AuthenticationMethodFactory. CreateAuthenticationWithToken(deviceId, sasToken), TransportType.Amqp); while (true) { try { Message receivedMessage = await deviceClient.ReceiveAsync(); if (receivedMessage == null) { continue; } var messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes()); await deviceClient.CompleteAsync(receivedMessage); //return messageData; retval = messageData.ToString(); //MainPage.ReceiveMessage.Text = retval; return(retval); } catch (Exception ex) { throw; } } } catch (Exception ex) { retval = ex.Message.ToString(); Debug.WriteLine("Error in sample: {0}", ex.Message); } return(retval); }
public static async Task <string> ReceiveCloudToDeviceMessageAsyncButton() { string retval = string.Empty; Microsoft.Devices.Tpm.TpmDevice myDevice = new Microsoft.Devices.Tpm.TpmDevice(0); // Use logical device 0 on the TPM by default string hubUri = myDevice.GetHostName(); string deviceId = myDevice.GetDeviceId(); string sasToken = myDevice.GetSASToken(); var deviceClient = DeviceClient.Create( hubUri, AuthenticationMethodFactory. CreateAuthenticationWithToken(deviceId, sasToken), TransportType.Amqp); var receivedMessage = await deviceClient.ReceiveAsync(); if (receivedMessage != null) { var messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes()); await deviceClient.CompleteAsync(receivedMessage); //return messageData; retval += messageData.ToString(); } //while (true) //{ // var receivedMessage = await deviceClient.ReceiveAsync(); // if (receivedMessage != null) // { // var messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes()); // await deviceClient.CompleteAsync(receivedMessage); // return messageData; // } // await Task.Delay(TimeSpan.FromSeconds(1)); //} return(retval); }
private async void SendDeviceToCloudMessagesAsync() { Microsoft.Devices.Tpm.TpmDevice myDevice = new Microsoft.Devices.Tpm.TpmDevice(0); // Use logical device 0 on the TPM by default string hubUri = myDevice.GetHostName(); string deviceId = myDevice.GetDeviceId(); string sasToken = myDevice.GetSASToken(); double minTemperature = 20; double minHumidity = 60; Random rand = new Random(); while (true) { try { //deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt); var deviceClientsend = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Http1); double currentTemperature = minTemperature + rand.NextDouble() * 15; double currentHumidity = minHumidity + rand.NextDouble() * 20; //get a reference to SenseHat senseHat = await SenseHatFactory.GetSenseHat(); senseHat.Sensors.ImuSensor.Update(); senseHat.Sensors.HumiditySensor.Update(); senseHat.Sensors.PressureSensor.Update(); //gather data SenseHatData data = new SenseHatData(); data.Temperature = senseHat.Sensors.Temperature; data.Humidity = senseHat.Sensors.Humidity; data.Pressure = senseHat.Sensors.Pressure; //notify UI TempText.Text = data.Temperature.ToString(); TempHumidity.Text = data.Humidity.ToString(); TempPressure.Text = data.Pressure.ToString(); if (senseHat.Sensors.Acceleration.HasValue) { Acceleration al = new Acceleration(); al.X = senseHat.Sensors.Acceleration.Value.X; al.Y = senseHat.Sensors.Acceleration.Value.Y; al.Z = senseHat.Sensors.Acceleration.Value.Z; TempAcceleration.Text = " X= " + al.X.ToString() + " Y= " + al.Y.ToString() + " Z= " + al.Z.ToString(); data.Acceleration = al; } if (senseHat.Sensors.Gyro.HasValue) { Gyro gy = new Gyro(); gy.X = senseHat.Sensors.Gyro.Value.X; gy.Y = senseHat.Sensors.Gyro.Value.Y; gy.Z = senseHat.Sensors.Gyro.Value.Z; TempGyro.Text = " X= " + gy.X.ToString() + " Y= " + gy.Y.ToString() + " Z= " + gy.Z.ToString(); data.Gyro = gy; } if (senseHat.Sensors.Pose.HasValue) { Pose po = new Pose(); po.X = senseHat.Sensors.Pose.Value.X; po.Y = senseHat.Sensors.Pose.Value.Y; po.Z = senseHat.Sensors.Pose.Value.Z; TempPose.Text = " X= " + po.X.ToString() + " Y= " + po.Y.ToString() + " Z= " + po.Z.ToString(); data.po = po; } if (senseHat.Sensors.MagneticField.HasValue) { MagneticField mf = new MagneticField(); mf.X = senseHat.Sensors.MagneticField.Value.X; mf.Y = senseHat.Sensors.MagneticField.Value.Y; mf.Z = senseHat.Sensors.MagneticField.Value.Z; TempMagneticField.Text = " X= " + mf.X.ToString() + " Y= " + mf.Y.ToString() + " Z= " + mf.Z.ToString(); data.mf = mf; } data.deviceID = deviceId; //send them to the cloud //await AzureIoTHub.SendSenseHatDataToCloudAsync(data); var messageString = JsonConvert.SerializeObject(data); string levelValue; //if (rand.NextDouble() > 0.7) //{ // messageString = "This is a critical message"; // levelValue = "critical"; //} //else //{ // levelValue = "normal"; //} levelValue = "normal"; var message = new Message(Encoding.ASCII.GetBytes(messageString)); message.Properties.Add("level", levelValue); if (deviceClientsend == null) { deviceClientsend = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt); } await deviceClientsend.SendEventAsync(message); //Console.WriteLine("{0} > Sent message: {1}", DateTime.Now, messageString); //string _scrollText = "Hello Pi 3"; ////now update the Sensor HAT UI //// Create the font from the image. //MultiColorFont font = MultiColorFont.LoadFromImage( // new Uri("ms-appx:///Assets/ColorFont.png"), // " ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖÉÜabcdefghijklmnopqrstuvwxyzåäöéü0123456789.,?!\"#$%&-+*:;/\\<>()'`=", // Color.FromArgb(0xFF, 0xFF, 0x00, 0xFF)).Result; //// Get the characters to scroll. //IEnumerable<MultiColorCharacter> characters = font.GetChars(_scrollText); //// Choose a background color (or draw your own more complex background!) //Color backgroundColor = Color.FromArgb(0xFF, 0x00, 0x20, 0x00); //// Create the character renderer. //var characterRenderer = new MultiColorCharacterRenderer(); //// Create the text scroller. //var textScroller = new TextScroller<MultiColorCharacter>( // senseHat.Display, // characterRenderer, // characters); //// Clear the display. //senseHat.Display.Fill(backgroundColor); //// Draw the scroll text. //textScroller.Render(); //// Update the physical display. //senseHat.Display.Update(); connectionstatus.Text = "Connected " + DateTime.Now.ToString(); } catch (Exception ex) { Debug.WriteLine("Error in sample SendDeviceToCloudMessagesAsync: {0}", ex.Message); } await Task.Delay(30000); } }
private async void SendDeviceToCloudMessagesAsyncSimulated() { Microsoft.Devices.Tpm.TpmDevice myDevice = new Microsoft.Devices.Tpm.TpmDevice(0); // Use logical device 0 on the TPM by default string hubUri = myDevice.GetHostName(); string deviceId = myDevice.GetDeviceId(); string sasToken = myDevice.GetSASToken(); double minTemperature = 20; double minHumidity = 60; double minPressure = 30; Random rand = new Random(); //var deviceClient = DeviceClient.Create( // hubUri, // AuthenticationMethodFactory. // CreateAuthenticationWithToken(deviceId, sasToken), TransportType.Amqp); //String _deviceConnectionString = "HostName=bbdevicehub.azure-devices.net;DeviceId=device1;SharedAccessKey=xxxxxxxxxxxxxxxx="; //String _deviceConnectionString = "HostName=bbdevicehub.azure-devices.net;DeviceId=device1;SharedAccessKey=xxxxxxxxxxxxxxxxx"; while (true) { try { //deviceClient = DeviceClient.CreateFromConnectionString(_deviceConnectionString, TransportType.Mqtt); deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Http1); double currentTemperature = minTemperature + rand.NextDouble() * 15; double currentHumidity = minHumidity + rand.NextDouble() * 20; double currentPressure = minPressure + rand.NextDouble() * 5; //notify UI TempText.Text = currentTemperature.ToString(); TempHumidity.Text = currentHumidity.ToString(); TempPressure.Text = currentPressure.ToString(); var telemetryDataPoint = new { deviceId = deviceId, temperature = currentTemperature, humidity = currentHumidity, pressure = currentPressure }; var messageString = JsonConvert.SerializeObject(telemetryDataPoint); string levelValue; if (rand.NextDouble() > 0.7) { messageString = "This is a critical message"; levelValue = "critical"; } else { levelValue = "normal"; } var message = new Message(Encoding.ASCII.GetBytes(messageString)); message.Properties.Add("level", levelValue); await deviceClient.SendEventAsync(message); //Console.WriteLine("{0} > Sent message: {1}", DateTime.Now, messageString); } catch (Exception ex) { Debug.WriteLine("Error in sample: {0}", ex.Message); } await Task.Delay(20000); } }