# Getting Started

The easiest way to prototype a device on Blynk and correctly configure the integration is to use a device that is already [connected to The Things Stack](https://www.thethingsindustries.com/docs/devices/adding-devices/). Once the device is connected and successfully goes online, [create a new template on Blynk](https://docs.blynk.io/en/getting-started/template-quick-setup) for this device. Once you have created a new template, navigate to the **Developer Zone -> Integrations -> The Things Stack**. Click the "**+ Add**" button and select the template you previously created.

<figure><img src="https://1839001309-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MBFTVMf7L6S67HOuqVC%2Fuploads%2FGYgJTIlKp6OMgZZxemQG%2Fadd-application.png?alt=media&#x26;token=7b730afd-ca73-46bf-b69f-27e40e64e805" alt="" width="563"><figcaption><p>Create a new The Things Stack Application on Blynk</p></figcaption></figure>

In the application settings, fill in The Things Stack hostname, username, and password. To [obtain the credentials](https://www.thethingsindustries.com/docs/integrations/mqtt/#creating-an-api-key), navigate to the application page you wish to connect on The Things Stack. Click on the **Integrations** button in the left sidebar and select **MQTT**. Copy the Public TLS address under the MQTT server host section and paste it into Blynk (if you are going to use non-TLS MQTT address, change the switch to "**Insecure**"). Then, copy the username from the page and paste it into Blynk. After that, click on the "**Generate new API key**" button, copy the password, and paste it into Blynk.

<div data-full-width="false"><figure><img src="https://1839001309-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MBFTVMf7L6S67HOuqVC%2Fuploads%2FrDKfQdsqtH5DmXKmrQyt%2F%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202024-07-18%20%D0%B2%2016.36.27.png?alt=media&#x26;token=cdd04d3c-f750-4fc8-9b69-4e9f4a1893bf" alt=""><figcaption><p>Capturing the credentials from The Things Stack</p></figcaption></figure></div>

Click the "**Connect**" button at the bottom of the application card on Blynk. You will see the integration status change to "**Connected**".

{% hint style="info" %}
At this point, the metadata for *TheThingsNetwork Device* will be created in the template if it wasn't created before. Blynk uses this metadata to identify each device in The Things Stack application.
{% endhint %}

<figure><img src="https://1839001309-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MBFTVMf7L6S67HOuqVC%2Fuploads%2FlWtDP8pTBMJ4SMm0SD9Q%2Fthe-things-network-application-connected.png?alt=media&#x26;token=ed5676a4-b05b-44af-b12a-3a25b55a7d60" alt="" width="563"><figcaption><p>The Things Stack Integration Page with a Connected Application</p></figcaption></figure>

Once this is done, Blynk will automatically create the device connected to The Things Stack as soon as it sends any data. This device will then be available on the [Devices page](https://docs.blynk.io/en/blynk.console/devices).

## Sending uplink and downlink messages

Once your first device is connected to Blynk, it's time to transfer some data! Start by configuring [the Payload formatter on The Things Stack](https://www.thethingsindustries.com/docs/integrations/payload-formatters/).

{% hint style="danger" %}
If you are using any provided The Things Stack Payload Formatter, such as a Payload Formatter imported from the The Things Stack Device Repository, additional steps might be required to convert the data format into the format required by Blynk.
{% endhint %}

### Uplink

For uplinks, Blynk requires the data to be in *key-value* pair format, where the *key* is either the datastream name or pin (such as `v1` or `a10`) and the *value* is the datastream value. The value type must match the datastream value type! Also, prefer using only Virtual Pin, Enum and Location datastream types. Here is an example of the decoded uplink:

```json
{
  "temperature": 24.1,
  "humidity": 54,
  "idle": false,
  "state": "Working",
  "location": [-73.935242, 40.730610]
}
```

In this example, Blynk will set the datastream with the name "temperature" to the value 24.1 and the datastream with the name "humidity" to the value 54.

{% hint style="info" %}
Since Blynk does not support a boolean datastream value type, you must use either the Integer or Double datastream value type. The value `true` will be represented as `1`, and `false` as `0`.
{% endhint %}

The datastreams configuration for this example may look like this:

<figure><img src="https://1839001309-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MBFTVMf7L6S67HOuqVC%2Fuploads%2FGVvIC5CmjuYG5VX2aqEp%2F%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202024-07-18%20%D0%B2%2016.37.27.png?alt=media&#x26;token=024a164a-a33d-4d87-b99c-0ca8106fa610" alt=""><figcaption><p>The datastreams configuration for the uplink and downlink examples</p></figcaption></figure>

Refer to the [Datastream documentation](https://docs.blynk.io/en/blynk.console/templates/datastreams) to create datastreams.

{% hint style="info" %}
Use the ['Log when device reports any data'](https://docs.blynk.io/en/blynk.console/templates/connection-lifecycle#log-when-device-reports-any-data) feature in Lifecycle to mark the device as online with each uplink received from The Things Stack.
{% endhint %}

{% hint style="success" %}
Additionally, refer to the [System DataStreams](https://docs.blynk.io/en/hardware-guides/the-things-stack/system-datastreams) section, which provides details on how to retrieve RSSI, SNR, and Firmware Version information.
{% endhint %}

### Downlink

For each datastream update via the web dashboard, mobile application, automation, etc., Blynk will send a separate downlink to the device. Here is an example of the downlink that Blynk will send to the device, indicating that the datastream with the name "speed" (pin `v2`) has been updated to the value `3`:

```json
{
  "name": "speed",
  "pin": "v2",
  "ts": 1721202415068,
  "value": 3
}
```

The `ts` field represents the timestamp in Unix milliseconds format when the value was actually updated.

## Application Statuses

Each application has its own status, representing its current state:

* **Not Connected**: This initial status indicates that the application has not been saved yet. Therefore, Blynk has not yet connected to the application, and the integration will not work. This status also applies when Blynk initially fails to connect to The Things Stack with the specified parameters.
* **Connected**: This status represents the normal state of the integration. Everything should operate normally in this status.
* **Broken**: This status indicates a problem related to the application. Possible issues include Blynk failing to establish a connection with The Things Stack work server (e.g., due to a server restart), invalid credentials, or an internal error on The Things Stack. In this status, Blynk will periodically try to reconnect to The Things Stack server to fix the integration. Once the integration operates normally, Blynk will automatically change the status to *Connected*.

## Next steps

With a configured Blynk template and a working device, you can now start creating a [Web Dashboard](https://docs.blynk.io/en/blynk.console/templates/dashboard) and [Mobile application](https://docs.blynk.io/en/blynk.apps/constructor).

For scaling your device fleet, refer to the [Device Grouping](https://docs.blynk.io/en/hardware-guides/the-things-stack/device-grouping) and [Automated Device Onboarding](https://docs.blynk.io/en/hardware-guides/the-things-stack/automated-device-onboarding) sections. Be sure to explore the [System DataStreams](https://docs.blynk.io/en/hardware-guides/the-things-stack/system-datastreams) section to extract more valuable data from your device.

Additionally, take advantage of other powerful Blynk features, such as [Automations](https://docs.blynk.io/en/concepts/automations), [WebHooks](https://docs.blynk.io/en/blynk.console/settings/developers/webhooks), [Device Connection Lifecycle](https://docs.blynk.io/en/concepts/connection-lifecycle-management), and the [Device HTTPS API](https://docs.blynk.io/en/blynk.cloud/device-https-api), to build more advanced workflows.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.blynk.io/en/hardware-guides/the-things-stack/getting-started.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
