This sample shows how to use the NanoFramework.System.Net API to POST a message to the Azure IoT Hub. This can also be used as a starting point to understand a HTTP Post.
The sample is located here.
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
private const string MySsid = "<replace-with-valid-ssid"; private const string MyPassword = "<replace-with-valid-password>";
-
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 = new GpioController().OpenPin(0, PinMode.Input); _userButton.ValueChanged += UserButton_ValueChanged;
- Start Microsoft Visual Studio 2022 or Visual Studio 2019 (Visual Studio 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
.
Note
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 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.