Raspberry Pi and Visual Studio Code: A great combination: Raspberry Pis are everywhere. From the tiny new Pico microcontroller to the low-cost desktop PC that’s the Raspberry Pi 400, the ARM-based single board computer is a powerful tool that works as well in education as it does as an IoT device.
In a previous video, I had posted instructions on installing Visual Studio Code on the Pi. That is nolonger necessary; you can now grab the ARM binaries stra. Visual Studio Code is totally free but until recently was not available in an official version for the Raspberry Pi. You can find a version in the GitHub projects of Steve Desmond for version 1.28.2, but VSC is already on version 1.50.1 today.
The Raspberry Pi foundation has published a new article to its blog today, providing information on how to code remotely using your Raspberry Pi and the Visual Studio Code application. Earlier this month a Raspberry Pi supported version of the excellent Visual Studio Code software was made available for the Raspberry Pi mini PC by Microsoft.
If you like to run your Raspberry Pi as a ‘headless’ device to control various electronics, the new article provides everything you need to know about coding your Pi mini PC remotely using VS Code. Ashley Whittaker explains more over on the official Raspberry Pi blog.
“Visual Studio Code, or VS Code, is a free, open source, developer’s text editor with a whole swathe of extensions to support you coding in multiple languages, and provide tools to support your development. I practically live day to day in VS Code: whether I’m writing blog posts, documentation or Python code, or programming microcontrollers, it’s my work ‘home’. You can run VS Code on Windows, macOS, and of course on a Raspberry Pi.
One of the extensions that helps here is the Remote SSH extension, part of a pack of remote development extensions. This extension allows you to connect to a remote device over SSH, and run VS Code as if you were running on that remote device. You see the remote file system, the VS Code terminal runs on the remote device, and you access the remote device’s hardware. When you are debugging, the debug session runs on the remote device, but VS Code runs on the host machine.”
For full instructions on how to set up your Raspberry Pi for coding remotely with Visual Studio Code follow the link below.
Source : Raspberry Pi Foundation : VSC
Filed Under: DIY Projects, Top NewsLatest Geeky Gadgets Deals
Disclosure: Some of our articles include affiliate links. If you buy something through one of these links, Geeky Gadgets may earn an affiliate commission. Learn more.
We’re back with our LTM (Learning through Making) series of Node.js tutorials and we’re gearing up and getting ready to write some code! We’ve learned how to build a Raspberry Pi from the ground up including Node.js, we’ve created a web server in Node without code, and we’ve even managed to get this web server on the Internet as a cool trick.
So how are we going to write Node.js code? There’s nothing that would stop us from simply jumping onto our RasPi and using the Leafpad text editor or even the nano console-based editor to write our code. I propose that we use some more robust tools in the form of an IDE (Integrated Development Environment) to help us along in our coding journey.
It turns out that the RasPi 2 and RasPi 3—in spite of the significant CPU/memory boost they offer over their predecessors—will run most IDEs a bit sluggishly and will be frustrating for any serious Node.js work. I offer here a creative alternative we will use to expedite the software development lifecycle and run the Node.js code natively on the Pi.
In this tutorial, we’re going to use Visual Studio Code for doing Node.js development. Don’t worry if you have another preferred IDE such as Sublime Text or Atom as these steps will be generally applicable. I’m approaching this from the Windows platform, but I believe these steps can be adapted for OS X and Linux platforms as well.
The solution is conceptually simple. We will create a network file share on the RasPi and map the drive on the Windows machine so we can harness the computing power of our Windows machine to write the code. When we are done writing the code, we can then run it natively on either the Windows machine or the RasPi. We’ll see the beauty and versatility of cross-platform JavaScript in action! Let’s get started.
Create Windows File Share on the RasPi
This is one of the steps we completed as part of my Beginner’s Guide to Installing Node.js on a Raspberry Pi so please reference that article if you want a comprehensive guide on the entire RasPi installation.
We will create a Windows file share on the RasPi so we can use our Node.js IDE from our Windows machine directly. Here’s how we do it:
- Launch a terminal session.
- Create a directory called “share” in your home directory as follows:
- Issue the following command to launch the Leafpad editor.
- We could have also used the console-based nano editor, but Leafpad provides a GUI which is more suitable for beginners. The ampersand on the end of the command launches Leafpad as a background process so we are returned to a shell prompt in the terminal. Also, the smb.conf is part of the Samba package we installed early and this is why it exists on our system.
- Scroll down to the very bottom of the smb.conf configuration file and add the following:
- Save the changes to the smb.conf configuration file and exit the Leafpad editor. (Raspbian will “automagically” restart the SMB (Samba) services based on the changes you made to the configuration file.)
- Back in a terminal session, we need to create an SMB (Samba) user so we can connect securely from Windows. We will create a user named “pi”, but this will be an SMB (Windows user) rather than the Rasbian “pi” user. Here’s how we do it:
You will be prompted to enter and re-enter a password. Document this password somewhere for future reference.
We are ready to connect to our newly created RasPi file share from Windows!
- Back on your Windows machine, type the Windows key and “R” (Win-R) to launch a run box for typing in commands.
- Enter two backslashes followed by the name of your RasPi machine (i.e. your Hostname) and hit enter. For example:
- When prompted for credentials, enter the following:
- Username: The format to enter here in Windows parlance is the
domainusername
. For us, our domain will be our RasPi hostname and our user name will be “pi”. Therefore, we will enter:raspipi
(unless you named your RasPi hostname differently.)
If your Hostname is not raspi, you will obviously enter that instead. - Password: (enter the password you created with the smbpasswd command above)
- Username: The format to enter here in Windows parlance is the
- Voilà! We should now be connected. You can test the file share by creating a text file on the Windows side and verifying that it shows up in the /home/pi/share directory on the RasPi side.
Map A Network Drive to the File Share Hosted on the RasPi
Very nice – we have connectivity to the RasPi file share. We will now map a drive to facilitate development so we don’t have to specify the full UNC path each and every time. Feel free to choose a drive letter of your choice. I’m going to choose “P” since the drive we are mapping resides on a “Pi” after all. I like to keep things simple. 🙂
To map a drive, launch Windows Explorer. As one way to do this, use the Windows key on your keyboard to do a Win+E keyboard combination to launch Windows Explorer.
Next, click on the “Map network drive” icon as shown here:
You will then be presented with the following dialog box:
Visual Studio Code Raspbian
As shown in the screenshot, change the drive letter to “P” or other drive letter of your choice. Enter the UNC path to the RasPi network file share such as “raspiPiShare” . Check the “Reconnect at sign-in” checkbox to ensure the drive is mapped every time you log into Windows. Don’t worry if your RasPi is not always on since Windows will simply keep the icon ready to use when your RasPi is available.
If you are prompted for credentials, remember that the user name is in the format of domainusername
. For us, our domain will be our RasPi hostname and our user name will be “pi”. Therefore, we will enter: raspipi
for the user name (unless you named your RasPi hostname something different.)
You should now be able to double click on your “P” drive in Windows Explorer to launch the Pi network file share. Operating system interoperability amazes me every time; Samba is our friend!
Visual Studio Code Raspberry
Install Visual Studio Code on Laptop/Desktop Machine
Your next step is to install Visual Studio Code. I have written the Visual Studio Code Jumpstart for Node.js Developers article to assist you. Come back here when you are done with those steps. I’ll be waiting. 🙂
Use Visual Studio Code to Write Node.js Code
You can now launch Visual Studio Code on your Windows (or other) desktop/laptop machine and write some code. We’ll be expounding on this a great deal in future tutorials. For now:
- Create a folder called
mytest
on the P drive. - In the
mytest
folder, create a file calledindex.js
with the following contents:
Save your code and run it from the Windows command prompt:
You should see “Hello world!” printed at the console. This is a bit unimaginative, but it is a good test!
Run Node.js Code on the RasPi
Next, if it is not already open, initiate a remote desktop connection to your RasPi via XRDP (described in my Beginner’s Guide to Installing Node.js on a Raspberry Pi). Launch a terminal session and type the following:
You should see “Hello world!”. We have success!
Conclusion
Using this workflow, you will have the full power of your laptop/desktop at your disposal for doing Node.js development work. You can then run your Node.js code natively from either your laptop/desktop or on the RasPi itself. There will be some cases where you will use Node.js native npm modules to read/write from GPIO pins on the Raspberry Pi and these, of course, will only run from the Pi.
That’s it for now. There will be more exciting tutorials on the way!
Visual Studio Code Raspberry Pi 4
Follow Dave Johnson on Twitter to stay up to date on the latest tutorials and tech articles.