AzureIoTHub.SendDeviceToCloudMessageAsync C# (CSharp) Method

SendDeviceToCloudMessageAsync() public static method

public static SendDeviceToCloudMessageAsync ( ) : Task
return Task
    public static async Task SendDeviceToCloudMessageAsync()
    {
        var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString, TransportType.Amqp);

#if WINDOWS_UWP
        var str = "Hello, Cloud from a UWP C# app!";
#else
        var str = "Hello, Cloud from a C# app!";
#endif
        var message = new Message(Encoding.ASCII.GetBytes(str));

        await deviceClient.SendEventAsync(message);
    }

Usage Example

        static void DoSends(CancellationToken ct, string iotHubConnectionString)
        {
            // Was cancellation already requested?
            if (ct.IsCancellationRequested == true)
            {
                Console.WriteLine("DoSends was cancelled before it got started.");
                ct.ThrowIfCancellationRequested();
            }

            long msgTime      = 1473956534640;
            var  deviceClient = DeviceClient.CreateFromConnectionString(iotHubConnectionString, TransportType.Amqp);

            while (true)
            {
                //send device to cloud telemetry
                var deviceToCloudMessage = new DeviceTelemetryMessage
                {
                    datestamp   = DateTime.UtcNow,
                    response    = "environment",
                    temperature = lastTemp,
                    humidity    = 28.7,
                    pressure    = 100912
                };

                var messageString = JsonConvert.SerializeObject(deviceToCloudMessage);
                var sendTask      = AzureIoTHub.SendDeviceToCloudMessageAsync(deviceClient, messageString);
                sendTask.Wait(ct);
                Console.WriteLine("Sent Message to Cloud: {0}", messageString);
                ct.WaitHandle.WaitOne(TimeSpan.FromSeconds(5));//wait before sending so we are not in a tight loop

                ++msgTime;
            }
        }
All Usage Examples Of AzureIoTHub::SendDeviceToCloudMessageAsync