{"id":1432,"date":"2019-09-28T07:55:09","date_gmt":"2019-09-28T07:55:09","guid":{"rendered":"https:\/\/wordpress-634681-2064240.cloudwaysapps.com\/?p=1432"},"modified":"2020-05-02T22:25:43","modified_gmt":"2020-05-02T22:25:43","slug":"explorations-in-dot-net-core-3-0-for-raspberry-pi-part-4","status":"publish","type":"post","link":"https:\/\/www.petecodes.co.uk\/explorations-in-dot-net-core-3-0-for-raspberry-pi-part-4\/","title":{"rendered":"Explorations in Dot Net Core 3.0 for Raspberry Pi – Part 4"},"content":{"rendered":"\n
This is part 4 of a (at least) 5 part blog series on Dot Net Core on the Raspberry Pi….<\/em> On September 25th 2019<\/a>, the Dot Net Team released version 3.0 of the Dot Net Core Framework at .Net Conf.<\/a><\/p>\n\n\n\n To join in the fun, I held a special with Notts IoT<\/a>, the IoT group I organise in Nottingham, where I gave a talk on Dot Net Core 3.0 on the Raspberry Pi.<\/a> <\/p>\n\n\n\n This blog post is what I’ve learnt along the way to preparing for the talk as well as afterwards…! <\/p>\n\n\n\n In the last blog post<\/a> we’d created a Console app that read the status of a button and sent a message to an Azure IoT Hub.<\/p>\n\n\n\n In this post we’ll add code to allow us to receive a message back from our IoT Hub that we send from the Microsoft IoT Device Explorer. <\/p>\n\n\n\n If you’ve followed along with the previous blog, you’ll already have installed the Dot Net Core 3.0 binaries and runtimes, and have a console app running that flashes an LED and sends a message to an IoT Hub when you press a button.<\/p>\n\n\n\n Our next step is to write some code which receives messages from the IoT Hub. We can send these messages through the hub using our Azure Device Explorer<\/p>\n\n\n\n Let’s add a subroutine and some code that will listen for messages from the IoT Hub.<\/p>\n\n\n\n Create the following subroutine beneath our existing SendDeviceToCloudMessageAsync subroutine;<\/p>\n\n\n\n We’ll add a Console.WriteLine first so that we know our sub has been called successfully;<\/p>\n\n\n\n Next we’ll sit in a loop waiting for a message to be received from the IoT Hub… This is safe to do because we’re using an Asynchronous Subroutine. This way the rest of the code will continue. Add the following while loop to the subroutine;<\/p>\n\n\n\n Next we need to try receiving a Message from the IoT Hub… We do this by calling the Device Client ReceiveAsync method<\/a>. Add the following to the while loop;<\/p>\n\n\n\n If we don’t actually receive any messages from the IoT Hub, the above call will return a Null. So we need to check for this before doing anything else. Add the following below the ReceiveAsync call;<\/p>\n\n\n\n Next, assuming that we’ve received a valid message from the IoT Hub, we can decode that message in a similair way to how we encoded the message string when we were sending a message to the IoT Hub… Add the following within the if statement which will convert the IoT Hub message into an ASCII String;<\/p>\n\n\n\n We can now spit our message out to the console so we know we’ve received it;<\/p>\n\n\n\n Next we need to tell the IoT Hub that we’ve received the message successfully. We do this by calling the Device Client CompleteAsync method<\/a> on our Device Client;<\/p>\n\n\n\n
You can see Part 1 – Installation and Hello World – right here<\/a>… <\/em>
You can see Part 2 – GPIO – right here<\/a>… <\/em>
You can see <\/em>Part 3 – Sending Azure IoT Hub Messages – right here…<\/em><\/a>
You can see Part 5 – Remote Deployment and Debugging – right here…<\/a><\/em> <\/p>\n\n\n\nPreviously on Pete Codes…<\/h2>\n\n\n\n
What you’ll need <\/h2>\n\n\n\n
Putting even more “I” in IoT<\/h2>\n\n\n\n
Listen up friend<\/h2>\n\n\n\n
private static async void ReceiveCloudToDeviceMessageAsync()\n{\n \n}<\/code><\/pre>\n\n\n\n
Console.WriteLine(\"Receiving Cloud to Device messages from IoT Hub\");<\/code><\/pre>\n\n\n\n
while (true)\n{\n}<\/code><\/pre>\n\n\n\n
Message receivedMessage = await deviceClient.ReceiveAsync();<\/code><\/pre>\n\n\n\n
if (receivedMessage != null) \n{\n}<\/code><\/pre>\n\n\n\n
string receivedMessageString = Encoding.ASCII.GetString(receivedMessage.GetBytes());<\/code><\/pre>\n\n\n\n
Console.WriteLine(\"Received message: {0}\", receivedMessageString);<\/code><\/pre>\n\n\n\n
await deviceClient.CompleteAsync(receivedMessage);<\/code><\/pre>\n\n\n\n