Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A QuizInternet of Things IoT Frameworks 3 years ago
User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
manpreet
Best Answer
3 years ago
_x000D_ Please try this .Hope it helps. Declare the constants first. private const string DeviceKey = "your secret key"; private const string DeviceId = "DeviceName"; And then create a connection string based on SymmetricKey _deviceClient = DeviceClient.Create(IotHubUri, new DeviceAuthenticationWithRegistrySymmetricKey(DeviceId, DeviceKey), TransportType.Mqtt); Sample code of Simulator: using System; using System.Text; using System.Threading.Tasks; using Microsoft.Azure.Devices.Client; using Newtonsoft.Json; namespace SimulatedDevice { class Program { private const string IotHubUri = "YourHubName.azure-devices.net"; private const string DeviceKey = "Secret Key"; private const string DeviceId = "DeviceId"; private const double MinVoltage = 40; private const double MinTemperature = 10; private const double MinHumidity = 50; private static readonly Random Rand = new Random(); private static DeviceClient _deviceClient; private static int _messageId = 1; static void Main(string[] args) { Console.WriteLine("Simulated device\n"); _deviceClient = DeviceClient.Create(IotHubUri, new DeviceAuthenticationWithRegistrySymmetricKey(DeviceId, DeviceKey), TransportType.Mqtt); _deviceClient.ProductInfo = "Simulated_Client"; SendDeviceToCloudMessagesAsync(); Console.ReadLine(); } private static async void SendDeviceToCloudMessagesAsync() { while (true) { var currentVoltage = MinVoltage + Rand.NextDouble() * 15; var currentTemperature = MinTemperature + Rand.NextDouble() * 20; var currentHumidity = MinHumidity + Rand.NextDouble() * 25; var telemetryDataPoint = new { deviceId = DeviceId, timestamp = DateTime.UtcNow, Temperature = currentTemperature, Humidity = currentHumidity, Voltage = currentVoltage, }; var messageString = JsonConvert.SerializeObject(telemetryDataPoint); var message = new Message(Encoding.ASCII.GetBytes(messageString)); await _deviceClient.SendEventAsync(message); Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString); await Task.Delay(2000); } } } }