# IoT for the .NET Developer

In this blog post, we are going to start the process of developing an Internet of Things (IoT) solution using .NET 7 on a Raspberry Pi.

IoT systems often run a lightweight Linux OS and leverage ARM hardware with low-power processors and have small amounts of RAM and limited storage. A typical IoT device will be designed around a very specific use case such as monitoring a sensor and sending measurements back to a cloud-based solution.

For example: imagine an underground aquifer that needs to be monitored. A solution could be based on a water level meter. The rest of the device would be a system on a chip and a 3/4G comms device that allows the device to post data to a cloud-based solution. This will be the use case that we explore in this series of blog posts and videos.

Traditionally .NET was not a popular choice to be used in a constrained environment like IoT devices. These devices would not be able to run the older .NET Framework as they would not be capable of running Windows. Even with .NET Core and .net 5/6 the devices would not have sufficient storage to have the full .NET Runtime installed, and the limited computing power and memory means that the Just In Time compilation for a typical .NET application would be impractical.

However.NET 7 and Native Ahead of Time (NativeAoT) compilation means that .NET can now be used as a development tool for these constrained devices.

For prototyping the solution I will use a Raspberry Pi 4 Revision B, however, any similar device should be able to be used. The Raspberry Pi has had a fresh copy of the OS installed, and the only changes to the default configuration are that the WiFi has been connected, and the system has been configured to allow SSH access to the Pi.

## Installing .NET

To install .NET 7 on the device, start by logging into the unit via SSH. In the PowerShell command prompt type the following command:

```bash

ssh {Username}@{DNS Name}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673142585328/59526d9a-33d9-4035-a5c5-9ff905d0e783.png align="center")

Once you have connected to the Raspberry Pi, you will have a standard bash shell that you can use to install .NET 7 from the command line. Installing .NET 7 can be done through the use of a Script that Microsoft provides.

Enter the following command at the SSH command shell:

```bash
curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --channel STS
```

The final portion of the command line specifies the channel that you will be using to get .NET from. This command will use the Short Term support channel, allowing you to download and install .NET 7. If you need to stay with only Long Term supported released, you can change the STS channel to LTS.

This command will automatically download the .NET SDK and extract the files.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673143247924/f790943a-1b5b-4a64-b55f-8eade867041f.png align="center")

Once the files have been installed, the final remaining step in getting .NET 7 set up and configured is to update your .bashrc file with the environment variables. Enter the following commands at the command prompt to update your environment.

```bash
echo 'export DOTNET_ROOT=$HOME/.dotnet' >> ~/.bashrc 
echo 'export PATH=$PATH:$HOME/.dotnet' >> ~/.bashrc 
source ~/.bashrc
```

Once you have completed these steps, you will have .NET 7 installed and ready to run. You can test the installation to make sure everything is working correctly by typing the following at the command prompt:

```bash
dotnet --version
```

You should see the following output:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673143587987/0b84573e-9ad5-46da-86c5-281ca13a4b90.png align="center")

From the command prompt type the following Commands:

```bash
cd Documents
mkdir src
cd src
mkdir DotNetTest
cd DotNetTest
```

Once you are in the DotNetTest folder, create a new .NET 7 console application by typing the following command:

```bash
dotnet new console
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673143905696/df8dd56e-3fea-47ed-83b5-02c5713d2691.png align="center")

## Setting up remote code editing:

To develop our .NET applications it would be helpful to be able to use Visual Studio Code from our local machine and edit the code remotely. Visual Studio code makes this very easy to accomplish.

Start Visual Studio Code on your local machine. When visual Studio stars, select the Extensions option on the left, then type "remote" in the search window.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673144327444/ba32a189-5d5d-41db-9431-c7d8d381a7c7.png align="center")

Install the "Remote - SSH" extension.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673144422183/55b7b784-854d-43ff-bdf4-245611764306.png align="center")

This extension will allow you to edit code on the Raspberry Pi remotely, from your laptop.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673144699753/92ff668e-bae7-4aa5-a22b-b747eca0af3c.png align="center")

Select Remote from the left-hand navigation panel. At the top of the Remote Explorer, make sure the option for 'Remote' is selected, and not 'Containers.' You will now be able to create a new connection to the Raspberry Pi from inside of Visual Studio Code.

Click the + button next to SSH, and enter the same details that you entered in when you connected via SSH to install .NET.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673144904900/f55f3fd6-98d5-44ec-8c98-bb30acb82a62.png align="center")

Select the default option to store your SSH configuration:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673145035971/259ba777-95e6-4d7d-a186-cda9eae6c0fe.png align="center")

You will then be prompted for the password to connect to the Raspberry PI. Enter in the password in order to connect. The first time you connect to the server, Visual Studio Code will install the server-side components onto the Raspberry Pi.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673145243096/ebbdfa29-f487-4a0d-8aea-32e3bf00e680.png align="center")

Once the installation has been completed, you can select the File Explorer option on the left navigation panel, then click Open Folder. You can then select from the available folders in the drop-down list shown. This is the folder list on the remote Raspberry Pi. Select Documents from the dropdown list.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673145331780/60ff8203-a569-4d6a-befd-ebec8c23b089.png align="center")

You may be required to confirm your password when connecting to a folder.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673145580757/37eddd3f-1d95-420a-bf2d-1841b43814bc.png align="center")

The first time you successfully connect you will be prompted to confirm rather or not you trust the authors of the files.

You can now browse and edit the source files on the remote machine from your laptop.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673145725433/32a7a8a1-55f4-44a2-9f02-d4743647c8f3.png align="center")

You can see here, the files that were created when we created the new console application earlier.

## Conclusion

Installing .NET 7 on the Raspberry Pi is a fairly straightforward process, that will allow you to start developing IoT applications using .NET 7. Once this has been completed you are now ready to move on to building an IoT solution.

I run through this process on my YouTube channel as a companion to this blog post. This video will guide you through the process visually. Check it out on the [Basement Programmer Youtube Channel](https://youtu.be/xdIvpPRLK9g)
