nanoFramework: Run your C# code into ESP32
In this article, we will run hello world example for IoT using ESP32 microcontroller and nanoFramework.
Nanoframework is the newest approach how to run C# code into microcontrollers (first was micro framework). In simple words, this framework allows you to run your c# apps on ESP32 (I use ESP32 WROOM 32) after installing nanoFramework on it. It’s worth mentioning that nanoFramework is supported by the .NET Fundation. From drawbacks, nanoFramework does not support ESP8266 which is the most popular Wi-Fi controller ☹️
NanoFramework has a very simple mechanism for deploying your code to the microcontroller. In this article, we will use Visual Studio 2019, nanoFramework extension, and nanoFramework version 1.14 .
In the first step we must install nanoFramework extension, to do that go to your VS 2019 => Exstensions => Manage Exstensions => search for nanoFramework => install nanoFramework. Restart VS 2019.
Now we must install nanoFramework on Windows. To do that open PowerShell and insert
dotnet tool install -g nanoff
If you want to install a specific version use this command
dotnet tool install -g nanoff --version 1.14.0
After we have nanoFramework on our PC it’s time to deploy it to ESP32. Connect your device and run this PowerShell command.
nanoff --target ESP32_WROOM_32 --serialport COM4
Of course, the number of COM may be different. To check your serial port number go to windows device manager and check.
Next in VS 2019 go to View => Other Windows => Device Explorer. After this step, you should see on the left side of the VS 2019 Device Explorer section.
In the device section of device explorer, you should see your device. Try to click the first icon (Ping device) to see if everything is OK and you can ping your device.
Clone blinky example code from my Github repo. Open project, chose device in device explorer, and click run in VS 2019. That's it congratulation you run c# code on the microcontroller. You can put a breakpoint in while loop and debug your c# IoT program 💪
On snippet, you can see the code that is running on the ESP32. I used ESP32 WROOM 32 and in this microcontroller build in led is on pin 2 but maybe you will need to provide a different number.
To run this code you need those libs
I used those versions
<package id=”nanoFramework.CoreLibrary” version=”1.10.1-preview.9" targetFramework=”netnanoframework10" />
<package id=”nanoFramework.Runtime.Events” version=”1.9.0-preview.9" targetFramework=”netnanoframework10" />
<package id=”nanoFramework.System.Device.Gpio” version=”1.0.0-preview.26" targetFramework=”netnanoframework10" />
There are plenty examples on the nanoFramework project official GitHub page. I use nanoFramework 1.14 because the newest preview version wasn’t working on my machine so keep it in mind. This project is still in preview so sometimes, something may not work 😎. But I am sure that this project is worth checking. Additionally, they have great support on discord.
If you have any questions don’t hesitate to comment on this article I will do my best to answer all questions.