Passwordless login to remote RPI from VSCODE with public / private keys

Steps to Set Up SSH Key Authentication Between Windows and Raspberry Pi (So You Don’t Have to Enter the Password)

  1. Generate SSH Keys on Windows (If Not Done Already)

You should have generated the SSH key pair on your Windows machine (the one you’re using for VSCode).

Open PowerShell (or Command Prompt) on your Windows machine.
Run the following command to generate an SSH key pair:

ssh-keygen -t rsa -b 4096

Follow the prompts:
    Press Enter to accept the default location for the key (C:\Users\<YourUserName>\.ssh\id_rsa).
    Optionally, enter a passphrase, or leave it blank for no passphrase (easier but less secure).

Now you should have two files in your C:\Users\.ssh directory:

id_rsa (your private key)
id_rsa.pub (your public key)
  1. Copy the Public Key to the Raspberry Pi

You need to copy the public key (id_rsa.pub) to the Raspberry Pi so that it can authenticate you.
Option 1: Using ssh-copy-id (from Windows with Git Bash)

If you have Git Bash installed on your Windows machine, you can use the ssh-copy-id tool to copy the public key to the Raspberry Pi:
    Open Git Bash on Windows.
    Run the following command:

    ssh-copy-id pi@<RPI_IP_ADDRESS>

    Replace <RPI_IP_ADDRESS> with the actual IP address of your Raspberry Pi.
        It will prompt you for the Raspberry Pi password. After entering it, your key will be copied.

Option 2: Manually Copy the Key

If ssh-copy-id is not available, follow these manual steps:

Copy the public key:
    Open id_rsa.pub (located in C:\Users\<YourUserName>\.ssh\id_rsa.pub) with a text editor, and copy its entire content.

Log into the Raspberry Pi:
    Open PowerShell or Command Prompt on Windows, and run:

ssh pi@<RPI_IP_ADDRESS>

Enter the Raspberry Pi password when prompted.

Paste the public key into the authorized_keys file:

On the Raspberry Pi, run:

mkdir -p ~/.ssh
nano ~/.ssh/authorized_keys

Paste the contents of id_rsa.pub into the authorized_keys file.
Save and exit (Ctrl + X, then press Y to confirm, and Enter).

Set the correct permissions:

Run the following commands on the Raspberry Pi to ensure the permissions are correct:

    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys
  1. Configure SSH to Use Key Authentication (on the Raspberry Pi)

Now, ensure that the SSH server on the Raspberry Pi is configured to allow key-based authentication and disable password authentication (if you want to fully rely on SSH keys).

Edit the SSH configuration file on the Raspberry Pi:

sudo nano /etc/ssh/sshd_config

