Running .NET on ESP32
Introduction
It’s that time of year again to tidy up my desk. Always a nice period in which to find back some lost treasures. I just came across this HUZZAH32 feather board which I bought +/- a year ago. This is a tiny programmable board hosting an ESP32 System-on-Chip (SoC) micro-controller.
The ESP32 is the successor of the famous ESP8266 which are both WiFi enabled micro-controllers. The fact that they have WiFi on-board make them very interesting and useful.
As an example: I’ve built this Playmobil®-house for my two daughters where they can dim the light in each room separately using their tablet over the air. Needless to say: they love it!
The hardware for the project above only consists of an ESP8266, an I²C to 12-channel PWM controller, Osram LED strips I had lying around and a cheap 7805 voltage regulator. The ESP was programmed in C, hosting a static index.html with embedded JavaScript that sends POST requests to a simple CGI API hosted by the ESP which directs the right values over I²C to the 12-channel PWM controller.
At that time, while still being intrigued by its possibilities, I bought this ESP32-based HUZZAH32 feather board with the FeatherWing LCD.
As you can see, it’s nice and tiny hardware, but I didn’t really have a project for it so it was catching dust on my desk. Normally these micro-controllers are programmed in C or LUA but as a Microsoft .NET fanatic myself, I wondered if it would be possible to program the board using C#. Please follow me through this adventure…
The setup
Prerequisites
- An ESP32 board with a USB-to-serial bridge.
- You have the USB-to-serial bridge drivers installed.
- Visual Studio 2019, the free Community Edition should do
Install the nanoFramework extension
It’s our lucky day, it seems that there’s already an open-source project that went through the trouble of writing .NET board-support and IL firmware for the ESP32 SoC (and others). All their code and documentation is on https://github.com/nanoframework.
Anyway, to get started, we need to install their Visual Studio extension. To make sure we get the latest version, we will add their VSIX Gallery. To do this, go to Tools > Options, expand Environment and select Extensions. Now add an additional gallery with this URL: http://vsixgallery.com/feed/author/nanoframework/
After that, it’s time to add the nanoFramework extension:
Once the extension has been downloaded, we need to close Visual Studio and wait for the extension to install.
Restart Visual Studio once the extension has been installed.
Our first project
Create a blank application
We will now create a new project based on the Blank Application (nanoFramework) template.
This will generate a Program.cs with the following content:
Install the nanoFramework firmware
At this time, we still have a firmware on the ESP32 that will either run C or LUA code, so we need to flash our ESP32 with the firmware from nanoFramework.
First, we will install the ‘flasher’ application. To do this, open the Package Manager Console in Visual Studio (View > Other Windows > Package Manager Console). In the console, execute:
dotnet tool install -g nanoFirmwareFlasher
To update the tool at a later stage, execute:
dotnet tool update -g nanoFirmwareFlasher
Now, we can upload the nanoFramework to our ESP32 connected over USB. Before you continue, you need to know the virtual COM port number that was assigned to your board. This can be checked under Device Manager in the Computer Manager in Windows.
In our case, the virtual COM port number for the board is COM3. When in doubt, you can disconnect and reconnect the ESP32 to see which port pops up.
We’re ready to install the firmware. In the Package Manager console, execute (replace the COM-port number!):
The output should look like this:
From these logs, we see that the firmware version 1.4.0-preview has been uploaded, remember this version as it will be important in the next step.
Match NuGet dependencies with the firmware
One last step before we can run our project is matching the version of the nanoFramework NuGet dependencies with the firmware version installed in the previous step. In our case, this is version 1.4.0-preview.
Right-click the solution and select Manage NuGet Packages for Solution. From the Installed tab, select each nanoFramework package and upgrade or downgrade it to match the firmware version. In our case that’s 1.4.0-preview.
Start debugging
We’re ready to start debugging the Hello world application. Hit F5 or the Start Debugging button on the top and check the output window:
Can you spot “Hello world!”? Cool!
Next steps
As a next step, I would suggest exploring the other nanoFramework packages. At the time of writing, they have 45 😮 packages available. See https://www.nuget.org/profiles/nanoframework
Those that get my interest are:
That should be enough to be able to toggle a GPIO-pin through an API call, right?😎
Wrapping up
Thanks to nanoFramework, this was really easier than I thought. The amount of packages looks promising and I might make a real project out of this, so stay tuned!
That’s it for now. Have a nice day!