Example #1
0
    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);
    }
Example #2
0
        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();
        }
Example #3
0
        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);
        }