Setting Up VS Code to Program Azure IoT Devices


Microsoft VS Code is a programmer's development environment designed to run on all the major desktop operating systems. However, VS Code has also evolved into a cross-compilation environment, which is to say one can write and compile code on a Windows PC, Linux, or a Mac and then upload the code to a different hardware platform, such as a RISC-V device, to name just one. Indeed, Platform IO is a set of VS Code extensions that eases code development for a wide range of different processor chips and hardware development boards. Platform IO is growing in popularity among manufacturers of cutting-edge chips and development boards, not to mention among developers and engineers specializing in the Internet of Things (IoT). As we shall see, some IoT development boards can make use of VS Code without requiring the full power of Platform IO. Often, we will be able to accomplish what we need with the simpler Arduino interface.

The Azure IoT Device Workbench

We are going to take a look at setting up VS Code for a subset of the broad IoT enterprise, in this case the development of IoT devices that will report data to, and be managed by, Microsoft Azure. A toolset that was once spread across many small VS Code extensions has now been integrated into a single extension called Azure IoT Tools. The point-and-click installation could not be easier.

We start the installation by clicking on the extensions icon in the left-edge menu. We then type "Azure IoT" into the filter box. The resulting list is large, and includes offerings from third-party vendors. Azure IoT Tools is at the top of the list, and despite the length of the list, this one extension includes virtually everything we need.

Opening the command palette through the View | Command Palette menu, or simply clicking F1, you can see the long list of Azure IoT Device Workbench commands. You can, if you like, provision an Azure IoT hub from VS Code rather than use the Azure portal.

Installing The Arduino IDE

To develop code for, say, an Arduino Uno microcontroller requires compilers, code libraries, linkers, debuggers and a host of other utilities collectively known as the toolchain. Whenever we begin to work with a new hardware platform the first task, and often a difficult one, is the installation and configuration of the appropriate toolchain. We will choose for our example the Microsoft AZ3166 microcontroller board since it is one of the easiest to work with if you are getting started with Azure IoT.

The simplicity of the Arduino Integrated Development Environment (IDE) led to great popularity for the Arduino microcontroller boards among both electronic engineers and avid hobbyists. This popularity encouraged manufacturers of alternative microcontrollers to make their products accessible via the Arduino IDE as well. As a result the Arduino IDE is no longer just for Arduino microcontrollers. The MS AZ3166 is an example of one such microcontroller. It comes with useful sensors built right in; developers and engineers can have their IoT device up-and-running in an hour. But how can we enjoy the simplicity of the Arduino development environment while also taking advantage of the professional power of VS Code? If you said "another VS Code extension", you've guessed it. We can install the Arduino IDE on our system with no intention of actually using it directly. The IDE installation process will install the tools we need for compiling and linking a microcontroller program. Once we have installed the Arduino IDE we can download the Arduino extension into VS Code. VS Code now becomes a development environment for any device that can be programmed with the Arduino IDE.

The Arduino Extension for VS Code

In addition to the compilers and linkers, the Arduino IDE also provides utilities to install toolchains for a wide variety of hardware development boards, above and beyond the traditional Arduino. After installing the VS Code Arduino extension, these same utilities are now accessible in VS Code. We will now look at the process by which the requisite AZ3166 tools are installed into VS Code using the Arduino extension.

Once we have installed the Arduino Extension we are, in principle, ready to work with any development board that provides Arduino IDE support. However, for any particular board there are some additional specific steps that will need to be completed.

Install the Necessary Drivers

You will have to identify and install the device drivers that will manage the communication between VS Code and your development board via a USB port. The required drivers will depend on both the development board you hope to work with and the operating system you use. If you wish to develop for the Microsoft AZ3166 board, you will need to download the driver from https://www.st.com/content/st_com/en/products/development-tools/software-development-tools/stm32-software-development-tools/stm32-utilities/stsw-link009.html#tools-software

Preference Settings

Just as with the Arduino IDE, you must provide a url in the Preferences | Settings that will tell other tools, like the Board Manager, where to find and download necessary tools. There is one important difference, however. The Arduino IDE maintains preference settings just for itself. VS Code must maintain settings for virtually any extension you may wish to run. We therefore must entire the Arduino preference settings as one set among potentially many in VS Code's settings.json file. This is accessed through the menu system File | Preferences | Settings. To open the settings.json file we must click one more button, however, which is not immediately obvious.

We then paste the necessary information between the parentheses. If there is already settings information for other extensions, you must take care not to break the json structure when you paste the text.

"arduino.path": "C:\\Program Files (x86)\\Arduino", "arduino.additionalUrls": "https://raw.githubusercontent.com/VSChina/azureiotdevkit_tools/master/package_azureboard_index.json"

The arduino.path specification will be different for the Mac and for Linux. In addition, it might be different on windows if you did not install the Arduino IDE as administrator.

When you are finished pasting, your file should look something like this:

The Board Manager

Once the Arduino preferences have been set, we can load the information specific to our development board. Once again, the process is similar for any board, but we are using the AZ3166 board as our example

Clicking F1 or <ctrl><shift>P brings up the VS Code command palette. Starting to type "Arduino" brings commands starting with "Arduino:" to the top of the list. We choose Arduino: Board Manager. Once the Board Manager window has appeared, we can start typing in the filter box. Typing "az" should be sufficient.

When we have found the "MXChip - Microsoft Azure IoT Developer Kit by Microsoft Developer", we note the latest version (2.0.0 as of this writing) and install the tools for the AZ3166 developer board.

We're Ready!

The tools are now set up and the fun of developing an IoT device can begin!

Dan Buskirk

Written by Dan Buskirk

The pleasures of the table belong to all ages.” Actually, Brillat-Savaron was talking about the dinner table, but the quote applies equally well to Dan’s other big interest, tables of data. Dan has worked with Microsoft Excel since the Dark Ages and has utilized SQL Server since Windows NT first became available to developers as a beta (it was 32 bits! wow!). Since then, Dan has helped corporations and government agencies gather, store, and analyze data and has also taught and mentored their teams using the Microsoft Business Intelligence Stack to impose order on chaos. Dan has taught Learning Tree in Learning Tree’s SQL Server & Microsoft Office curriculums for over 14 years. In addition to his professional data and analysis work, Dan is a proponent of functional programming techniques in general, especially Microsoft’s new .NET functional language F#. Dan enjoys speaking at .NET and F# user’s groups on these topics.