public static async Task SendDeviceToCloudMessageAsync(string deviceID, string mesage) { var deviceClient = DeviceClient.CreateFromConnectionString(AzureIoTHub.GetDeviceConnectionString(deviceID), TransportType.Amqp); var str = mesage; var message = new Message(Encoding.ASCII.GetBytes(str)); await deviceClient.SendEventAsync(message); }
public void InitFileUpload() { Console.WriteLine("Simulated device to upload File to IoT Hub\n"); CreateFile(); try { _deviceClient = DeviceClient.CreateFromConnectionString(AzureIoTHub.GetDeviceConnectionString(), TransportType.Mqtt); SendDeviceDataToBlob(); } catch (Exception) { // } Console.WriteLine("Add IoT hub File upload Notification"); Console.ReadLine(); }
public async Task <string> ReceiveCloudToDeviceMessageAsync() { var deviceClient = DeviceClient.CreateFromConnectionString(AzureIoTHub.GetDeviceConnectionString(_deviceID), TransportType.Amqp); while (true) { var receivedMessage = await deviceClient.ReceiveAsync(); if (receivedMessage != null) { var messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes()); await deviceClient.CompleteAsync(receivedMessage); processCommand(messageData); } await Task.Delay(TimeSpan.FromSeconds(1)); } }
/// <summary> /// Send Device to Cloud Message /// </summary> /// <param name="deviceId"></param> /// <param name="transportType"></param> private static void SendDeviceMessages(string deviceId, TransportType transportType) { var deviceClient = DeviceClient.CreateFromConnectionString(AzureIoTHub.GetDeviceConnectionString(), transportType); new DeviceSimulator(deviceClient).SendDeviceToCloudMessagesAsync(deviceId); }