© Sibeesh Venu 2020
S. VenuAsp.Net Core and Azure with Raspberry Pi 4https://doi.org/10.1007/978-1-4842-6443-0_3

3. Setting Up the Prerequisites to Develop the Application

Sibeesh Venu1  
(1)
Birkenfeld, Germany
 

In the last few chapters, we were busy setting up the Raspberry Pi device. In this chapter, we concentrate on setting up our machine to create a custom application so we can deploy and run it in our Raspberry Pi. This chapter assumes that you have already set up your device. If you have not, read the previous two chapters.

We will be developing the application using .NET Core, for several reasons:
  • It is an open source, general-purpose development platform.

  • It can be used to create applications for Windows, macOS, Linux, and ARM64 processors.

  • It is widely supported and maintained by the community, so there are frameworks available for cloud, IoT, machine learning, and so on.

  • It supports multiple languages, such as C#, F#, C++, and Visual Basic .NET.

  • Good documentation is available to help users learn and use it.

In this chapter, I assume that you have enough knowledge on this topic. If you are not sure, read some of the articles at https://docs.microsoft.com/en-au/dotnet/core. You can also try some examples at https://github.com/dotnet/core. You can download the .NET Core from https://dotnet.microsoft.com/download.

It is worth mentioning that the Pi Zero and Pi 1 models are not supported. This is because the .NET Core JIT depends on armv7 instructions and they are not available in the earlier Pi versions.

Developing the Application

To develop the application, we are going to use Visual Studio code. Here are some reasons why I love Visual Studio Code (VSCode):
  • It’s lightweight and powerful.

  • It’s available for Windows, macOS, and Linux.

  • It has built-in support for JavaScript, TypeScript, and Node.js.

  • It has support for other languages, as provided by its wide varieties of extensions.

If you find any of these key points interesting, you can download this tool at https://code.visualstudio.com.

Using WSL

First, let’s discuss what Windows Subsystem for Linux (WSL) is. The key points of WSL are as follows:
  • It allows developers to run a GNU/Linux environment directly on Windows.

  • It includes many command-line-tools, utilities, and applications.

  • If there is no WSL, you have to create an additional Linux VM and run the programs there, which is a lot of work.

WSL vs. WSL2

The new version of WSL is WSL2. It has many advantages over the WSL version. WSL2 has better file system performance and it uses the latest virtualization technology. Since WSL2 uses a lightweight utility VM, it also uses less memory on startup.

To install WSL2, you should be running Windows 10 with build 19041 or higher (version 2004).

You can easily determine your Windows version by running the winver command in the command window (press the Windows key+R, and then type winver). You’ll see a screen similar to Figure 3-1.
../images/488238_1_En_3_Chapter/488238_1_En_3_Fig1_HTML.jpg
Figure 3-1

Windows 10 version

As you can see in Figure 3-1, my Windows version is 1909. I am using Surface Book 2, and the latest update is not yet compatible (as of writing this book).

Installing WSL

The optional feature, called Windows Subsystem for Linux, must be enabled before you can install any Linux distributions. To do that, run the following command in your PowerShell as the administrator:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
You should see the output shown in Figure 3-2.
../images/488238_1_En_3_Chapter/488238_1_En_3_Fig2_HTML.jpg
Figure 3-2

Enabling Windows Subsystem for Linux

If your system meets the criteria needed to install WSL2, you must enable the Virtual Machine Platform feature. You can do that by running the following PowerShell command:
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
You can also set WSL2 as your default version. This comes in handy when you install a new Linux distribution.
wsl --set-default-version 2
Don’t forget to restart your machine before you go to the next step. If you do not restart it, you will get the error shown in Figure 3-3.
../images/488238_1_En_3_Chapter/488238_1_En_3_Fig3_HTML.jpg
Figure 3-3

Optional component is not enabled

Installing the Linux Distribution

We are going to install the Linux distribution from the Microsoft Store. I recommend Ubuntu 20.04 LTS, as it is the latest version of Ubuntu. You can either open the https://aka.ms/wslstore URL in the browser, or search the Microsoft Store app and then search for Ubuntu (see Figure 3-4).
../images/488238_1_En_3_Chapter/488238_1_En_3_Fig4_HTML.jpg
Figure 3-4

Microsoft Store search result

Now click the Get button, as shown in Figure 3-5.
../images/488238_1_En_3_Chapter/488238_1_En_3_Fig5_HTML.jpg
Figure 3-5

Get Ubuntu

The system will start downloading Ubuntu. The file is around 500MB, so the download can take a few minutes, depending on your network speed. Once the installation is complete, you can click the Launch button in the Microsoft Store.

The initial installation may take some time and the command screen will ask you to enter your proposed UNIX username. This username doesn’t need to match your Windows username. You can be selective here.

After you set your username, you’ll need to enter a password. Make sure you enter the correct password when asked to retype it. Figure 3-6 is for your reference.
../images/488238_1_En_3_Chapter/488238_1_En_3_Fig6_HTML.jpg
Figure 3-6

Ubuntu setup screen

