Connect an ESP32 To Azure IoT with .NET nanoFramework

Published Sep 13 2021 11:24 AM 1,019 Views

You don’t need a big device to connect to Azure IoT Hub

When you are looking for IoT devices to connect to Azure IoT Hub most are minicomputers or devkits that are cheap but limited in what they can do or just very power hungry. But what about using a Microprocessor I hear you say, well these are great, but you are then normally limited to programming in C which we all know isn’t great.

 

The Azure SDK for Embedded C is designed to allow small embedded (IoT) devices to communicate with Azure services and can be found here but again it’s C you are forced to use, and it’s targeted at larger device as mentioned before due to the memory required.

 

So what options do we have for us .NET developers that is smaller than a Raspberry Pi and allows us to use C#. Well, we have the awesome Wilderness Labs F7 but what about even lower powered off the shelf from Amazon devices like the ESP32-CAM-MBthat are just £10/$13 each or even cheaper if you shop around.

 

This blog post is hopefully going to show you how to take one of these super cheap and easy to purchase devices and with your .NET skills connect to Azure IoT Hub all using C#. For this magic to happen I will be using the .NET nanoFramework which makes it easy to write C# code for a select number of IoT Devices.

 

Let’s dive in…

 

Device being used

The device I am using is an ESP32-CAM and you can get them online just search your favourite online store for ESP32-CAM-MB boards.

 

2021-07-24-22-29-38.png

Connecting the device

When you first connect the device to your PC/Laptop you will hear the Windows Bing-bong but looking in the Device Manager you will notice that it appears listed under the Other Devices section.

 

2021-07-24-22-34-04.png

 

To use the ESP32-CAM-MB you need the CH340 Drivers otherwise it will not connect as a USB device. You can get installation software for the drivers from Sparkfun as this is the quickest and easiest way to install them.

 

After you have the driver installed the device then appears as an Com device in the Device Manager:

 

2021-07-24-22-36-36.png

 

Set-up Visual Studio

First I need to point out that you can use ANY version of Visual Studio to follow along, from the FREE Community Edition all the way up to the Enterprise version. You can grab the FREE Community edition Here

 

We now need to install the Nano Framework VS2019 extention this is a simple installation from either the MarketPlace or using the Extention Manager in VS19 and searching for nanoFrameWork

 

2021-08-10-17-24-15.png

 

Configure the ESP32 Board

Now that the VS19 Extention is installed you need to flash the .NET nanoFramework Firmware onto the ESP32 board as this will include the required nanoFramework Firmware and Bootloaders etc so that you can connect the board to the PC with Visual Studio and target the board for development.

 

The tool need is Open Source here on Github from the nanoFramework team if you want to read up on it.

 

From here you can see that you need to install the CLI tools which is really easy using the dotnet CLI so within your Terminal program like WindowsTerminal run this command.

 

 

dotnet tool install -g nanoff

 

 

Now that nanoff is installed you can connect your board and find it’s Com Port in your Device Manager, right click the windows logo and select Device Manager I find is the quickest way, under the Ports (Com & LPT) you will see your device if you have multiple then unplug and see which one disappears is a good way to work out which port you need. Make a note of the COM number so for me it’s COM6.

 

2021-07-24-22-36-36.png

 

Now you have the Com port you can flash the firmware so back to the Terminal program and run this command, changing the digit after the COM to match your device COM Port

 

 

nanoff --target ESP32_WROOM_32 --serialport COM6 --update --preview 

 

 

You should see it whirl away for a minute or two as it erases the flash and then burns the new firmware.

 

2021-07-24-22-44-46.png

 

Create the Project

We are now ready to create our project, so within Visual Studio create a new project on opening and in the search box enter nano and you will see that you can create a Blank Application, give it a name and save location as you normally do in Visual Studio.

 

2021-07-15-14-38-29.png

 

When you have the default blank app showing you now need to install some NuGet packages so that you can use the GPIO of the device and these can be found by typing nanoFramework.Windows.Devices.Gpio into the search box, you may also need to select the ‘Include Pre-release’ box as I did at the time of writing.

 

 

GPIO = General Purpose Input Output

 

 

2021-07-24-22-47-57.png

 

