# Data collection

## MQTT data collection

This section shows how to use OpenGate MQTT connector for data collection.

Using MQTT, your devices only need one TCP connection to exchange messages with the platform: publish collected data, receive operation requests, send operation responses, ask for pending operations, etc.

## How to connect to OpenGate MQTT connector

These are the parameters to establish a MQTT connection with OpenGate:

- Host: `api.opengate.es`
- Port: `1883`
- User: `your-device-id`
- Password: `your-api-key`

### Obtaining your API key

1. Login onto the OpenGate web interface
2. Click on the cogs that are at the top-right of the OpenGate home page
3. Click on the **User** option
4. Click on the "**Click to show**" link

### Collecting data using `mosquitto` CLI tool

`mosquitto` is an open source MQTT client and server. The following example shows how to connect and publish data using OpenGate MQTT connector:

```bash
mosquitto_pub \
    -h api.opengate.es -p 1883 \
    -t odm/iot/your-device-id \
    -u your-device-id -P your-api-key \
    -m 'your-datastreams-as-json'
```

## OpenGate default MQTT topics

- To publish collected data: `odm/iot/your-device-id`
- To subscribe to incoming operations from OpenGate: `odm/request/your-device-id`
- To publish operation responses: `odm/response/your-device-id`

You have to replace `your-device-id` with the OpenGate unique identifier of your device.

## Data collection payload

The payload definition in the section [HTTP integration](../../http/) to collect data is entirely valid. You only have to add a `"device": "your-device-id"` field, filled with your OpenGate device unique identifier, at the top level of the JSON document with the collected values.

See the following example:

Publish to `odm/iot/your-device-id` topic this JSON:

```json
{
  "version": "1.0.0",
  "device": "your-device-id",
  "datastreams": [
    {
      "id": "temperature",
      "datapoints": [{ "value": 24.1 }]
    }
  ]
}
```