Boom, you have successfully set up Subsystem for Linux in Windows!

Setting Up the Connection to Raspberry Pi

In this section, you will do the following tasks:
  • Create a new SSH key.

  • Copy the public key to Raspberry Pi. If you don’t do this, you will be asked to type the password every time you deploy your app to Raspberry Pi.

  • Install the Visual Studio debugger on the Raspberry Pi, which is needed in the next step, as you will be deploying your application.

This section assumes that your Raspberry Pi is known as raspberrypi.local. You need to make sure that you can connect to the Raspberry Pi via SSH. If you are on Linux, just open a new Terminal and type the following command.

If you are on Windows, search for the keyword wsl in the Windows search box and then open the WSL Run Command to start a new Linux terminal command prompt. Once the prompt is opened, type the ssh [email protected] command to connect to your Raspberry Pi.

If you are using WSL1, you will receive this error:
ERROR: ssh: Could not resolve hostname raspberrypi.local: Name or service not known"
Note that this issue has been fixed in WSL2. To fix this error in WSL1, you have to change the hostname to the IP address of the Raspberry Pi. To get the IP address, run the ping command, as shown in Figure 3-7.
../images/488238_1_En_3_Chapter/488238_1_En_3_Fig7_HTML.jpg
Figure 3-7

Ping raspberrypi.local

As you can see, the ping command shows the IPV6 address, but we need the IPV4 address. To get that, run the same command with -4 on the end. Here is how the command will look:
ping raspberrypi.local -4
Figure 3-8 shows the results.
../images/488238_1_En_3_Chapter/488238_1_En_3_Fig8_HTML.jpg
Figure 3-8

Ping raspberrypi.local IPV4

Now change the hostname in the previous command to the IP address. The new command should look like this:
Next, update the Raspberry Pi OS and reboot it.
sudo apt update && sudo apt upgrade && sudo reboot
You should see the output shown in Figure 3-9.
../images/488238_1_En_3_Chapter/488238_1_En_3_Fig9_HTML.jpg
Figure 3-9

Updating the Raspberry Pi OS

We now need to generate a new SSH certificate and copy the public certificate to Raspberry Pi. To do that, we’ll run the previous command in the same command prompt. First make sure that you are in the current user’s home directory before you run the command. By default, the command prompt will open in the Windows System32 folder. It is not recommended you modify this folder. Figure 3-10 shows the results when we run the cd command.
../images/488238_1_En_3_Chapter/488238_1_En_3_Fig10_HTML.jpg
Figure 3-10

User’s home directory

Now you can run the command:
ssh-keygen -t rsa && ssh-copy-id [email protected]
You should see the output in Figure 3-11.
../images/488238_1_En_3_Chapter/488238_1_En_3_Fig11_HTML.jpg
Figure 3-11

Copy SSH to Raspberry Pi

To install the Visual Studio Code .NET debugger in Raspberry Pi, you simply run the following command. Keep in mind that you can use your Raspberry Pi name instead of the IP address if you are running WSL2.
ssh [email protected] "curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -r linux-arm -v latest -l ~/vsdbg"
You should see the output in Figure 3-12.
../images/488238_1_En_3_Chapter/488238_1_En_3_Fig12_HTML.jpg
Figure 3-12

Installing the debugger

Installing .NET Core on Ubuntu

Since we will be creating a .NET Core application, we need to install NET Core next. Go to the .NET Core download page at https://dotnet.microsoft.com/download and install the .NET Core SDK. If you are running WSL in Windows 10 like me, you need to install the SDK on the Windows WSL Linux distribution.

First, add the Microsoft package signing key to your list of trusted keys and add the package repository. To do that, run the following command at the WSL command prompt:
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
This command will ask you to type the user password. If you forgot the password, you can open a new command window and run the following command to open the WSL as a root user:
wsl --user root
Now type the following command to change the user password. You will be asked to type the new password:
passwd sibeeshvenu
Figure 3-13 shows this process.
../images/488238_1_En_3_Chapter/488238_1_En_3_Fig13_HTML.jpg
Figure 3-13

Changing the user password in WSL

Figure 3-14 shows the output of adding the Microsoft package signing key.
../images/488238_1_En_3_Chapter/488238_1_En_3_Fig14_HTML.jpg
Figure 3-14

Adding the Microsoft package signing key

Now you can install the .NET Core SDK by running the following command.
sudo apt-get update;
  sudo apt-get install -y apt-transport-https &&
  sudo apt-get update &&
  sudo apt-get install -y dotnet-sdk-3.1

If you don’t get any errors from the Terminal, you are good to go. Congratulations!

Summary

In this chapter, you learned about the following topics:
  • What VSCode is and why it is so popular.

  • What WSL is and why is it important.

  • How to enable WSL and the differences between WSL1 and WSL2.

  • How to install the Linux Distribution (Ubuntu) on Windows 10.

  • How to set up Raspberry Pi to connect with Ubuntu.

  • How to install .NET Core on Ubuntu.

Now take a deep breath and relax. You did well! It’s a good time to take a coffee break.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.166.98