We will also need the following NuGets so may as well install them now but be warned these are currently Preview packages so be sure to tick the ‘Include Pre-release` check box:

 

 

AMQPNetLite.nanoFramework
nanoFramework.NetWorkHelper

 

 

Write some code.

I have based this project on the AMQP examples on the nanoFramework Github page but as I don’t like just reporting Temperature and prefer reporting something a little more fun and useful here is my version. I am generating a Latitude and Longitude based off a random bearing and distance from the last waypoint to simulate tracking a device in the real world. This code you can cut and paste into Visual Studio and have a read over but there are a few more steps we need to complete before we can upload the code to the device.

 

 

using Amqp;
using nanoFramework.Networking;
using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using AmqpTrace = Amqp.Trace;

namespace ConnectESP32ToIoTHub
{
    public class Program
    {
        // Set-up Wifi Credentials so we can connect to the web.
        private static string Ssid = "CAS-Wifi";
        private static string WifiPassword = "JacobAgius0406!";

        // Azure IoTHub settings
        const string _hubName = "CAS-Learning-IoTHub";
        const string _deviceId = "nanoFramework-Device1";
        const string _sasToken = "SharedAccessSignature sr=CAS-Learning-IoTHub.azure-devices.net%2Fdevices%2FnanoFramework-Device1&sig=0eGgNE7BqjzWKfeffojGJYaTEQgFV72bTytkUCkU8qQ%3D&se=1631051254";

        // Lat/Lon Points
        static double Latitude;
        static double Longitude;
        const double radius = 6378;   // Radius of earth in Kilometers at the equator, yes it's a big planet. Fun Fact it's 6356Km pole to pole so the planet is an oblate spheroid or a squashed ball.
        private static Random _random = new Random();
        static bool TraceOn = false;

        public static void Main()
        {
            // Set-up first Point and I have chossen to use the great Royal Observatory, Greenwich, UK where East meets West.
            Latitude = 51.476852;
            Longitude = 0.0;

            Debug.WriteLine("Waiting for network up and IP address...");
            bool success = false;
            CancellationTokenSource cs = new(60000);

            success = NetworkHelper.ConnectWifiDhcp(Ssid, WifiPassword, setDateTime: true, token: cs.Token);

            if (!success)
            {
                Debug.WriteLine($"Can't get a proper IP address and DateTime, error: {NetworkHelper.ConnectionError.Error}.");
                if (NetworkHelper.ConnectionError.Exception != null)
                {
                    Debug.WriteLine($"Exception: {NetworkHelper.ConnectionError.Exception}");
                }
                return;
            }
            else
            {
                Debug.WriteLine($"YAY! Connected to Wifi - {Ssid}");
            }

            // setup AMQP
            // set trace level 
            AmqpTrace.TraceLevel = TraceLevel.Frame | TraceLevel.Information;
            // enable trace
            AmqpTrace.TraceListener = WriteTrace;
            Connection.DisableServerCertValidation = false;

            // launch worker thread
            new Thread(WorkerThread).Start();

            Thread.Sleep(Timeout.Infinite);
        }

        private static void WorkerThread()
        {
            try
            {
                // parse Azure IoT Hub Map settings to AMQP protocol settings
                string hostName = _hubName + ".azure-devices.net";
                string userName = _deviceId + "@sas." + _hubName;
                string senderAddress = "devices/" + _deviceId + "/messages/events";
                string receiverAddress = "devices/" + _deviceId + "/messages/deviceBound";

                Connection connection = new Connection(new Address(hostName, 5671, userName, _sasToken));
                Session session = new Session(connection);
                SenderLink sender = new SenderLink(session, "send-link", senderAddress);
                ReceiverLink receiver = new ReceiverLink(session, "receive-link", receiverAddress);
                receiver.Start(100, OnMessage);

                while (true)
                {

                    string messagePayload = $"{{\"Latitude\":{Latitude},\"Longitude\":{Longitude}}}";

                    // compose message
                    Message message = new Message(Encoding.UTF8.GetBytes(messagePayload));
                    message.ApplicationProperties = new Amqp.Framing.ApplicationProperties();

                    // send message with the new Lat/Lon
                    sender.Send(message, null, null);

                    // data sent
                    Debug.WriteLine($"*** DATA SENT - Lat - {Latitude}, Lon - {Longitude} ***");

                    // update the location data
                    GetNewDestination();

                    // wait before sending the next position update
                    Thread.Sleep(5000);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"-- D2C Error - {ex.Message} --");
            }
        }

        private static void OnMessage(IReceiverLink receiver, Message message)
        {
            try
            {
                // command received 
                Double.TryParse((string)message.ApplicationProperties["setlat"], out Latitude);
                Double.TryParse((string)message.ApplicationProperties["setlon"], out Longitude);
                Debug.WriteLine($"== Received new Location setting: Lat - {Latitude}, Lon - {Longitude} ==");
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"-- C2D Error - {ex.Message} --");
            }
        }

        static void WriteTrace(TraceLevel level, string format, params object[] args)
        {
            if (TraceOn)
            {
                Debug.WriteLine(Fx.Format(format, args));
            }
        }

        // Starting at the last Lat/Lon move along the bearing and for the distance to reset the Lat/Lon at a new point...
        public static void GetNewDestination()
        {
            // Get a random Bearing and Distance...
            double distance = _random.Next(10);     // Random distance from 0 to 10km...
            double bearing = _random.Next(360);     // Random bearing from 0 to 360 degrees...

            double lat1 = Latitude * (Math.PI / 180);
            double lon1 = Longitude * (Math.PI / 180);
            double brng = bearing * (Math.PI / 180);
            double lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(distance / radius) + Math.Cos(lat1) * Math.Sin(distance / radius) * Math.Cos(brng));
            double lon2 = lon1 + Math.Atan2(Math.Sin(brng) * Math.Sin(distance / radius) * Math.Cos(lat1), Math.Cos(distance / radius) - Math.Sin(lat1) * Math.Sin(lat2));

            Latitude = lat2 * (180 / Math.PI);
            Longitude = lon2 * (180 / Math.PI);
        }
    }
}

 

 

Fill in the Blanks.

As you can see at the top of the Class there are some placeholders where you need to fill in with some data, the easy ones are the Wifi credentials these you hopefully should know for your local network and they will allow the ESP32 module to connect to the internet so that it can communicate with the Azure IoT Hub.

 

Next we have some Azure IoT Hub settings that are needed and this is so that the ESP32 knows which Hub and device it is as well as the connection string or SAS Token it should use to find the IoT Hub. So where do we get these from? The Azure IoT Hub name and DeviceID we can get from the Azure IoT Hub on the Azure portal so lets head out to out Azure IoT Hub and add a device, if you want more detail on creating your first Azure IoT Hub and adding a device there is no better place than Docs.Microsoft.com

 

When in theAzure Portaland you have found or created your IoT Hub on the left menu under the Explorer tab click IoT Devices and then the + Add Device.

 

2021-08-17-10-53-07.png

 

On the new pane enter a meaningful name for your device and ensure that Symmetric Keys is selected and when your happy click the Save button at the bottom of the page.

 

2021-08-17-10-57-53.png

 

This means we now have the _hubname which is the name of your IoT Hub and will be at the top of the page as you can see in the first image in this section so for me it’s CAS-Learning-IoTHub.

 

Enter that into the placeholder in the code, next is the device name you just created so you know this already and can enter that for the placeholder of _deviceId but what about that _sasToken that one is a little more tricky.

Creating the SAS Token.

You can read all about SAS Tokens on the Docs website here but how do we go about creating the Token for our device.

 

There are a few ways, as the docs suggest we can use the CLI extension command az iot hub generate-sas-token, or the Azure IoT Tools for Visual Studio Code

Lets try the CLI

In the Azure portal in the top right where your account details are shown you can click to open the Azure CLI:

 

2021-08-17-14-37-17.png

This will open the Azure CLI at the bottom of the browser window and you can do the normal resize things and drag to increase the size or use the icons on the top right of that window.

 

2021-08-17-14-39-20.png

 

Now we can use the az iot hub CLI commands to request a SAS Token for our device and this will look something like this:

 

 

az iot hub generate-sas-token --hub-name CAS-Learning-IoTHub --device-id nanoFramework-Device1 
    --login 'HostName=CAS-Learning-IoTHub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=<SECRET KEY>'

 

 

As you can see you need to use the Hub Name and the Device ID which you already have from the previous steps but what about that login part? Well this we get from the Azure IoT Hub menu in the portal and click the ‘Shared Access Policies’ then under the list of policies click iothubowner and finally copy the ‘Primary Connection String’ and it’s this you need to use as the login in the CLI, just beware that you have to surround the string in the CLI with SINGLE quote marks for it to be accepted and used.

 

2021-08-17-14-56-16.png

2021-08-17-14-51-17.png

 

Now we have the connection string we can copy this into out place holder in our code, obviously I have blurred the parts that are my keys but you hopefully get the idea here.

What about VSCode

If you have not discovered already there is a fantastic Azure IoT extention for VSCode and it’s this that will be used here.

 

Install the extention and follow the set-up instructions on the marketplace so that you can connect to your Azure Account, personally I use the 2nd option to sign in to my account when doing this on a new machine as it’s easier.

 

Setup Azure IoT Hub through Sign in to Azure

 

Now your connected in your explorer you will see you ‘AZURE IoT HUB’ tab which for me is at the bottom, open this and select your IoT Hub and then Devices.

2021-08-17-15-05-39.png

 

Now right clicking on the device will show a context menu where you can just click the ‘Generate SAS token’

 

2021-08-17-15-07-15.png

 

The VSCode Command Palette will then show asking for the number of HOURS that you wish the token to live for so enter a number.

 

Just beware that this is in hours and not seconds like the CLI so it makes the maths in your head a little easier...

 

2021-08-17-15-08-14.png

 

Once you enter this you will notice in the VSCode output window at the bottom your new SASToken will be displayed (CTRL+ALT+O if the window not displayed).

 

2021-08-17-15-11-58.png

 

Again take this and place it into the placeholder in the code.

Root Certificate

This one got me at first as it’s a very easy thing to miss so I hope me putting it here helps you or even future ME! get this right.

 

We need to ensure that the device is connecting to the web and thus IoT Hub with the correct certificate, but they are currently in the process of switching them over from the Baltimore CyberTrust to DigiCert Global G2 now rather than me try to explain this here is a blog post all about it

 

So at the time of writing this post Azure IoT will only use the Baltimore CyberTrust so lets use that, the process is the same for both anyway. First we need to download the Certificate and there is a page in the Docs that has the links.

 

2021-08-17-15-32-28.png

 

Once downloaded we need to upload this file to the ESP32 module and this is where the awesome nanoFramework team have us covered. Connect your device to your PC/Laptop again and in Visual Studio open the nanoFramework Device Explorer

 

2021-08-17-15-41-46.png

 

In here we should see our device showing up, if not click the magnifying glass to scan and be sure that the cable you are using is a data cable and not a charging only cable (I throw charging only out now I have wasted too many hours of my life being caught by them…). Once your device is shown click the ‘Edit Network Configuration’ icon as shown boxed in the screen grab.

 

2021-08-17-15-45-41.png

 

This opens a dialog box and clicking the last tab ‘General’ we can see the ‘Root CA’ where we can browse for the Cert we downloaded and have it uploaded to the ESP32 device.

 

Note:

There is no feedback sadly that the certificate is selected to be uploaded so you will have to trust in yourself that you did it right, or like me do it twice to be sure.

 

2021-08-17-15-49-24.png

 

Upload our code

Now we have all the placeholders filled in and the Root cert prepared we are ready to upload and test, now as a .NET developer what is your muscle memory for running code…. Yep click F5… How awesome is that.

 

The first time you do this it takes a minute or two but subsequent builds are much faster.

 

Don’t forget you have the full power of the Visual Studio debugger so feel free to add a breakpoint or two and step through the code to watch the magic.

 

We now have the code running on out little ESP device and we can step through and check the variables etc but how can we see the data arriving at the IoT Hub? Well that can be done very easily again using either the VSCode extention or the Azure CLi.

Monitor using the CLI

Using the same method as shown above open the Azure CLI and use the Azure CLI Command

 

 

az iot hub monitor-events --output table --hub-name {YourIoTHubName} --login 'HostName=CAS-Learning-IoTHub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=<SECRET KEY>'

 

 

You will then see the data arriving and displayed inside the CLI, the cool advantage of using the CLI commands is that once you get used to them you can use the Mobile App on your Android/IoS device which is a Xamarin App by the way to query your Azure IoT Hub and devices.

 

2021-09-07-18-06-20.png

 

Monitor using VSCode

This is by far the easier way and you right click on the device like you did to generate the SAS Token but now you select the Start Monitoring Built-In Event Endpoint from the context menu.

 

2021-09-07-17-40-02.png

 

You should now see in the Output Window at the bottom the data will start to arrive.

 

2021-09-07-17-41-42.png

 

Consuming Messages

If like me you have an S1 Standard tier IoT Hub then you have a daily limit of 400,000 message which sounds a lot but now lets think about the number of messages this actually gives. Our Device is sending a message every 5 seconds so given the 24 hrs in the day that’s:

 

24 * 60 * 60 = 86,400 seconds per day

Divide by 5 gives - 17,280 messages per day.

But with a limit of 400,000 per day that is only 23 devices before they are all used up?

 

Therefore it’s always a good idea when planning your system to think about the number of devices and also the number of messages each will send. Do you really need an update every 5 seconds for example or will every 30 seconds do? That would change this from 23 devices to 138 devices as an example.

 

Of course if you do need more you can step up to S2 giving 6 million or and S3 giving 300 million but it’s a higher cost.

 

The other alternative is to add more S1 units and get multiples of the 400,000. It’s always worth doing the maths and picking the correct tier rather than having your boss ask why the Azure bill just jumped, you can looking at the pricing more Here

 

You can check you message usage on the Overview page of the IoT Hub and look at the usage graphs as well.

 

2021-09-07-17-56-05.png

 

Conclusion

So that’s it’s congratulations you now have a cheap £10/$13 ESP32 IoT device connecting over WiFi to Azure IoT Hub and sending messages, all using C# and .NET and I hope you agree it opens the IoT space to .NET developers which is awesome.

 

Connect a sensor or two to the Board and there are millions of sensors to choose from out there even some Temp and Humidity sensors if you really have to, and you have an IoT Solution for your business or your Home Automation and much cheaper and easier than other methods.

 

Those of you that didn’t just Cut&Paste the code from above like you do over at Stack Overflow will have noticed I added an OnMessage() method and this I will use in the next post I write where I will show that you can also use nanoFramework and IoT Hub to send Cloud-2-Device messages. These are messages that are sent from the Azure Cloud down to the device to change setting or configure a value, in this case I will be editing the Lat/Lon among other things as well as plotting the points on a Bing Map using PowerBI and Stream Analytics if you can’t wait you can take a quick peak at the Microsoft Docs for that part.

 

It just leaves me to say thanks to the amazing team behind nanoFramework and also to the .NET Foundation for supporting and helping the project grow.

 

Lastly if you have any projects that need support and help from a Freelance .NET IoT and Mobile developer or just want to chat, geek out or comment on this blog post you can find me over at Twitter @CliffordAgius DM’s are open.

 

Happy Coding!

%3CLINGO-SUB%20id%3D%22lingo-sub-2731691%22%20slang%3D%22en-US%22%3EConnect%20an%20ESP32%20To%20Azure%20IoT%20with%20.NET%20nanoFramework%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2731691%22%20slang%3D%22en-US%22%3E%3CH1%20id%3D%22you-dont-need-a-big-device-to-connect-to-iothub%22%20id%3D%22toc-hId-1442569962%22%20id%3D%22toc-hId-1443642379%22%3E%3CFONT%20size%3D%225%22%3EYou%20don%E2%80%99t%20need%20a%20big%20device%20to%20connect%20to%20Azure%20IoT%20Hub%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3EWhen%20you%20are%20looking%20for%20IoT%20devices%20to%20connect%20to%20Azure%20IoT%20Hub%20most%20are%20minicomputers%20or%20devkits%20that%20are%20cheap%20but%20limited%20in%20what%20they%20can%20do%20or%20just%20very%20power%20hungry.%20But%20what%20about%20using%20a%20Microprocessor%20I%20hear%20you%20say%2C%20well%20these%20are%20great%2C%20but%20you%20are%20then%20normally%20limited%20to%20programming%20in%20C%20which%20we%20all%20know%20isn%E2%80%99t%20great.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20Azure%20SDK%20for%20Embedded%20C%20is%20designed%20to%20allow%20small%20embedded%20(IoT)%20devices%20to%20communicate%20with%20Azure%20services%20and%20can%20be%20found%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-sdk-for-c%23documentation%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehere%26nbsp%3B%3C%2FA%3Ebut%20again%20it%E2%80%99s%20C%20you%20are%20forced%20to%20use%2C%20and%20it%E2%80%99s%20targeted%20at%20larger%20device%20as%20mentioned%20before%20due%20to%20the%20memory%20required.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20what%20options%20do%20we%20have%20for%20us%20.NET%20developers%20that%20is%20smaller%20than%20a%20Raspberry%20Pi%20and%20allows%20us%20to%20use%20C%23.%20Well%2C%20we%20have%20the%20awesome%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fstore.wildernesslabs.co%2Fproducts%2Fmeadow-f7%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EWilderness%20Labs%20F7%26nbsp%3B%3C%2FA%3Ebut%20what%20about%20even%20lower%20powered%20off%20the%20shelf%20from%20Amazon%20devices%20like%20the%3CSPAN%3E%26nbsp%3BESP32-CAM-MB%3C%2FSPAN%3Ethat%20are%20just%20%C2%A310%2F%2413%20each%20or%20even%20cheaper%20if%20you%20shop%20around.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20blog%20post%20is%20hopefully%20going%20to%20show%20you%20how%20to%20take%20one%20of%20these%20super%20cheap%20and%20easy%20to%20purchase%20devices%20and%20with%20your%20.NET%20skills%20connect%20to%20Azure%20IoT%20Hub%20all%20using%20C%23.%20For%20this%20magic%20to%20happen%20I%20will%20be%20using%20the%20.NET%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwww.nanoframework.net%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EnanoFramework%26nbsp%3B%3C%2FA%3Ewhich%20makes%20it%20easy%20to%20write%20C%23%20code%20for%20a%20select%20number%20of%20IoT%20Devices.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet%E2%80%99s%20dive%20in%E2%80%A6%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22device-being-used%22%20id%3D%22toc-hId--364884501%22%20id%3D%22toc-hId--363812084%22%3E%3CFONT%20size%3D%225%22%3EDevice%20being%20used%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3EThe%20device%20I%20am%20using%20is%20an%20ESP32-CAM%20and%20you%20can%20get%20them%20online%20just%20search%20your%20favourite%20online%20store%20for%20%3CEM%3E%3CSTRONG%3EESP32-CAM-MB%3C%2FSTRONG%3E%3C%2FEM%3E%20boards.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3E%3CSTRONG%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-07-24-22-29-38.png%22%20style%3D%22width%3A%20782px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308869iE7DB445EFBA5A9CA%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-07-24-22-29-38.png%22%20alt%3D%222021-07-24-22-29-38.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%0A%3CH1%20id%3D%22connecting-the-device%22%20id%3D%22toc-hId-2122628332%22%20id%3D%22toc-hId-2123700749%22%3E%3CFONT%20size%3D%225%22%3EConnecting%20the%20device%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3EWhen%20you%20first%20connect%20the%20device%20to%20your%20PC%2FLaptop%20you%20will%20hear%20the%20Windows%20Bing-bong%20but%20looking%20in%20the%20Device%20Manager%20you%20will%20notice%20that%20it%20appears%20listed%20under%20the%26nbsp%3B%3CCODE%3EOther%20Devices%26nbsp%3B%3C%2FCODE%3Esection.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-07-24-22-34-04.png%22%20style%3D%22width%3A%20609px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308870iC53BE31A938B00C6%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-07-24-22-34-04.png%22%20alt%3D%222021-07-24-22-34-04.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20use%20the%20ESP32-CAM-MB%20you%20need%20the%20CH340%20Drivers%20otherwise%20it%20will%20not%20connect%20as%20a%20USB%20device.%20You%20can%20get%20installation%20software%20for%20the%20drivers%20from%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Flearn.sparkfun.com%2Ftutorials%2Fhow-to-install-ch340-drivers%2Fall%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3ESparkfun%3C%2FA%3E%20as%20this%20is%20the%20quickest%20and%20easiest%20way%20to%20install%20them.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAfter%20you%20have%20the%20driver%20installed%20the%20device%20then%20appears%20as%20an%20Com%20device%20in%20the%20Device%20Manager%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-07-24-22-36-36.png%22%20style%3D%22width%3A%20632px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308871iB3F281FCC675931F%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-07-24-22-36-36.png%22%20alt%3D%222021-07-24-22-36-36.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22set-up-visual-studio%22%20id%3D%22toc-hId-315173869%22%20id%3D%22toc-hId-316246286%22%3E%3CFONT%20size%3D%225%22%3ESet-up%20Visual%20Studio%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3EFirst%20I%20need%20to%20point%20out%20that%20you%20can%20use%20ANY%20version%20of%20Visual%20Studio%20to%20follow%20along%2C%20from%20the%20FREE%20Community%20Edition%20all%20the%20way%20up%20to%20the%20Enterprise%20version.%20You%20can%20grab%20the%20FREE%20Community%20edition%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fvisualstudio.microsoft.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EHere%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20now%20need%20to%20install%20the%20Nano%20Framework%20VS2019%20extention%20this%20is%20a%20simple%20installation%20from%20either%20the%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dnanoframework.nanoFramework-VS2019-Extension%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EMarketPlace%26nbsp%3B%3C%2FA%3Eor%20using%20the%20Extention%20Manager%20in%20VS19%20and%20searching%20for%26nbsp%3B%3CCODE%3EnanoFrameWork%3C%2FCODE%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-10-17-24-15.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308873iA34A0918C1EEBD28%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-10-17-24-15.png%22%20alt%3D%222021-08-10-17-24-15.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22configure-the-esp32-board%22%20id%3D%22toc-hId--1492280594%22%20id%3D%22toc-hId--1491208177%22%3E%3CFONT%20size%3D%225%22%3EConfigure%20the%20ESP32%20Board%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3ENow%20that%20the%20VS19%20Extention%20is%20installed%20you%20need%20to%20flash%20the%20.NET%20nanoFramework%20Firmware%20onto%20the%20ESP32%20board%20as%20this%20will%20include%20the%20required%20nanoFramework%20Firmware%20and%20Bootloaders%20etc%20so%20that%20you%20can%20connect%20the%20board%20to%20the%20PC%20with%20Visual%20Studio%20and%20target%20the%20board%20for%20development.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20tool%20need%20is%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fnanoframework%2FnanoFirmwareFlasher%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EOpen%20Source%20here%20on%20Github%26nbsp%3B%3C%2FA%3Efrom%20the%20nanoFramework%20team%20if%20you%20want%20to%20read%20up%20on%20it.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFrom%20here%20you%20can%20see%20that%20you%20need%20to%20install%20the%20CLI%20tools%20which%20is%20really%20easy%20using%20the%26nbsp%3B%3CCODE%3Edotnet%20CLI%26nbsp%3B%3C%2FCODE%3Eso%20within%20your%20Terminal%20program%20like%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwww.microsoft.com%2Fp%2Fwindows-terminal%2F9n0dx20hk701%3Factivetab%3Dpivot%3Aoverviewtab%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EWindowsTerminal%26nbsp%3B%3C%2FA%3Erun%20this%20command.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Edotnet%20tool%20install%20-g%20nanoff%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ENow%20that%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Enanoff%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bis%20installed%20you%20can%20connect%20your%20board%20and%20find%20it%E2%80%99s%20Com%20Port%20in%20your%20Device%20Manager%2C%20right%20click%20the%20windows%20logo%20and%20select%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EDevice%20Manager%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3BI%20find%20is%20the%20quickest%20way%2C%20under%20the%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EPorts%20(Com%20%26amp%3B%20LPT)%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Byou%20will%20see%20your%20device%20if%20you%20have%20multiple%20then%20unplug%20and%20see%20which%20one%20disappears%20is%20a%20good%20way%20to%20work%20out%20which%20port%20you%20need.%20Make%20a%20note%20of%20the%20COM%20number%20so%20for%20me%20it%E2%80%99s%20COM6.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-07-24-22-36-36.png%22%20style%3D%22width%3A%20632px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308874i3A6AAEB0413936E4%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-07-24-22-36-36.png%22%20alt%3D%222021-07-24-22-36-36.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ENow%20you%20have%20the%20Com%20port%20you%20can%20flash%20the%20firmware%20so%20back%20to%20the%20Terminal%20program%20and%20run%20this%20command%2C%20changing%20the%20digit%20after%20the%26nbsp%3B%3CCODE%3ECOM%3C%2FCODE%3E%26nbsp%3Bto%20match%20your%20device%20COM%20Port%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Enanoff%20--target%20ESP32_WROOM_32%20--serialport%20COM6%20--update%20--preview%20%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYou%20should%20see%20it%20whirl%20away%20for%20a%20minute%20or%20two%20as%20it%20erases%20the%20flash%20and%20then%20burns%20the%20new%20firmware.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-07-24-22-44-46.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308875i4269F96CD0062B9D%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-07-24-22-44-46.png%22%20alt%3D%222021-07-24-22-44-46.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22create-the-project%22%20id%3D%22toc-hId-995232239%22%20id%3D%22toc-hId-996304656%22%3E%3CFONT%20size%3D%225%22%3ECreate%20the%20Project%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3EWe%20are%20now%20ready%20to%20create%20our%20project%2C%20so%20within%20Visual%20Studio%20create%20a%20new%20project%20on%20opening%20and%20in%20the%20search%20box%20enter%26nbsp%3B%3CCODE%3Enano%26nbsp%3B%3C%2FCODE%3Eand%20you%20will%20see%20that%20you%20can%20create%20a%20Blank%20Application%2C%20give%20it%20a%20name%20and%20save%20location%20as%20you%20normally%20do%20in%20Visual%20Studio.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-07-15-14-38-29.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308876i367E314BD41D2E55%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-07-15-14-38-29.png%22%20alt%3D%222021-07-15-14-38-29.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWhen%20you%20have%20the%20default%20blank%20app%20showing%20you%20now%20need%20to%20install%20some%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ENuGet%20packages%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bso%20that%20you%20can%20use%20the%20GPIO%20of%20the%20device%20and%20these%20can%20be%20found%20by%20typing%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EnanoFramework.Windows.Devices.Gpio%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Binto%20the%20search%20box%2C%20you%20may%20also%20need%20to%20select%20the%20%E2%80%98Include%20Pre-release%E2%80%99%20box%20as%20I%20did%20at%20the%20time%20of%20writing.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EGPIO%20%3D%20General%20Purpose%20Input%20Output%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-07-24-22-47-57.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308877iDA3E41BE980CFD3C%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-07-24-22-47-57.png%22%20alt%3D%222021-07-24-22-47-57.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWe%20will%20also%20need%20the%20following%20NuGets%20so%20may%20as%20well%20install%20them%20now%20but%20be%20warned%20these%20are%20currently%20Preview%20packages%20so%20be%20sure%20to%20tick%20the%20%E2%80%98Include%20Pre-release%60%20check%20box%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EAMQPNetLite.nanoFramework%0AnanoFramework.NetWorkHelper%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22write-some-code%22%20id%3D%22toc-hId--812222224%22%20id%3D%22toc-hId--811149807%22%3E%3CFONT%20size%3D%225%22%3EWrite%20some%20code.%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3EI%20have%20based%20this%20project%20on%20the%20AMQP%20examples%20on%20the%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fnanoframework%2FSamples%2Ftree%2Fmain%2Fsamples%2FAMQP%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EnanoFramework%20Github%20page%26nbsp%3B%3C%2FA%3Ebut%20as%20I%20don%E2%80%99t%20like%20just%20reporting%20Temperature%20and%20prefer%20reporting%20something%20a%20little%20more%20fun%20and%20useful%20here%20is%20my%20version.%20I%20am%20generating%20a%20Latitude%20and%20Longitude%20based%20off%20a%20random%20bearing%20and%20distance%20from%20the%20last%20waypoint%20to%20simulate%20tracking%20a%20device%20in%20the%20real%20world.%20This%20code%20you%20can%20cut%20and%20paste%20into%20Visual%20Studio%20and%20have%20a%20read%20over%20but%20there%20are%20a%20few%20more%20steps%20we%20need%20to%20complete%20before%20we%20can%20upload%20the%20code%20to%20the%20device.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eusing%20Amqp%3B%0Ausing%20nanoFramework.Networking%3B%0Ausing%20System%3B%0Ausing%20System.Diagnostics%3B%0Ausing%20System.Text%3B%0Ausing%20System.Threading%3B%0Ausing%20AmqpTrace%20%3D%20Amqp.Trace%3B%0A%0Anamespace%20ConnectESP32ToIoTHub%0A%7B%0A%20%20%20%20public%20class%20Program%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Set-up%20Wifi%20Credentials%20so%20we%20can%20connect%20to%20the%20web.%0A%20%20%20%20%20%20%20%20private%20static%20string%20Ssid%20%3D%20%22CAS-Wifi%22%3B%0A%20%20%20%20%20%20%20%20private%20static%20string%20WifiPassword%20%3D%20%22JacobAgius0406!%22%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Azure%20IoTHub%20settings%0A%20%20%20%20%20%20%20%20const%20string%20_hubName%20%3D%20%22CAS-Learning-IoTHub%22%3B%0A%20%20%20%20%20%20%20%20const%20string%20_deviceId%20%3D%20%22nanoFramework-Device1%22%3B%0A%20%20%20%20%20%20%20%20const%20string%20_sasToken%20%3D%20%22SharedAccessSignature%20sr%3DCAS-Learning-IoTHub.azure-devices.net%252Fdevices%252FnanoFramework-Device1%26amp%3Bsig%3D0eGgNE7BqjzWKfeffojGJYaTEQgFV72bTytkUCkU8qQ%253D%26amp%3Bse%3D1631051254%22%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Lat%2FLon%20Points%0A%20%20%20%20%20%20%20%20static%20double%20Latitude%3B%0A%20%20%20%20%20%20%20%20static%20double%20Longitude%3B%0A%20%20%20%20%20%20%20%20const%20double%20radius%20%3D%206378%3B%20%20%20%2F%2F%20Radius%20of%20earth%20in%20Kilometers%20at%20the%20equator%2C%20yes%20it's%20a%20big%20planet.%20Fun%20Fact%20it's%206356Km%20pole%20to%20pole%20so%20the%20planet%20is%20an%20oblate%20spheroid%20or%20a%20squashed%20ball.%0A%20%20%20%20%20%20%20%20private%20static%20Random%20_random%20%3D%20new%20Random()%3B%0A%20%20%20%20%20%20%20%20static%20bool%20TraceOn%20%3D%20false%3B%0A%0A%20%20%20%20%20%20%20%20public%20static%20void%20Main()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Set-up%20first%20Point%20and%20I%20have%20chossen%20to%20use%20the%20great%20Royal%20Observatory%2C%20Greenwich%2C%20UK%20where%20East%20meets%20West.%0A%20%20%20%20%20%20%20%20%20%20%20%20Latitude%20%3D%2051.476852%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Longitude%20%3D%200.0%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20Debug.WriteLine(%22Waiting%20for%20network%20up%20and%20IP%20address...%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20bool%20success%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20CancellationTokenSource%20cs%20%3D%20new(60000)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20success%20%3D%20NetworkHelper.ConnectWifiDhcp(Ssid%2C%20WifiPassword%2C%20setDateTime%3A%20true%2C%20token%3A%20cs.Token)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(!success)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Debug.WriteLine(%24%22Can't%20get%20a%20proper%20IP%20address%20and%20DateTime%2C%20error%3A%20%7BNetworkHelper.ConnectionError.Error%7D.%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(NetworkHelper.ConnectionError.Exception%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Debug.WriteLine(%24%22Exception%3A%20%7BNetworkHelper.ConnectionError.Exception%7D%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Debug.WriteLine(%24%22YAY!%20Connected%20to%20Wifi%20-%20%7BSsid%7D%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20setup%20AMQP%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20set%20trace%20level%20%0A%20%20%20%20%20%20%20%20%20%20%20%20AmqpTrace.TraceLevel%20%3D%20TraceLevel.Frame%20%7C%20TraceLevel.Information%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20enable%20trace%0A%20%20%20%20%20%20%20%20%20%20%20%20AmqpTrace.TraceListener%20%3D%20WriteTrace%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Connection.DisableServerCertValidation%20%3D%20false%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20launch%20worker%20thread%0A%20%20%20%20%20%20%20%20%20%20%20%20new%20Thread(WorkerThread).Start()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20Thread.Sleep(Timeout.Infinite)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20private%20static%20void%20WorkerThread()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20try%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20parse%20Azure%20IoT%20Hub%20Map%20settings%20to%20AMQP%20protocol%20settings%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20string%20hostName%20%3D%20_hubName%20%2B%20%22.azure-devices.net%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20string%20userName%20%3D%20_deviceId%20%2B%20%22%40sas.%22%20%2B%20_hubName%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20string%20senderAddress%20%3D%20%22devices%2F%22%20%2B%20_deviceId%20%2B%20%22%2Fmessages%2Fevents%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20string%20receiverAddress%20%3D%20%22devices%2F%22%20%2B%20_deviceId%20%2B%20%22%2Fmessages%2FdeviceBound%22%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Connection%20connection%20%3D%20new%20Connection(new%20Address(hostName%2C%205671%2C%20userName%2C%20_sasToken))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Session%20session%20%3D%20new%20Session(connection)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20SenderLink%20sender%20%3D%20new%20SenderLink(session%2C%20%22send-link%22%2C%20senderAddress)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ReceiverLink%20receiver%20%3D%20new%20ReceiverLink(session%2C%20%22receive-link%22%2C%20receiverAddress)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20receiver.Start(100%2C%20OnMessage)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20while%20(true)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20string%20messagePayload%20%3D%20%24%22%7B%7B%5C%22Latitude%5C%22%3A%7BLatitude%7D%2C%5C%22Longitude%5C%22%3A%7BLongitude%7D%7D%7D%22%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20compose%20message%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Message%20message%20%3D%20new%20Message(Encoding.UTF8.GetBytes(messagePayload))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20message.ApplicationProperties%20%3D%20new%20Amqp.Framing.ApplicationProperties()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20send%20message%20with%20the%20new%20Lat%2FLon%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sender.Send(message%2C%20null%2C%20null)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20data%20sent%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Debug.WriteLine(%24%22***%20DATA%20SENT%20-%20Lat%20-%20%7BLatitude%7D%2C%20Lon%20-%20%7BLongitude%7D%20***%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20update%20the%20location%20data%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20GetNewDestination()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20wait%20before%20sending%20the%20next%20position%20update%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Thread.Sleep(5000)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20catch%20(Exception%20ex)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Debug.WriteLine(%24%22--%20D2C%20Error%20-%20%7Bex.Message%7D%20--%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20private%20static%20void%20OnMessage(IReceiverLink%20receiver%2C%20Message%20message)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20try%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20command%20received%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Double.TryParse((string)message.ApplicationProperties%5B%22setlat%22%5D%2C%20out%20Latitude)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Double.TryParse((string)message.ApplicationProperties%5B%22setlon%22%5D%2C%20out%20Longitude)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Debug.WriteLine(%24%22%3D%3D%20Received%20new%20Location%20setting%3A%20Lat%20-%20%7BLatitude%7D%2C%20Lon%20-%20%7BLongitude%7D%20%3D%3D%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20catch%20(Exception%20ex)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Debug.WriteLine(%24%22--%20C2D%20Error%20-%20%7Bex.Message%7D%20--%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20static%20void%20WriteTrace(TraceLevel%20level%2C%20string%20format%2C%20params%20object%5B%5D%20args)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(TraceOn)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Debug.WriteLine(Fx.Format(format%2C%20args))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Starting%20at%20the%20last%20Lat%2FLon%20move%20along%20the%20bearing%20and%20for%20the%20distance%20to%20reset%20the%20Lat%2FLon%20at%20a%20new%20point...%0A%20%20%20%20%20%20%20%20public%20static%20void%20GetNewDestination()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Get%20a%20random%20Bearing%20and%20Distance...%0A%20%20%20%20%20%20%20%20%20%20%20%20double%20distance%20%3D%20_random.Next(10)%3B%20%20%20%20%20%2F%2F%20Random%20distance%20from%200%20to%2010km...%0A%20%20%20%20%20%20%20%20%20%20%20%20double%20bearing%20%3D%20_random.Next(360)%3B%20%20%20%20%20%2F%2F%20Random%20bearing%20from%200%20to%20360%20degrees...%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20double%20lat1%20%3D%20Latitude%20*%20(Math.PI%20%2F%20180)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20double%20lon1%20%3D%20Longitude%20*%20(Math.PI%20%2F%20180)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20double%20brng%20%3D%20bearing%20*%20(Math.PI%20%2F%20180)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20double%20lat2%20%3D%20Math.Asin(Math.Sin(lat1)%20*%20Math.Cos(distance%20%2F%20radius)%20%2B%20Math.Cos(lat1)%20*%20Math.Sin(distance%20%2F%20radius)%20*%20Math.Cos(brng))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20double%20lon2%20%3D%20lon1%20%2B%20Math.Atan2(Math.Sin(brng)%20*%20Math.Sin(distance%20%2F%20radius)%20*%20Math.Cos(lat1)%2C%20Math.Cos(distance%20%2F%20radius)%20-%20Math.Sin(lat1)%20*%20Math.Sin(lat2))%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20Latitude%20%3D%20lat2%20*%20(180%20%2F%20Math.PI)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Longitude%20%3D%20lon2%20*%20(180%20%2F%20Math.PI)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22fill-in-the-blanks%22%20id%3D%22toc-hId-1675290609%22%20id%3D%22toc-hId-1676363026%22%3E%3CFONT%20size%3D%225%22%3EFill%20in%20the%20Blanks.%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3EAs%20you%20can%20see%20at%20the%20top%20of%20the%20Class%20there%20are%20some%20placeholders%20where%20you%20need%20to%20fill%20in%20with%20some%20data%2C%20the%20easy%20ones%20are%20the%20Wifi%20credentials%20these%20you%20hopefully%20should%20know%20for%20your%20local%20network%20and%20they%20will%20allow%20the%20ESP32%20module%20to%20connect%20to%20the%20internet%20so%20that%20it%20can%20communicate%20with%20the%20Azure%20IoT%20Hub.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%20we%20have%20some%20Azure%20IoT%20Hub%20settings%20that%20are%20needed%20and%20this%20is%20so%20that%20the%20ESP32%20knows%20which%20Hub%20and%20device%20it%20is%20as%20well%20as%20the%20connection%20string%20or%20SAS%20Token%20it%20should%20use%20to%20find%20the%20IoT%20Hub.%20So%20where%20do%20we%20get%20these%20from%3F%20The%20Azure%20IoT%20Hub%20name%20and%20DeviceID%20we%20can%20get%20from%20the%20Azure%20IoT%20Hub%20on%20the%20Azure%20portal%20so%20lets%20head%20out%20to%20out%20Azure%20IoT%20Hub%20and%20add%20a%20device%2C%20if%20you%20want%20more%20detail%20on%20creating%20your%20first%20Azure%20IoT%20Hub%20and%20adding%20a%20device%20there%20is%20no%20better%20place%20than%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fiot-hub%2Fiot-hub-create-through-portal%3FWT.mc_id%3DAZ-MVP-5003764%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EDocs.Microsoft.com%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20in%20the%3CA%20href%3D%22https%3A%2F%2Fwww.cliffordagius.co.uk%2Fpost%2Fconnectesp32toiothub%2Fportal.azure.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EAzure%20Portal%3C%2FA%3Eand%20you%20have%20found%20or%20created%20your%20IoT%20Hub%20on%20the%20left%20menu%20under%20the%20Explorer%20tab%20click%26nbsp%3B%3CCODE%3EIoT%20Devices%26nbsp%3B%3C%2FCODE%3Eand%20then%20the%26nbsp%3B%3CCODE%3E%2B%20Add%20Device%3C%2FCODE%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-17-10-53-07.png%22%20style%3D%22width%3A%20930px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308878i464B181189D11C3D%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-17-10-53-07.png%22%20alt%3D%222021-08-17-10-53-07.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EOn%20the%20new%20pane%20enter%20a%20meaningful%20name%20for%20your%20device%20and%20ensure%20that%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ESymmetric%20Keys%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bis%20selected%20and%20when%20your%20happy%20click%20the%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ESave%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bbutton%20at%20the%20bottom%20of%20the%20page.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-17-10-57-53.png%22%20style%3D%22width%3A%20743px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308879i1F9D262013AFC923%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-17-10-57-53.png%22%20alt%3D%222021-08-17-10-57-53.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20means%20we%20now%20have%20the%26nbsp%3B%3CCODE%3E_hubname%26nbsp%3B%3C%2FCODE%3Ewhich%20is%20the%20name%20of%20your%20IoT%20Hub%20and%20will%20be%20at%20the%20top%20of%20the%20page%20as%20you%20can%20see%20in%20the%20first%20image%20in%20this%20section%20so%20for%20me%20it%E2%80%99s%26nbsp%3B%3CCODE%3ECAS-Learning-IoTHub%3C%2FCODE%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEnter%20that%20into%20the%20placeholder%20in%20the%20code%2C%20next%20is%20the%20device%20name%20you%20just%20created%20so%20you%20know%20this%20already%20and%20can%20enter%20that%20for%20the%20placeholder%20of%3CCODE%3E_deviceId%3C%2FCODE%3Ebut%20what%20about%20that%3CCODE%3E_sasToken%3C%2FCODE%3Ethat%20one%20is%20a%20little%20more%20tricky.%3C%2FP%3E%0A%3CH1%20id%3D%22creating-the-sas-token%22%20id%3D%22toc-hId--132163854%22%20id%3D%22toc-hId--131091437%22%3E%3CFONT%20size%3D%225%22%3ECreating%20the%20SAS%20Token.%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3EYou%20can%20read%20all%20about%20SAS%20Tokens%20on%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fiot-hub%2Fiot-hub-dev-guide-sas%3Ftabs%3Dnode%3FWT.mc_id%3DAZ-MVP-5003764%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EDocs%20website%20here%3C%2FA%3E%20but%20how%20do%20we%20go%20about%20creating%20the%20Token%20for%20our%20device.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20are%20a%20few%20ways%2C%20as%20the%20docs%20suggest%20we%20can%20use%20the%20CLI%20extension%20command%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fcli%2Fazure%2Fiot%2Fhub%3Fview%3Dazure-cli-latest%23az_iot_hub_generate_sas_token%3FWT.mc_id%3DAZ-MVP-5003764%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Eaz%20iot%20hub%20generate-sas-token%3C%2FA%3E%2C%20or%20the%20%3CA%20href%3D%22https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dvsciot-vscode.azure-iot-tools%26amp%3BWT.mc_id%3DAZ-MVP-5003764%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20IoT%20Tools%20for%20Visual%20Studio%20Code%3C%2FA%3E%3C%2FP%3E%0A%3CH1%20id%3D%22lets-try-the-cli%22%20id%3D%22toc-hId--1939618317%22%20id%3D%22toc-hId--1938545900%22%3E%3CFONT%20size%3D%225%22%3ELets%20try%20the%20CLI%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3EIn%20the%20Azure%20portal%20in%20the%20top%20right%20where%20your%20account%20details%20are%20shown%20you%20can%20click%20to%20open%20the%20Azure%20CLI%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-17-14-37-17.png%22%20style%3D%22width%3A%20255px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308884iADB51AC8EEF34215%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-17-14-37-17.png%22%20alt%3D%222021-08-17-14-37-17.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThis%20will%20open%20the%20Azure%20CLI%20at%20the%20bottom%20of%20the%20browser%20window%20and%20you%20can%20do%20the%20normal%20resize%20things%20and%20drag%20to%20increase%20the%20size%20or%20use%20the%20icons%20on%20the%20top%20right%20of%20that%20window.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-17-14-39-20.png%22%20style%3D%22width%3A%20860px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308885iC42E461F5CE34090%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-17-14-39-20.png%22%20alt%3D%222021-08-17-14-39-20.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ENow%20we%20can%20use%20the%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fcli%2Fazure%2Fiot%2Fhub%3Fview%3Dazure-cli-latest%26amp%3BWT.mc_id%3DAZ-MVP-5003764%23az_iot_hub_generate_sas_token%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Eaz%20iot%20hub%20CLI%20commands%3C%2FA%3E%26nbsp%3Bto%20request%20a%20SAS%20Token%20for%20our%20device%20and%20this%20will%20look%20something%20like%20this%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Eaz%20iot%20hub%20generate-sas-token%20--hub-name%20CAS-Learning-IoTHub%20--device-id%20nanoFramework-Device1%20%0A%20%20%20%20--login%20'HostName%3DCAS-Learning-IoTHub.azure-devices.net%3BSharedAccessKeyName%3Diothubowner%3BSharedAccessKey%3D%3CSECRET%20key%3D%22%22%3E'%3C%2FSECRET%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAs%20you%20can%20see%20you%20need%20to%20use%20the%20Hub%20Name%20and%20the%20Device%20ID%20which%20you%20already%20have%20from%20the%20previous%20steps%20but%20what%20about%20that%20login%20part%3F%20Well%20this%20we%20get%20from%20the%20Azure%20IoT%20Hub%20menu%20in%20the%20portal%20and%20click%20the%20%E2%80%98Shared%20Access%20Policies%E2%80%99%20then%20under%20the%20list%20of%20policies%20click%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Eiothubowner%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Band%20finally%20copy%20the%20%E2%80%98Primary%20Connection%20String%E2%80%99%20and%20it%E2%80%99s%20this%20you%20need%20to%20use%20as%20the%20login%20in%20the%20CLI%2C%20just%20beware%20that%20you%20have%20to%20surround%20the%20string%20in%20the%20CLI%20with%20SINGLE%20quote%20marks%20for%20it%20to%20be%20accepted%20and%20used.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-17-14-56-16.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308886i1F087D8997C28906%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-17-14-56-16.png%22%20alt%3D%222021-08-17-14-56-16.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-17-14-51-17.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308887iDD7976E9C6CA33F5%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-17-14-51-17.png%22%20alt%3D%222021-08-17-14-51-17.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20we%20have%20the%20connection%20string%20we%20can%20copy%20this%20into%20out%20place%20holder%20in%20our%20code%2C%20obviously%20I%20have%20blurred%20the%20parts%20that%20are%20my%20keys%20but%20you%20hopefully%20get%20the%20idea%20here.%3C%2FP%3E%0A%3CH1%20id%3D%22what-about-vscode%22%20id%3D%22toc-hId-295630177%22%20id%3D%22toc-hId-296702594%22%3E%3CFONT%20size%3D%225%22%3EWhat%20about%20VSCode%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3EIf%20you%20have%20not%20discovered%20already%20there%20is%20a%20fantastic%20%3CA%20href%3D%22https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dvsciot-vscode.azure-iot-tools%26amp%3BWT.mc_id%3DAZ-MVP-5003764%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20IoT%20extention%20for%20VSCode%3C%2FA%3E%20and%20it%E2%80%99s%20this%20that%20will%20be%20used%20here.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EInstall%20the%20extention%20and%20follow%20the%20set-up%20instructions%20on%20the%20marketplace%20so%20that%20you%20can%20connect%20to%20your%20Azure%20Account%2C%20personally%20I%20use%20the%202nd%20option%20to%20sign%20in%20to%20my%20account%20when%20doing%20this%20on%20a%20new%20machine%20as%20it%E2%80%99s%20easier.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CSTRONG%3E%3CEM%3ESetup%20Azure%20IoT%20Hub%20through%20Sign%20in%20to%20Azure%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ENow%20your%20connected%20in%20your%20explorer%20you%20will%20see%20you%20%E2%80%98AZURE%20IoT%20HUB%E2%80%99%20tab%20which%20for%20me%20is%20at%20the%20bottom%2C%20open%20this%20and%20select%20your%20IoT%20Hub%20and%20then%20Devices.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-17-15-05-39.png%22%20style%3D%22width%3A%20411px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308888iB20B67A27FE08FB6%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-17-15-05-39.png%22%20alt%3D%222021-08-17-15-05-39.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ENow%20right%20clicking%20on%20the%20device%20will%20show%20a%20context%20menu%20where%20you%20can%20just%20click%20the%20%E2%80%98Generate%20SAS%20token%E2%80%99%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-17-15-07-15.png%22%20style%3D%22width%3A%20515px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308889i5B63650329D7F0A2%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-17-15-07-15.png%22%20alt%3D%222021-08-17-15-07-15.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20VSCode%20Command%20Palette%20will%20then%20show%20asking%20for%20the%20number%20of%26nbsp%3B%3CCODE%3EHOURS%3C%2FCODE%3E%26nbsp%3Bthat%20you%20wish%20the%20token%20to%20live%20for%20so%20enter%20a%20number.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CEM%3E%3CSTRONG%3EJust%20beware%20that%20this%20is%20in%20hours%20and%20not%20seconds%20like%20the%20CLI%20so%20it%20makes%20the%20maths%20in%20your%20head%20a%20little%20easier...%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-17-15-08-14.png%22%20style%3D%22width%3A%20606px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308890i12DAAF23963AE636%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-17-15-08-14.png%22%20alt%3D%222021-08-17-15-08-14.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EOnce%20you%20enter%20this%20you%20will%20notice%20in%20the%20VSCode%20output%20window%20at%20the%20bottom%20your%20new%20SASToken%20will%20be%20displayed%20(CTRL%2BALT%2BO%20if%20the%20window%20not%20displayed).%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-17-15-11-58.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308891iE8F9A2BAFF389B9C%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-17-15-11-58.png%22%20alt%3D%222021-08-17-15-11-58.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAgain%20take%20this%20and%20place%20it%20into%20the%20placeholder%20in%20the%20code.%3C%2FP%3E%0A%3CH1%20id%3D%22root-certificate%22%20id%3D%22toc-hId--1511824286%22%20id%3D%22toc-hId--1510751869%22%3E%3CFONT%20size%3D%225%22%3ERoot%20Certificate%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3EThis%20one%20got%20me%20at%20first%20as%20it%E2%80%99s%20a%20very%20easy%20thing%20to%20miss%20so%20I%20hope%20me%20putting%20it%20here%20helps%20you%20or%20even%20future%20ME!%20get%20this%20right.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20need%20to%20ensure%20that%20the%20device%20is%20connecting%20to%20the%20web%20and%20thus%20IoT%20Hub%20with%20the%20correct%20certificate%2C%20but%20they%20are%20currently%20in%20the%20process%20of%20switching%20them%20over%20from%20the%20Baltimore%20CyberTrust%20to%20DigiCert%20Global%20G2%20now%20rather%20than%20me%20try%20to%20explain%20this%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Finternet-of-things%2Fazure-iot-tls-critical-changes-are-almost-here-and-why-you%2Fba-p%2F2393169%22%20target%3D%22_blank%22%3Ehere%20is%20a%20blog%20post%20all%20about%20it%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20at%20the%20time%20of%20writing%20this%20post%20Azure%20IoT%20will%20only%20use%20the%20Baltimore%20CyberTrust%20so%20lets%20use%20that%2C%20the%20process%20is%20the%20same%20for%20both%20anyway.%20First%20we%20need%20to%20download%20the%20Certificate%20and%20there%20is%20a%20page%20in%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsecurity%2Ffundamentals%2Ftls-certificate-changes%23what-is-changing%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EDocs%3C%2FA%3E%20that%20has%20the%20links.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-17-15-32-28.png%22%20style%3D%22width%3A%20989px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308892i39A349D0C3B3E30A%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-17-15-32-28.png%22%20alt%3D%222021-08-17-15-32-28.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EOnce%20downloaded%20we%20need%20to%20upload%20this%20file%20to%20the%20ESP32%20module%20and%20this%20is%20where%20the%20awesome%20nanoFramework%20team%20have%20us%20covered.%20Connect%20your%20device%20to%20your%20PC%2FLaptop%20again%20and%20in%20Visual%20Studio%20open%20the%20nanoFramework%20Device%20Explorer%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-17-15-41-46.png%22%20style%3D%22width%3A%20984px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308893i683363AEAD2F8290%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-17-15-41-46.png%22%20alt%3D%222021-08-17-15-41-46.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIn%20here%20we%20should%20see%20our%20device%20showing%20up%2C%20if%20not%20click%20the%20magnifying%20glass%20to%20scan%20and%20be%20sure%20that%20the%20cable%20you%20are%20using%20is%20a%20data%20cable%20and%20not%20a%20charging%20only%20cable%20(I%20throw%20charging%20only%20out%20now%20I%20have%20wasted%20too%20many%20hours%20of%20my%20life%20being%20caught%20by%20them%E2%80%A6).%20Once%20your%20device%20is%20shown%20click%20the%20%E2%80%98Edit%20Network%20Configuration%E2%80%99%20icon%20as%20shown%20boxed%20in%20the%20screen%20grab.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-17-15-45-41.png%22%20style%3D%22width%3A%20739px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308894i971C27D5FC52C214%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-17-15-45-41.png%22%20alt%3D%222021-08-17-15-45-41.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThis%20opens%20a%20dialog%20box%20and%20clicking%20the%20last%20tab%20%E2%80%98General%E2%80%99%20we%20can%20see%20the%20%E2%80%98Root%20CA%E2%80%99%20where%20we%20can%20browse%20for%20the%20Cert%20we%20downloaded%20and%20have%20it%20uploaded%20to%20the%20ESP32%20device.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CEM%3E%3CSTRONG%3ENote%3A%20%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CEM%3E%3CSTRONG%3EThere%20is%20no%20feedback%20sadly%20that%20the%20certificate%20is%20selected%20to%20be%20uploaded%20so%20you%20will%20have%20to%20trust%20in%20yourself%20that%20you%20did%20it%20right%2C%20or%20like%20me%20do%20it%20twice%20to%20be%20sure.%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-17-15-49-24.png%22%20style%3D%22width%3A%20802px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308895i4F8CC56C7D374BB9%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-08-17-15-49-24.png%22%20alt%3D%222021-08-17-15-49-24.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22upload-our-code%22%20id%3D%22toc-hId-975688547%22%20id%3D%22toc-hId-976760964%22%3E%3CFONT%20size%3D%225%22%3EUpload%20our%20code%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3ENow%20we%20have%20all%20the%20placeholders%20filled%20in%20and%20the%20Root%20cert%20prepared%20we%20are%20ready%20to%20upload%20and%20test%2C%20now%20as%20a%20.NET%20developer%20what%20is%20your%20muscle%20memory%20for%20running%20code%E2%80%A6.%20Yep%20click%20F5%E2%80%A6%20How%20awesome%20is%20that.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20first%20time%20you%20do%20this%20it%20takes%20a%20minute%20or%20two%20but%20subsequent%20builds%20are%20much%20faster.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDon%E2%80%99t%20forget%20you%20have%20the%20full%20power%20of%20the%20Visual%20Studio%20debugger%20so%20feel%20free%20to%20add%20a%20breakpoint%20or%20two%20and%20step%20through%20the%20code%20to%20watch%20the%20magic.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20now%20have%20the%20code%20running%20on%20out%20little%20ESP%20device%20and%20we%20can%20step%20through%20and%20check%20the%20variables%20etc%20but%20how%20can%20we%20see%20the%20data%20arriving%20at%20the%20IoT%20Hub%3F%20Well%20that%20can%20be%20done%20very%20easily%20again%20using%20either%20the%20VSCode%20extention%20or%20the%20Azure%20CLi.%3C%2FP%3E%0A%3CH1%20id%3D%22monitor-using-the-cli%22%20id%3D%22toc-hId--831765916%22%20id%3D%22toc-hId--830693499%22%3E%3CFONT%20size%3D%225%22%3EMonitor%20using%20the%20CLI%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3EUsing%20the%20same%20method%20as%20shown%20above%20open%20the%20Azure%20CLI%20and%20use%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fiot-hub%2Fquickstart-send-telemetry-cli%3FWT.mc_id%3DAZ-MVP-5003764%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20CLI%20Command%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Eaz%20iot%20hub%20monitor-events%20--output%20table%20--hub-name%20%7BYourIoTHubName%7D%20--login%20'HostName%3DCAS-Learning-IoTHub.azure-devices.net%3BSharedAccessKeyName%3Diothubowner%3BSharedAccessKey%3D%3CSECRET%20key%3D%22%22%3E'%3C%2FSECRET%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYou%20will%20then%20see%20the%20data%20arriving%20and%20displayed%20inside%20the%20CLI%2C%20the%20cool%20advantage%20of%20using%20the%20CLI%20commands%20is%20that%20once%20you%20get%20used%20to%20them%20you%20can%20use%20the%20Mobile%20App%20on%20your%20Android%2FIoS%20device%20which%20is%20a%20Xamarin%20App%20by%20the%20way%20to%20query%20your%20Azure%20IoT%20Hub%20and%20devices.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-09-07-18-06-20.png%22%20style%3D%22width%3A%20473px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308896i0507FAE7716E703F%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-09-07-18-06-20.png%22%20alt%3D%222021-09-07-18-06-20.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22monitor-using-vscode%22%20id%3D%22toc-hId-1655746917%22%20id%3D%22toc-hId-1656819334%22%3E%3CFONT%20size%3D%225%22%3EMonitor%20using%20VSCode%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3EThis%20is%20by%20far%20the%20easier%20way%20and%20you%20right%20click%20on%20the%20device%20like%20you%20did%20to%20generate%20the%20SAS%20Token%20but%20now%20you%20select%20the%3CCODE%3EStart%20Monitoring%20Built-In%20Event%20Endpoint%3C%2FCODE%3Efrom%20the%20context%20menu.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-09-07-17-40-02.png%22%20style%3D%22width%3A%20441px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308897i044532ACC2BF84F9%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-09-07-17-40-02.png%22%20alt%3D%222021-09-07-17-40-02.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYou%20should%20now%20see%20in%20the%20Output%20Window%20at%20the%20bottom%20the%20data%20will%20start%20to%20arrive.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-09-07-17-41-42.png%22%20style%3D%22width%3A%20865px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308898iEF7F8F0FA1A3E36F%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-09-07-17-41-42.png%22%20alt%3D%222021-09-07-17-41-42.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22consuming-messages%22%20id%3D%22toc-hId--151707546%22%20id%3D%22toc-hId--150635129%22%3E%3CFONT%20size%3D%225%22%3EConsuming%20Messages%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3EIf%20like%20me%20you%20have%20an%3CCODE%3ES1%20Standard%3C%2FCODE%3Etier%20IoT%20Hub%20then%20you%20have%20a%20daily%20limit%20of%20400%2C000%20message%20which%20sounds%20a%20lot%20but%20now%20lets%20think%20about%20the%20number%20of%20messages%20this%20actually%20gives.%20Our%20Device%20is%20sending%20a%20message%20every%205%20seconds%20so%20given%20the%2024%20hrs%20in%20the%20day%20that%E2%80%99s%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CEM%3E24%20*%2060%20*%2060%20%3D%2086%2C400%20seconds%20per%20day%3C%2FEM%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CEM%3E%20Divide%20by%205%20gives%20-%2017%2C280%20messages%20per%20day.%20%3C%2FEM%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CEM%3EBut%20with%20a%20limit%20of%20400%2C000%20per%20day%20that%20is%20only%2023%20devices%20before%20they%20are%20all%20used%20up%3F%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETherefore%20it%E2%80%99s%20always%20a%20good%20idea%20when%20planning%20your%20system%20to%20think%20about%20the%20number%20of%20devices%20and%20also%20the%20number%20of%20messages%20each%20will%20send.%20Do%20you%20really%20need%20an%20update%20every%205%20seconds%20for%20example%20or%20will%20every%2030%20seconds%20do%3F%20That%20would%20change%20this%20from%2023%20devices%20to%20138%20devices%20as%20an%20example.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOf%20course%20if%20you%20do%20need%20more%20you%20can%20step%20up%20to%20S2%20giving%206%20million%20or%20and%20S3%20giving%20300%20million%20but%20it%E2%80%99s%20a%20higher%20cost.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20other%20alternative%20is%20to%20add%20more%20S1%20units%20and%20get%20multiples%20of%20the%20400%2C000.%20It%E2%80%99s%20always%20worth%20doing%20the%20maths%20and%20picking%20the%20correct%20tier%20rather%20than%20having%20your%20boss%20ask%20why%20the%20Azure%20bill%20just%20jumped%2C%20you%20can%20looking%20at%20the%20pricing%20more%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fpricing%2Fdetails%2Fiot-hub%2F%3FWT.mc_id%3DAZ-MVP-5003764%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EHere%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20check%20you%20message%20usage%20on%20the%20Overview%20page%20of%20the%20IoT%20Hub%20and%20look%20at%20the%20usage%20graphs%20as%20well.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-09-07-17-56-05.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308899i7D626918758FC964%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222021-09-07-17-56-05.png%22%20alt%3D%222021-09-07-17-56-05.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22conclusion%22%20id%3D%22toc-hId--1959162009%22%20id%3D%22toc-hId--1958089592%22%3E%3CFONT%20size%3D%225%22%3EConclusion%3C%2FFONT%3E%3C%2FH1%3E%0A%3CP%3ESo%20that%E2%80%99s%20it%E2%80%99s%20congratulations%20you%20now%20have%20a%20cheap%20%C2%A310%2F%2413%20ESP32%20IoT%20device%20connecting%20over%20WiFi%20to%20Azure%20IoT%20Hub%20and%20sending%20messages%2C%20all%20using%20C%23%20and%20.NET%20and%20I%20hope%20you%20agree%20it%20opens%20the%20IoT%20space%20to%20.NET%20developers%20which%20is%20awesome.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EConnect%20a%20sensor%20or%20two%20to%20the%20Board%20and%20there%20are%20millions%20of%20sensors%20to%20choose%20from%20out%20there%20even%20some%20Temp%20and%20Humidity%20sensors%20if%20you%20really%20have%20to%2C%20and%20you%20have%20an%20IoT%20Solution%20for%20your%20business%20or%20your%20Home%20Automation%20and%20much%20cheaper%20and%20easier%20than%20other%20methods.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThose%20of%20you%20that%20didn%E2%80%99t%20just%20Cut%26amp%3BPaste%20the%20code%20from%20above%20like%20you%20do%20over%20at%20Stack%20Overflow%20will%20have%20noticed%20I%20added%20an%3CCODE%3EOnMessage()%3C%2FCODE%3Emethod%20and%20this%20I%20will%20use%20in%20the%20next%20post%20I%20write%20where%20I%20will%20show%20that%20you%20can%20also%20use%20nanoFramework%20and%20IoT%20Hub%20to%20send%20Cloud-2-Device%20messages.%20These%20are%20messages%20that%20are%20sent%20from%20the%20Azure%20Cloud%20down%20to%20the%20device%20to%20change%20setting%20or%20configure%20a%20value%2C%20in%20this%20case%20I%20will%20be%20editing%20the%20Lat%2FLon%20among%20other%20things%20as%20well%20as%20plotting%20the%20points%20on%20a%20Bing%20Map%20using%20PowerBI%20and%20Stream%20Analytics%20if%20you%20can%E2%80%99t%20wait%20you%20can%20take%20a%20quick%20peak%20at%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fiot-hub%2Fiot-hub-live-data-visualization-in-power-bi%3FWT.mc_id%3DAZ-MVP-5003764%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EMicrosoft%20Docs%3C%2FA%3E%20for%20that%20part.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt%20just%20leaves%20me%20to%20say%20thanks%20to%20the%20%3CA%20href%3D%22https%3A%2F%2Fopencollective.com%2Fnanoframework%23support%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Eamazing%20team%20behind%20nanoFramework%3C%2FA%3E%20and%20also%20to%20the%20%3CA%20href%3D%22https%3A%2F%2Fdotnetfoundation.org%2Fprojects%2Fnanoframework%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3E.NET%20Foundation%3C%2FA%3E%20for%20supporting%20and%20helping%20the%20project%20grow.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELastly%20if%20you%20have%20any%20projects%20that%20need%20support%20and%20help%20from%20a%20Freelance%20.NET%20IoT%20and%20Mobile%20developer%20or%20just%20want%20to%20chat%2C%20geek%20out%20or%20comment%20on%20this%20blog%20post%20you%20can%20find%20me%20over%20at%20%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2FCliffordAgius%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3ETwitter%20%40CliffordAgius%3C%2FA%3E%20DM%E2%80%99s%20are%20open.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHappy%20Coding!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2731691%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22post-content-preview%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222021-08-10-17-24-15.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F310035i92BBE266AA7D4B63%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%222021-08-10-17-24-15.png%22%20alt%3D%222021-08-10-17-24-15.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3EUsing%26nbsp%3Bthe%26nbsp%3BOpen%26nbsp%3BSource%26nbsp%3Bproject%26nbsp%3BNanoFramework%26nbsp%3BI'll%26nbsp%3Bshow%26nbsp%3Byou%26nbsp%3Bhow%26nbsp%3Bto%26nbsp%3Bconnect%26nbsp%3Band%26nbsp%3BESP32%26nbsp%3Bdevice%26nbsp%3Bthat%26nbsp%3Bis%26nbsp%3Bvery%26nbsp%3Bcheap%26nbsp%3Bto%26nbsp%3BAzure%26nbsp%3BIoT%26nbsp%3BHub%26nbsp%3Band%26nbsp%3Bsend%26nbsp%3Btelemetry%26nbsp%3Bdata%26nbsp%3Bup%26nbsp%3Bto%26nbsp%3Bthe%26nbsp%3BCloud%26nbsp%3Bfor%26nbsp%3Bprocessing.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3E%3CLI-WRAPPER%3E%3C%2FLI-WRAPPER%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2731691%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3E.NET%20nanoFramework%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20IoT%20Hub%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EIoT%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Sep 13 2021 11:24 AM