# Visual Studio Code

For advanced developers and more complex projects, an advanced IDE can be beneficial. Visual Studio Code (VS Code) offers features like GIT control, code completion, and a variety of useful extensions to streamline the development process. By combining VS Code with PlatformIO, a powerful setup for Thinger.io projects is obtained.

**Benefits of Using Visual Studio Code with PlatformIO:**

* **GIT Control**: Version control can be easily managed with integrated GIT support.
* **Code Completion**: Enjoy enhanced code completion for faster and more accurate coding.
* **Extensions**: Access a wide range of extensions to add functionality and improve productivity.

To get started, download and install Visual Studio Code and the PlatformIO extension.

## Install Visual Studio Code

**Visual Studio Code** is a free source-code editor developed by Microsoft for Windows, Linux and macOS. It can be extended via [extensions](https://en.wikipedia.org/wiki/Plug-in_\(computing\)), available through a central repository to add language support, new programming languages, [themes](https://en.wikipedia.org/wiki/Theme_\(computing\)), and [debuggers](https://en.wikipedia.org/wiki/Debugger), or perform [static code analysis](https://en.wikipedia.org/wiki/Static_code_analysis). It can be downloaded for free from the official website.

&#x20;[**Download Visual Studio Code**](https://code.visualstudio.com/download)

![Visual Studio Code](https://2241973068-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LpXqB3J1BMD5s4OpYSg%2F-M89ku-fm6SCl7vMSY1_%2F-M8A0fPMgP3DvdsrYMo9%2Fimage.png?alt=media\&token=46efeb59-fb73-4e37-bf13-2100757d6644)

## **Install PlatformIO**

PlatformIO is a cross-platform, cross-architecture, multi-framework professional tool for embedded systems engineers and software developers working on embedded products. It can be installed as an extension in Visual Studio Code.

**Steps to Install PlatformIO:**

1. **Open Extensions in Visual Studio Code**:
   * Press `Ctrl + Shift + X` on Windows or `Command + Shift + X` on Mac to open the Extensions view.
2. **Search for PlatformIO**:
   * In the Extensions view, type "PlatformIO" in the search bar.
3. **Install PlatformIO**:
   * Click on the **PlatformIO IDE** result.
   * Click the **Install** button.

Once installed, PlatformIO provides powerful features to enhance the development process for Thinger.io projects.

![](https://2241973068-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LpXqB3J1BMD5s4OpYSg%2F-M89ku-fm6SCl7vMSY1_%2F-M8A0qZzA1HSL-WwMJFE%2Fimage.png?alt=media\&token=4a427372-4bdb-4db8-8d17-cc0aa9e4ac81)

## Starting a Project

Once Visual Studio Code with PlatformIO is installed, it is possible to create a new project for our specific board. For this purpose, we can access the PIO Home and click on the `New Project` button:

![Create a new Project from PIO Home.](https://2241973068-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LpXqB3J1BMD5s4OpYSg%2F-MjTJt_RZbl-di6c3M9c%2F-MjVGv3WM7vDHwmVhzNq%2Fimage.png?alt=media\&token=dd3320c6-87b8-4547-9572-a36d62e834d3)

For this example, we will be using the ESP32 board, so in the `Project Wizard` pop-up, we enter a `Project Name`, select the `Espressif ESP32 Dev Module`, as a generic ESP32 board, and the `Arduino` Framework. Once done, click on `Finish` and wait for PlatformIO to download the required toolchains for the device.

![PlatformIO project Wizard](https://2241973068-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LpXqB3J1BMD5s4OpYSg%2F-MjTJt_RZbl-di6c3M9c%2F-MjVH99PHfyC7w8xrXcC%2Fimage.png?alt=media\&token=bf48da14-f32c-4a5f-88fa-71e6d524eb64)

After the project initialization is done, PlatformIO generates a file structure:

![PlatformIO default project structure](https://2241973068-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LpXqB3J1BMD5s4OpYSg%2F-MjTJt_RZbl-di6c3M9c%2F-MjVKnzeuV3nQS-rWWdn%2Fimage.png?alt=media\&token=cfd2cae5-bf6b-402f-a588-fd049fdec068)

As shown in the above picture, each PlatformIO project has a configuration file named `platformio.ini` in the root directory of the project. This is an INI-style file.

`platformio.ini` has sections (each denoted by a `[header]`) and key/value pairs within the sections. Lines beginning with `;`are ignored and may be used to provide comments.

In our default `ESP32` project:

```bash
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
```

Now, to start working with Thinger.io, it is required to add the Thinger.io client library using the `lib_deps` property:

```bash
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps = thinger.io
```

After this configuration is done, it is possible to start compiling for our device. A basic example for our ESP32 device:

{% tabs %}
{% tab title="main.cpp" %}

```cpp
#define THINGER_SERIAL_DEBUG

#include <ThingerESP32.h>
#include "arduino_secrets.h"

ThingerESP32 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() {
  // open serial for debugging
  Serial.begin(115200);

  pinMode(16, OUTPUT);

  thing.add_wifi(SSID, SSID_PASSWORD);

  // digital pin control example (i.e. turning on/off a light, a relay, configuring a parameter, etc)
  thing["GPIO_16"] << digitalPin(16);

  // resource output example (i.e. reading a sensor value)
  thing["millis"] >> outputValue(millis());

  // more details at http://docs.thinger.io/arduino/
}

void loop() {
  thing.handle();
}
```

{% endtab %}

{% tab title="arduino\_secrets.h" %}

```cpp
#define USERNAME "your_user_name"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"

#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_ssid_password"
```

{% endtab %}
{% endtabs %}