Ensure the following lines are present and not commented out (# means commented out):

PubkeyAuthentication yes
PasswordAuthentication no (This line will disable password authentication, forcing key-based login)
AuthorizedKeysFile .ssh/authorized_keys

Restart the SSH service to apply changes:

sudo systemctl restart ssh
  1. Test the SSH Connection

Test that SSH key authentication is working by attempting to log in again from your Windows machine:

Open PowerShell or Command Prompt on your Windows machine.

Run the following command:

ssh pi@<RPI_IP_ADDRESS>

You should now be able to log in without a password prompt. If everything is set up correctly, SSH should use the key authentication, and you'll be logged in automatically.
  1. Set Up VSCode to Use SSH

Now that SSH key authentication is working, you can connect from VSCode:

Install the Remote - SSH extension in VSCode if you haven't already.
Configure the SSH connection in VSCode:
    Open Command Palette (Ctrl + Shift + P) in VSCode.
    Type Remote-SSH: Open SSH Configuration File.
    Add an entry for your Raspberry Pi:

    Host raspberrypi
        HostName <RPI_IP_ADDRESS>
        User pi
        IdentityFile C:\Users\<YourUserName>\.ssh\id_rsa

Connect to the Raspberry Pi:
    Open Command Palette (Ctrl + Shift + P) in VSCode again.
    Select Remote-SSH: Connect to Host... and choose your Raspberry Pi entry.

Now, VSCode should connect to your Raspberry Pi without asking for a password each time you reboot your Raspberry Pi.

  1. Troubleshooting If you’re still being asked for a password, check that:
    The private key (id_rsa) is being correctly used.
    The Raspberry Pi’s SSH server is configured to allow key-based authentication (PubkeyAuthentication yes).
    The authorized_keys file on the Raspberry Pi has the correct permissions (chmod 600 ~/.ssh/authorized_keys).

With this setup, you should no longer be prompted for a password after rebooting the Raspberry Pi!

How to use VSCODE remote debuging on a raspberry pi 3 without OS crashes

You will often encounter OS crashes when using VSCODE on a rpi3 for debugging. The cause: memory overflow and too low swap size. If you set the swap memory to 1024MB you will have solved the problem.

Follow these steps (source https://nebl.io/neblio-university/enabling-increasing-raspberry-pi-swap/) ->

1. Temporarily Stop Swap:

sudo dphys-swapfile swapoff

2. Modify the size of the swap

As root, edit the file /etc/dphys-swapfile and modify the variable CONF_SWAPSIZE to 1024:

CONF_SWAPSIZE=1024

Using a command such as:

sudo nano /etc/dphys-swapfile

3. Initialize Swap File

sudo dphys-swapfile setup

4. Start Swap

sudo dphys-swapfile swapon

Hvordan kompilere eldre .NET / C# prosjekter fra Visual Studio i VSCODE (notes to self)

Må ha support for C# utvikling installert i VSCODE. Hvis det er et eldre prosjekt som man henter opp som er laget i Visual Studio så må man ha C# Dev kit installert siden det støtter MSBUILD projects osv. 
https://code.visualstudio.com/docs/languages/csharp

###

Må sjekke at ikke det er feil versjon av dotnet som kjøres med kommando dotnet --info
Installering i et CMD vindu kan skje med winget: https://learn.microsoft.com/en-us/dotnet/core/install/windows?tabs=net70

###

X86 path kan være fær X64 path. Rekkefølgen på path prioriteres
Sjekk PATH ! IKke bare PATH for ALLE brukere, men sjekk path for DIN bruker 
https://github.com/dotnet/core/issues/5962#issuecomment-780084535 MRK denne tutorialen fokuserer på SYSTEM PATH men det var YOUR ACCOUNT PATH som var feil 
Hvis path peker til x86 versjonen av dotnet mens du har installert dev packs osv for X64 versjonen av dotnet så får du feilmeldinger i VSCODE
Må ha core dev installert
List hva som er installert med "dotnet --list-sdks" 

###

Må modde .csproj fila i det opprinnelige prosjektet for å få kopmpilert med VSCODE
https://github.com/dotnet/msbuild/issues/5787

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net48</TargetFramework>
    <GenerateResourceUsePreserializedResources>true</GenerateResourceUsePreserializedResources>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Resources.Extensions" Version="5.0.0" />
  </ItemGroup>

</Project>


###

Måtte konvertere .csprj fila til nytt format som VSCODE ville godta (fikk build feilmelding) 
Brukte dette toolet på kommandolinje: https://treit.github.io/c%23,/programming/2019/02/18/ConvertingCsProjectsToNewSdkFormat.html

dotnet tool install --global Project2015To2017.Migrate2017.Tool
conv HelloWorld.csproj

Litt mer info om konvertering til nytt CSPROJ format: https://www.hanselman.com/blog/upgrading-an-existing-net-project-files-to-the-lean-new-csproj-format-from-net-core

How to convert binary fractions to decimal ( fixed point arithmetics )

Example for n = 110.101

Step 1: Conversion of 110 to decimal
=> 1102 = (1*22) + (1*21) + (0*20)
=> 1102 = 4 + 2 + 0
=> 1102 = 6
So equivalent decimal of binary integral is 6.

Step 2: Conversion of .101 to decimal
=> 0.1012 = (1*1/2) + (0*1/22) + (1*1/23)
=> 0.1012 = 1*0.5 + 0*0.25 + 1*0.125
=> 0.1012 = 0.625
So equivalent decimal of binary fractional is 0.625

Step 3: Add result of step 1 and 2.
=> 6 + 0.625 = 6.625
Example for n = 4.47 k = 3

Step 1: Conversion of 4 to binary
1. 4/2 : Remainder = 0 : Quotient = 2
2. 2/2 : Remainder = 0 : Quotient = 1
3. 1/2 : Remainder = 1 : Quotient = 0

So equivalent binary of integral part of decimal is 100.

Step 2: Conversion of .47 to binary
1. 0.47 * 2 = 0.94, Integral part: 0
2. 0.94 * 2 = 1.88, Integral part: 1
3. 0.88 * 2 = 1.76, Integral part: 1

So equivalent binary of fractional part of decimal is .011

Step 3: Combined the result of step 1 and 2.

Final answer can be written as:
100 + .011 = 100.011

RTOS on Arduino

FreeRTOS Task for Blink LED in Arduino UNOProfessional software developers of time critical code often use a real time operating system (RTOS) in their work. The reason is that the OS is taking care of timing and scheduling tasks with higher priority. Now you can easily use an RTOS on your Arduino as well! Read Jain Risbah’s article over at circuitdigest: https://circuitdigest.com/microcontroller-projects/arduino-freertos-tutorial1-creating-freertos-task-to-blink-led-in-arduino-uno

How to check for a NAN (Not A Number) in C++ in embedded systems without exception handlers

Sometimes there are bugs or special corner conditions that makes a NAN (Not A Number) occur in code compiled from a C++ source and executed on an embedded systems without memory to run exception handlers.

Here is what @Jalf writes over at StackExchange:

According to the IEEE standard, NaN values have the odd property that comparisons involving them are always false. That is, for a float f, f != f will be true only if f is NaN.

Note that, as some comments below have pointed out, not all compilers respect this when optimizing code.

For any compiler which claims to use IEEE floating point, this trick should work.

If you inplement a function in a separate .cpp file , how do you access objects instantiated in main from functions implemented that file?

Sometimes there is a need to spread code into several .cpp files to avoid clutter (for example main.cpp, other1.cpp, other 2.cpp) . So if you implement a function in a separate .cpp file, how do you access objects instantiated in main?

Solution: use extern

other1.cpp:

extern ObjectType objectname

void doSomething (void) 
{
      objectname.method();
}

Of course this is very basic stuff, but many new programmers ask about this so I included a short post about this here.

How to develop and debug C++ code for Arduino in Visual Studio if you are tired of the Arduino IDE

The Arduino platform has gained a fantastic popularity over the past ten years. For small quick and dirty projects, the .ino files and the standard IDE is OK. However, for professional development projects and for developers that want control over the .hpp and .cpp files the standard Arduino IDE is somewhat regarded like a toy. Furthermore there is no proper debugger in the Arduino IDE  (whaaat?, you gotta be kidding?)

Well, this has changed as Visual Micro has developed a plugin to Microsoft Visual Studio. You can write code the normal way you do it with .cpp and .hpp files. You can also run the GDB debugger. The IDE has support for the regular .INO files and Arduino libraries. Here is how both .cpp and .ino files are handled: http://www.visualmicro.com/page/User-Guide.aspx?doc=INOs-and-CPPs.html

There is a free version of Visual Micro and it works against the free versions of Microsoft Visual Studio 2017 Community Edition. All you need is a Microsoft account to be able to download and install Visual Studio 2017 Community edition. Then in MSVC 2017, go to tools, Extensions and Updates and enter a search for Arduino in the search bar. It will will offer to automatically install the Arduino tools and the GDB debugger. Click install and you are on the right path.

Links:
http://www.visualmicro.com/
https://marketplace.visualstudio.com/items?itemName=VisualMicro.ArduinoIDEforVisualStudio

If you find this tip useful, please share on facebook and share the link. Also, feel free to report your experiences in the comment field.