S06E12 1h 09mSeason 6

Episode Summary
In the rapidly evolving landscape of embedded systems and IoT development, the .NET nanoFramework emerges as a powerful platform that empowers developers to create applications for resource-constrained devices. This stripped-down version of the .NET Framework offers a lightweight and efficient solution, providing a familiar coding experience while abstracting away the complexities of working with microcontrollers. In this article, we delve into the insights shared in an episode of The Modern .NET Show, where experts discuss the capabilities and advantages of .NET nanoFramework.
Net nanoFramework is a specialized platform designed specifically for embedded systems and IoT development. With its ability to align closely with the full .NET Framework API, .NET nanoFramework enables developers to write code for microcontrollers with limited Flash and RAM capabilities. By dividing the framework into various NuGet packages, developers can selectively choose the components they require, ensuring optimal usage of memory and storage space.
One of the standout features of .NET nanoFramework is its ability to boost productivity and facilitate code reusability. The platform employs the same project system as traditional .NET, allowing developers to leverage their existing knowledge and skills. This enables them to quickly transition from desktop development to coding for embedded systems with just a few days of training. Additionally, the framework’s layered approach ensures that the upper layers of the application remain consistent, even when migrating between platforms. This leads to significant productivity gains and minimizes the need for extensive code modifications.
Embedded systems and IoT devices often operate under stringent resource constraints, such as limited memory and processing power. .NET nanoFramework acknowledges these limitations and offers a set of efficient tools and functionalities to address them. While the lack of support for Async Await may seem unconventional, it aligns with the nature of embedded systems, where quick request handling and efficient task management take precedence over complex multi-threading. Instead, developers can utilize multithreading to achieve similar results and optimize their code for improved performance.
.NET nanoFramework emerges as a game-changer in the realm of embedded systems and IoT development. Its ability to provide a lightweight and efficient platform, coupled with a familiar .NET coding experience, empowers developers to overcome resource constraints and build robust applications. With its focus on productivity, code reusability, and hardware compatibility, .NET nanoFramework paves the way for rapid innovation and development in the embedded systems landscape.
Episode Transcription
Welcome to The Modern .NET Show! Formerly known as The .NET Core Podcast, we are the go-to podcast for all .NET developers worldwide and I am your host Jamie “GaProgMan” Taylor.
In this episode, I spoke with José Simões about the .NET nanoFramework, a powerful platform for embedded systems and IoT development. I was incredibly impressed when José spoke about just how quickly you can get started with .NET nanoFramework and an ESP32:
You grab a breadboard, you stick an ESP 32 module onto it, and you plug an LED or a sensor or wherever, and in half an hour, you have a proof of concept of something that you are building or just having fun with. And this is great not only for learning to code, but to come up with a proof of concept that you want to show to a customer.
If you want to do that in C, I dare you to do that. On the same time, you won’t be able to. Meaning that with that code and that proof of concept, then you can evolve, you can keep working on it, and then you can easily migrate that to a production grade system because you will be using the same code. You don’t have to change much to get it working.
So let’s sit back, open up a terminal, type in dotnet new podcast and we’ll dive into the core of Modern .NET.
Jamie
Welcome to the show. Oh, my goodness. We’ve been working on this episode for a while. We got chatting at MVP Summit back in April of 2023, because I’m not sure when this is going out. I’m hoping for a November release, but it will depend. It’s been a while. My goodness. Welcome to the show.
José
Jamie
José
Jamie
José
Jamie
José
Jamie
José
Jamie
José
Jamie
So I guess before we talk about .NET nanoFramework, mainly because I was a little confused coming in, is there a difference between embedded and IoT? And is it more of a personal difference for you, or is it like an industry difference? Like, if somebody was to come to me and say, “hey, Jamie, I want to be an embedded engineer, like an embedded systems engineer,” am I telling them, go get nanoFramework? Am I telling them, Go get a raspberry pi am I telling them, go do some ZigBee stuff. We’re not sponsored by ZigBee or anything like that. It’s just something that I have experience with. What is the difference? If there is a difference between embedded software engineering and IoT based software engineering.
José
Jamie
Does that mean that perhaps a power supply is also an embedded system, or are we talking just something that runs a rudimentary operating system?
José
Jamie
José
Jamie
José
Jamie
José
Jamie
José
Jamie
dotnet new nanoframework webapplication - I’m not sure what the name would be, but something along those lines. And then I hit run and guess what? I’ve got a web server sitting on my embedded device. That’s pretty cool.José
dotnet new because we are not in the same page as the .NET tooling. We are still using the old project system, but that’s only a nuance. On this you go to Visual Studio, you go to the project templates and you’ll have there the class template, the application template, as you are used to, just like any other C# application.Jamie
dotnet new because I spend a lot of time on the command line., And I’ve told this story several times. The listeners will be bored of me telling the story. But in the intro to every episode, I say dotnet new podcast, because I used to actually spin up new episodes of the podcast recording and all the ephemera that comes with it by doing a .NET podcast in the command line. Because you could do almost anything with the new templates, right?José
Jamie
José
What we do, we try to align as much as possible - and that’s been accomplished so far - with an API, which is the same that you find on the full net, let’s call it this way. And then you bring in NuGet packages. And we have this pretty much splitted and various NuGet packages for the simple reason that when you’re building your embedded system or your project nanoFramework project, if you will, and because the flash and RAM are usually sparse, you just bring in the absolute minimum that you need so you don’t go past those limits of the available memory and flash storage.
That said, we have an alignment as much as possible with [the .NET] API. The only difference that you’ll notice is that we don’t have support for generics yet, but that it’s in the works. And the other most obvious thing, we don’t have also the
async and await pattern there, although we have multithreading and all that stuff, we can do pretty much the same, just that we don’t have the async/await pattern. Apart from that, all the basic stuff from mscoree.lib, like string manipulation, byte arrays, encoding, whatever you have there, math library, all the same API as you have on the system.NET for networking, sockets, all that stuff, it’s there and it’s the same.You can follow that mantra from .NET, “you compile once and you run everywhere.”! It’s pretty much the same, although our execution engine is different, because it has to run on a microcontroller. But basically this is it. You can port your code from a desktop application, you can share code from your desktop application and nanoFramework, project all that is possible and everything that the .NET ecosystem allows you to do, you can do it with .NET nanoFramework.
Jamie
I mean, that makes sense, right? Like the current lack of
async/await makes sense because you were saying earlier on about how it is you’ve got an embedded system, you’ve got very small amount of memory to play with. You don’t necessarily want to have to deal with setting up the whole task management and the finite state machine and awaiting things. I’m imagining an IoT or embedded system situation where you don’t really want to be hyperthreaded, you don’t really want multiple threads. You want to be able to take a request and deal with it, send the response out straight away. You don’t want to have to take a request, put it on a thread, fire up a new… put it onto all sorts of stuff. You’re not doing multi user, you’re not really aiming for millions of people sending requests all at once. You’re likely aiming to deal with maybe, I don’t know - I’m going to just pull a number out the air, I’m going to be way off - but like maybe ten requests every second rather than a million requests every second, right?José
On the need for having
async/await, you can replace that with what we have, which is multithreading, and you will be using here the API of I would say the 4.7 or 4.8 network, which is the new thread. Start that thing because you can have as many threads as the amount of available memory allows you to. So you can split things between threads just like you would do on a desktop application. Because for example, you can have one thread or timer pulling a sensor reading every 30 seconds and then storing it on wherever storage you have. You can have another thread which is responsible for dealing with the LEDs, giving you the visual output on the system or even a display on where you want to output a sensor reading. And you may want to have another thread which is dealing with the network connection and sending the telemetry or maybe connecting to IoT Hub. So all that is possible and you can have pretty much the same, and equivalent stuff on what you get with async/await, but just with I would call it the old .NET patterns with around system thread.Jamie
async/await this because I’m running on a modern operating system on pretty powerful hardware.” Perhaps in the cloud, perhaps on my computer. So I can rely on that. Whereas I guess with embedded systems, with IoT, it’s a completely different paradigm. Right?José
On the other aspect. Yes, you have to deal with the differences and that can be something that kind of improves your skills as a developer because you have to be very careful with the memory usage because it’s short, it’s cast, and you just don’t go and declare a byte array of one mega because you don’t have enough room there. And you have to be careful on you don’t want to declare your variables and having them around just because you have to be wise on all that. And you have to be careful with everything that relates with memory. So it’s kind of a nice exercise as a developer to do some coding in nanoFramework because you have to mind about all those things that you don’t when you have a bunch of RAM and storage space and everything that you are used to when you are coding for desktop.
Sponsor Message
Do you have a WPF application and want to take it to macOS or Linux? Avalonia XPF, a binary-compatible cross-platform fork of WPF, enables WPF apps to run on new platforms with minimal effort and maximum compatibility.
With a few tweaks to your project file, your WPF app and all its dependencies are ready for testing on new platforms.
Start your app transformation journey with a 30-day free trial.
Head over to http://avaloniaui.net/themoderndotnetshow to get started today.
Jamie
Whereas, like you were saying there, the IoT paradigm means we’re going to strip all of that away. Strip all of that back to perhaps back to the style of programming that people did in the 70s and 80s where it was like, “no, we have to do precisely just what we need and nothing more.”
José
Jamie
José
Jamie
José
Jamie
José
And expanding a bit on this. On Raspberry Pi. You can go with the full net. There is the IoT core that you can run there on a Raspberry Pi. So it doesn’t make much sense to run on ananoFramework there. You’ll be running .NET, but not just nanoFramework. nanoFramework targets more resource constrained smart controllers, meaning that the all famous ESP32s out there and the likes STM 32s, NXP, Microcontrollers, Silicon Labs, Giant Jackals, a couple of microcontroller series from Texas Instruments, NXP. I already mentioned that there are firmware specifics for all those. So you have an idea.
The absolute minimum requirement in terms of storage to run on nanoFramework, it’s a microcontroller with 32 bits, and 128K’s of flash, and 64Ks of RAM. If you have more than that, it will run, and you can have a C# application there.
What you need beyond that, it depends on how complex your application is or how fast you want to or how performance you want the computation to work. So that’s the minimum requirement to run .NET nanoFramework. Am I making sense?
Jamie
Whereas I’m sitting here talking to you using a MacBook Air that has 24GB of RAM and 8 cores of some weird new CPU architecture that no one’s ever heard of. Well, I mean, they have heard of it, it’s an M2, but you know what I mean. It just boggles my mind, the difference in system requirements, right? Like I said, I’ve worked on, I’ve consulted on applications that are sitting on boxes, like servers, very expensive servers that are about as powerful as this Mac Air M2, which was top of the line when I bought it, that can’t handle more than 200 requests coming in a second. And you’re saying, “yeah, it’s fine, I mean, just go get yourself an ESP32 with teeny amounts of RAM and guess what? We’ve got a system running.”
José
Jamie
José
And that’s one of the key things of the .NET nanoFramework proposal, that you can have a regular desktop developer and you can have it coding for embedded systems in a very short time. And a very short time is just a few days training. Because everything which is complicated on embedded systems development - and this is complicated, don’t get me wrong. It’s abstracted by the nanoFramwork and the firmware that you have it because when you are coding for it, you just need to be made aware of how you connect to that chip using that bus and the GPIOs - which are the inputs outputs on the microcontroller - work, which is something that, with just a few explanations, you will understand that right away. And that makes you productive.
And considering what you have to pay these days to a specialized developer on C, on C++ and how much more common and C# developer is, you can see where this is going. You can even have people coding in the morning to embed the system in the afternoon to the cloud API that’s running on Azure, and the same person can handle that. But if you want to change that to a C/C++ programmer, you won’t be able to do that. And not to mention all the productivity gains that you have on coding with a managed language like C# is. If you code in C, you know that it can be painful to deal with pointers. You have to deal with memory management. You have to allocate memory. You have to deallocate memory. If you have a byte array, it will happily overrun it and start doing bad things and you have to be aware of that. And that can cost you time if you do a bug around that and you have to find it and you have to fix it. If you are using a managed language like C#, all of that goes away and you can just focus on your task at hand.
It’s a breeze to work with string manipulation, which is hard in C. You don’t have to worry whatsoever with memory management. The garbage collector will do that for you happily. And you just have to code. And this is something huge in terms of efficiency. And if you are a company, you are obviously worried with time to market and being efficient and the customer comes up with a bug and you can find it very easily, you can come up with the fix and you do a new release. Everything, it’s much easier when you are coding in C#.
Not to mention the coding experience. You have the Visual Studio IDE to work with, you have IntelliSense just like you have on a desktop project. You have all those helps from GitHub Copilot if you are lucky enough to have it. With everything that we are spoiled as developers when coding in Visual Studio, you just need a new USB cable to deploy to your nanoFramework board. You hit F5, you are running the application right on the board with just a plain, simple USB cable, as compared to… and if you go into the debug session, you can set breakpoints, you can go with the code back and forth. You can inspect locals, you can change the variable contents, everything that you are used to and all that. Or most of that doesn’t happen, or it’s not available, or it’s much harder if you are doing it with C or C++ on one of those wacky IDEs that are out there for embedded system developers.
Jamie
José
Jamie
Well, I’m guessing I could use
WebClient, or if we’re lucky enough, HttpClient, either way. But reading from the GPIO port, I’m guessing there’s probably a NuGet package for whatever I’m reading over the GPIO port. Right, I’m going to say, right, I need to read pins one, two and three. I need to read four bytes of data done right? Whereas with C++ it would be like interrogate these individual memory addresses for this much time and then format the answer. I feel like a lot of .NET devs don’t realize just how much effort there is.José
If you want to do that on C or C++ you have to go through the data sheet or the manual for your microcontroller. You have to find the address for the register that’s holding the contents for those GPIO ports that you want to read. You want to know which byte exactly it matches the GPIO that you want to read and you need to read that register and then you need to parse it and see if it’s high or low.
With C# and nanoFramework, as you were saying, you just reference the system device, GPIO NuGet package, and depending on the encoding of the microcontroller you’re using, “hey, I want to read bit two from port C,” and that’s it. You get a pin value object which is high or low depending on the state of the object and that’s it.
Because on… getting back to the C#, to the C description that I was giving a moment ago before that and for the GPIO to work you have to find also the registers that configure the GPIO to behave the way that you want; and if you want them to be input output and if they are transitioning states, if you want to be aware of transition high to low, low to high or both, blah, blah, blah and you have a bunch of stuff which you don’t even have to think about it when you are coding with nanoFramework because all that is abstracted from you and you just need to focus on the task at hand. “Hey, I want to read the state of pin two on port C,” and that’s it. One line of code.
Jamie
José
Jamie
José
Jamie
José
Jamie
José
Jamie
José
And so, having said that, the productivity, we already covered that. And the fact that you can have regular desktops developers using it, it’s also a plus. And regarding the usage fields, let me give you a couple of examples. One of the largest projects that we’ve just wrapped up a couple of months ago is for a customer on the US, which is Skyworks. They are a silicon manufacturer and we’re working with their timing division. They were for a long time a .NET shop, but they use .NET for coding the desktop applications. There are applications on which you configure the various parameters on the timing devices and here on timing devices, these are chips that generate clock signals and they are used from satellites to regular cellular devices and everything that needs timing, which is pretty much everything on embedded systems. And you have those desktop applications that were coding in C#. And the EBs - which stands for “Evaluation Boards” for the various components - were coded in C and C++.
They choose to switch that to nanoFramework in order to use .NET C# from the microcontroller up to the production line, which they now have, meaning that they are reusing code and classes across these different applications. They have the EB’s running firmware, which is nanoFramework. They have the desktop application which is connected to the EB through a USB cable running net, but the full .NET. They have C# .NET applications on the production lines doing the QA on the devices on the boards when they are developing. The have huge test rigs with a bunch of wires connected to the boards. And they have temperature chambers to evaluate the stability of the chips across the temperature range that they work with. It’s a very complex system. Everything runs around .NET, and with nanoFramework they manage to reach the last mile if you want to be able to do that on the pods. And that’s a typical usage for nanoFramework.
And then you have other customers that are using this on boards which run telemetry system for oil fields, which are deployed out there in the desert. You have a small box which is powered by a solar panel, and the device sits there every hour or so. It wakes up, reads a bunch of the sensors that you have there from the simplest, “what’s the level on the oil there? What’s the temperature?” And a bunch of others. It composes a package with all that telemetry data. It sends that telemetry data through a satellite connection, which will go into IoT Hub, and then get back to sleep. On the next couple of hours, it will wake up again. It will do the same thing. And this is over and over and over.
So this is kind of two, I would say, opposite fields, applications on embedded systems running nanoFramework. One of them is highly connected to the cloud, to the Internet of things. And the other one, it’s not. It’s a desktop application, if you will, because it’s not even connected to the Internet because it doesn’t need to.
Jamie
José
And now, in C#, you know that you can be very productive and you can code pretty much everything very efficiently, which makes this a great platform. You grab a breadboard, you stick an ESP32 module onto it, and you plug an LED or a sensor or wherever, and in half an hour, you have a proof of concept of something that you are building or just having fun with. And this is great not only for learning to code, but to come up with a proof of concept that you want to show to a customer.
If you want to do that in C, I dare you to do that [in] the same time, you won’t be able to. Meaning that with that code and that proof of concept, then you can evolve, you can keep working on it, and then you can easily migrate that to a production grade system because you will be using the same code. You don’t have to change much to get it working. And even if you change platform, if you go, for example, your proof of concept is on an ESP32 device and that you have on your breadboard. Okay, that’s working. You have your project in C#. All good. You need to move to production. But for some reason, you are not using an ESP32, you are using an NXP microcontroller. That’s good.
You just grab the code on that other device that will go, and that will be your production platform. And everything works. You may have to adjust a couple of GPIOs which are not in the same location, but the 99% of the code will just work. And despite you have changed the platform because the firmware has abstracted all the hardware intricacies from you and you can do that if you were doing it with C, you could not do that ever. You have to change everything because you are on a different platform, you have a different vendor, you have a different SDK, you have different APIs, you have a different architecture on the processor, possibly you have different GPIOs, all that. And it’s not going to work, at least easily.
Jamie
I don’t think it’s possible for us to downplay just how non-trivial it is to swap out hardware for a system that you’ve written in C or C++. My first bit of actual academic programming, I guess you’d use the phrase, when I was at college or whatever we were doing assembler on a Motorola 68k and this was part of - I’ve said it in other episodes as well - it was part of a development kit, a hardware development kit called a Flight 68k. So it was a Motorola 68k with loads of different bits plugged into it and you could swap them out, you could, “now I want the LEDs,” and then you take that out and then you’d say I don’t think it used GPIO. I can’t remember what he used, but he didn’t use that. And then you could plug in loads of different sensors.
And one of the tasks that I was given was calculate how fast a bullet travelled. They had to get a technician to build using an air rifle to build the hardware that I could plug into the board to measure the speed of the it was an air-soft bullet, but yeah, it was still a bullet, right? And that took a couple of days for them to put together, and I had made loads of assumptions about how it would work and where it would plug in and what the registers would be for my bit of code that would read this and the they presented me with this. Essentially it was a rifle glued to a piece of wood with two sets of crocodile clips that I could put sheets of tin foil in. I’m not sure what the cross country name of tin foil is, but just sheets of thin metal that I could put in between them and then I could replace those. But then they went through into individual cables that were bundled together that I had to then plug into the board and all of my code had to be rewritten. I’d say about 90% of my code had to be rewritten completely because I had assumed, “oh, it’ll be register one or it’ll be register two.” And it wasn’t. It was like I was messing with individual memory addresses. So I don’t think it can be underplayed just how much messing around is required when you’re writing in C, C++ or Assembler and the hardware changes.
José
Jamie
José
And you can even have some interaction with the devices. Meaning that you can have a firmware which is running the production test, which will be the very first application that will be deployed and run on your board. And you can do the usual stuff that you can find on QA electronic production line. You can see the LEDs blinking on some pattern. If you have a display, you’ll see those messages with the various tests that the system is running into. If it’s connected to the network, it will connect to some service and will get some reply. Everything will be tested with that application. Then you can just use the same command line to deploy the final application that will go into the field and deploy it in the real device. So you can do all that.
And speaking of professional tools, you have also on nanoFramework other tools that help you develop a better product and be sure on what you are doing. For example, we have a framework with unit tests, just like you have on the full .NET, meaning that if you want to get crazy with unit tests, which you should be, you can do that on our framework, and that’s available. If you want to do integration testing, you can do that also with a variation of the unit test that can run on the device. And we’ll take care of just the code, but the hardware interaction. We have libraries for doing benchmarking and logging. You can log to a file on SD card, you can log to a network service that you are connected to through sockets, you name it. All those advanced tools that you find on the full .NET and that you have on a desktop or on a cloud project. You have pretty much the same equivalents on nanoFramework to make sure that you are delivering a quality product and that your code is good, and that you can be as much sure as you can be on anything that is software.
Jamie
So then how do I get started? With .NET nanoFramework? Is it just go to the website and there’s maybe a “Hello, World” or something that I can copy down and figure out?
José
So basically you need two things: One, you fire up your Visual Studio, you go to the extension marketplace and you search for nanoFramework. It will show you the Visual Studio extension, which the name will be nanoFramework. Not very surprising. And you install that. With that you have all the debugging stuff that’s required for Visual Studio to work, and build, and deploy the project system and the templates. Because when you do File>New project, you’ll find under a C# nanoFramework folder, a class library template and standalone application template. And that one is exactly an “Hello, World,” which will give you a console write line Hello World from nanoFramework. And you can see that on the output pane on Visual Studio. And that’s it.
The second thing that you need, you have to install the nanoFramework Flasher, which is a CLI application. It’s a .NET tool, so you can run it everywhere that .NET runs. You install that with
dotnet tool install -g nanoff. And then you have a very simple command, which is kind of nanoff –update –platform ESP32 –port com11, which is wherever your ESP32 is connected to, and the the tool will connect to the device, will find out exactly what’s the series and the model, will grab the correct firmware and will flash it, and that’s it. On the next minute, you will have the device being discovered and listed on Visual Studio, and you can start coding with it for that “Hello, World” application. And that’s it. On five minutes, you are up and running.Jamie
José
Jamie
José
Jamie
José
Jamie
Wrapping Up
Thank you for listening to this episode of The Modern .NET Show with me, Jamie Taylor. I’d like to thank this episode’s guest, José Simões, for graciously sharing his time, expertise, and knowledge.
Be sure to check out the show notes for a bunch of links to some of the stuff that we covered, and full transcription of the interview. The show notes, as always, can be found at the podcast's website, and there will be a link directly to them in your podcatcher.
And don’t forget to spread the word, leave a rating or review on your podcatcher of choice - head over to dotnetcore.show/review for ways to do that - reach out via our contact page, or join our discord server at dotnetcore.show/discord - all of which are linked in the show notes.
But above all, I hope you have a fantastic rest of your day, and I hope that I’ll see you again, next time for more .NET goodness.
I will see you again real soon. See you later folks.
Useful Links
- .NET nanoFramework
- Eclo Solutions
- Zoom H4N
- HD60s+
- IoT Hub
- ESP32
- STM32
- GitHub Copilot
- Microsoft Learn Portal
- SPI (serial peripheral interface) bus
- .NET nanoFramework Visual Studio extension
- nanoFramework Flasher
- nanoFramework discord community
- Supporting the show:
- Getting in touch:
- Music created by Mono Memory Music, licensed to RJJ Software for use in The Modern .NET Show