This sample shows how to use the NanoFramework.System.Net API to GET a message from the Azure IoT Hub. This can also be used as a starting point to understand a HTTP GET.
Note: This sample is part of a large collection of nanoFramework feature samples. If you are unfamiliar with Git and GitHub, you can download the entire collection as a ZIP file, but be sure to unzip everything to access any shared dependencies.
Any hardware device running a nanoFramework image that supports WiFi.
- Create an Azure account
- Create an IoT Hub using your Azure Account
- Create a Device in your IoT Hub
- Generate a SAS Token by creating a symmetric key for your IoT Device you created utilizing the primary key of the device. There are many ways to do this. One example using the Azure CLI can be found here. A simple way to create this is using the Azure IoT explorer program.
- Start Microsoft Visual Studio 2019 and select File > Open > Project/Solution.
- Enter in your SAS Token into Program.cs using the following format "SharedAccessSignature sr=(YourIoTHubName).azure-devices.net%2Fdevices%2F(YourdeviceName)&sig=(YourDeviceSymmetricKey)"
string sas = "<Enter SAS Token Here See Read Me for example>";
- Enter your Wifi Settings in the program.cs file and create a #define BUILD_FOR_ESP32
private const string MySsid = "<replace-with-valid-ssid";
private const string MyPassword = "<replace-with-valid-password>";
- Send a message to your device using a service such as Azure Iot Explorer under the Cloud-To-Device tab. Or you can create an Azure Function to send a message
- Connect a button from the GPIO pin in Program.cs (Pin 0) to ground. When you press the button it will create a connection from Pin 0 to ground and send a message to the cloud.
// setup user button
_userButton = GpioController.GetDefault().OpenPin(0);
_userButton.SetDriveMode(GpioPinDriveMode.Input);
_userButton.ValueChanged += UserButton_ValueChanged;
- Start Microsoft Visual Studio 2019 (VS 2017 should be OK too) and select
File > Open > Project/Solution
. - Starting in the folder where you unzipped the samples/cloned the repository, go to the subfolder for this specific sample. Double-click the Visual Studio Solution (.sln) file.
- Press
Ctrl+Shift+B
, or selectBuild > Build Solution
.
The next steps depend on whether you just want to deploy the sample or you want to both deploy and run it.
- Select
Build > Deploy Solution
.
- To debug the sample and then run it, press F5 or select
Debug > Start Debugging
.
Important: Before deploying or running the sample, please make sure your device is visible in the Device Explorer.
Tip: To display the Device Explorer, go to Visual Studio menus:
View > Other Windows > Device Explorer
.
- Since this is an embedded device with the NanoFramework installed certificates are not prebuilt into the device to make a HTTPS connection.
- Therefore if you want to make a HTTPS POST or GET you have to use your own certificates or use ones that the website already has. That is why this example uses the .pem file from azure.
- For another example of using a certificate file look at the HTTPWebRequest sample.