locked
C2D message with UWP Backend App RRS feed

  • Question

  • I would like to send a command (message) from a backend UWP app to IoT Hub and then to  a ESP8266 (Arduino/ C++ code) device. I can successfully send a command to a ESP8266 device from a C# - Console backend app using the ServiceClient method. Apparently there is no equivalent method in C# UWP. Is there a way to do C2D simple messaging in a C# UWP app?

     

    Monday, February 5, 2018 9:50 PM

Answers

  • - it looks like you have a threading problem, see this.

    Thanks



    • Edited by Roman Kiss Wednesday, February 7, 2018 8:58 PM
    • Marked as answer by Marcus Davis Thursday, February 8, 2018 1:25 AM
    Wednesday, February 7, 2018 8:57 PM

All replies

  • Hello Marcus,

    You can use Connected Service for Azure IoT Hub, please refer to this blog and the introduction. The method ReceiveCloudToDeviceMessageAsync can receive C2D messages.

    And more, you can refer to this similar topic on StackOverflow:

    https://stackoverflow.com/questions/36061131/uwp-app-for-recievering-messages-from-azure-iot-hub

    Best Regards,

    Michael


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 6, 2018 2:37 AM
  • Michael,

    Thanks for the response. I am not sure the ReceiveCloudToDeviceMessageAsync helps me. What I want to do is Send a message to my ESP8266 from a UWP app. 

    I got my solution working using a Console app using the following boiler plate code snippets: 

    Am I missing something?

     Console.WriteLine("Send Cloud-to-Device message\n");
     serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
    
     Console.WriteLine("Press any key to send a C2D message.");
     Console.ReadLine();
     SendCloudToDeviceMessageAsync().Wait();
     Console.ReadLine();
    
    
    private async static Task SendCloudToDeviceMessageAsync()
     {
         var commandMessage = new Message(Encoding.ASCII.GetBytes("Cloud to device message."));
         await serviceClient.SendAsync("myFirstDevice", commandMessage);
     }

    Wednesday, February 7, 2018 1:50 AM
  • Hello Marcus,

    The code snippets you provided can also work in an UWP app. If your ESP8266 is as "myFirstDevice" that already added in IoTHub Devices, it can receive the message sending from UWP app.

    BTW, Console is unavailable for UWP app.

    Best Regards,

    Michael


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, February 7, 2018 9:59 AM
  • Hi Marcus,

    There is also another option using a service-facing endpoint of the Azure IoT Hub outside of the cloud-backend. The following picture shows this example:

    The above solution is based on the Azure HttpTrigger Function, where the http request is mapped into the AMQP message for service-facing endpoint of the Azure IoT Hub. Note, that your application is using a generic Https Post request.

    Thanks

    Roman



    • Edited by Roman Kiss Wednesday, February 7, 2018 1:05 PM
    Wednesday, February 7, 2018 1:04 PM
  • Roman thanks for the input. I am pretty new to Azure IoT so I don't want to complicate thing to much at this time. I

    Michael, I created a new UWP app and copied/pasted in console app code. See below. The program hangs up during the SendCloudToDeviceMessageAsync().Wait(). So I am still not able to sent a simple message to my IoT device (ESP8266).

    // UWP Code

    public sealed partial class MainPage : Page { static ServiceClient serviceClient; static string connectionString = "HostName=htsiot2.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=w8jurlZ3Cu0RUly+hfTvo/H00p4z9URhq2RM14UMBcg="; public MainPage() { this.InitializeComponent(); } private async static Task SendCloudToDeviceMessageAsync() { string sSwitchFan = "TurnFanOff"; var commandMessage = new Message(Encoding.ASCII.GetBytes("{\"Name\" : \"" + sSwitchFan + "\", \"Parameters\" : \"\"}")); await serviceClient.SendAsync("myDeviceId", commandMessage); } private void button1_Click(object sender, RoutedEventArgs e) { serviceClient = ServiceClient.CreateFromConnectionString(connectionString); SendCloudToDeviceMessageAsync().Wait(); } } }


    Wednesday, February 7, 2018 8:12 PM
  • - it looks like you have a threading problem, see this.

    Thanks



    • Edited by Roman Kiss Wednesday, February 7, 2018 8:58 PM
    • Marked as answer by Marcus Davis Thursday, February 8, 2018 1:25 AM
    Wednesday, February 7, 2018 8:57 PM
  • Roman you were correct I had a threading issue. 

    Thanks for your assistance.

    Thursday, February 8, 2018 1:30 AM