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;
            }
        }
Example #2
0
        static void DoSends(CancellationToken ct)
        {
            // Was cancellation already requested?
            if (ct.IsCancellationRequested == true)
            {
                Console.WriteLine("DoSends was cancelled before it got started.");
                ct.ThrowIfCancellationRequested();
            }

            long msgTime   = 1473956534640;
            int  iteration = 0;

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

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

                ++msgTime;
            }
        }