# How to integrate the XDK device from Legic via MQTT on akenza

<figure><img src="https://2165942204-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMKXTFIN5ZlLOjBlfC4%2Fuploads%2FxDF0IQHV5swGg7En24mC%2Fxdk%20legic.png?alt=media&#x26;token=487889b5-c179-4ae7-b561-f2d48aa65acd" alt=""><figcaption><p>XDK device from Legic</p></figcaption></figure>

{% hint style="success" %}
**Applications:**

* Smart Building
* Predictive maintenance & monitoring of machinery

The Legic XDK Secure Sensor Evaluation Kit is the “The Swiss army knife of IoT solutions”. It is a universal programmable sensor device & prototyping platform for any IoT use case.

**Product features:**

* Accelerometer
* Gyroscope
* Magnetic field strength
* Brightness
* Temperature
* Pressure
* Humidity
* Acoustic noise

Enables rapid prototyping and fast transition from prototype to mass production. The device consists of eight MEMS sensors, Bluetooth, Wi-Fi. The Software includes XDK Workbench, extensive libraries & modular source code

You can find more information about the XDK [here](https://www.xdk.io).
{% endhint %}

### 1. Create Data Flow

To create a **Data Flow**,  go to Data Flow and select *Create Data Flow*.

Select *Connect a device over MQTT*. \
You do not need to choose a device type. By default, the payload will be **passed through** without processing. This gives the sender the option to structure the payload freely.\
As an output connector, you can choose one or multiple output Connectors. For this example the default setting, akenza DB is sufficient. Therefore you will have access to historical data.

Save your **Data Flow** and proceed with the Asset creation in *Assets*.

### 2. Create an MQTT device

To create a new MQTT device, select *Create Device* in the menu of the Asset Inventory. Add a device name. The other fields are optional. Select your created MQTT – Data Flow. Once the device is saved, proceed with the configuration of your XDK device.

### 3. Configure the XDK device with the XDK Workbench

Please install the XDK-Workbench. Download [here](https://developerportal.xdk.io/downloads/).&#x20;

This will be used to **code** and **configure** the XDK device. Once the installation is executed, open the XDK-Workbench and select *Use Eclipse Mita* in the welcome screen.

<figure><img src="https://2165942204-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMKXTFIN5ZlLOjBlfC4%2Fuploads%2FAAZH81aedRRTcrqlcH5I%2Fscreely-1666595169096.png?alt=media&#x26;token=b0158957-3950-4f34-a4fe-96019b1e741b" alt=""><figcaption><p>XDK Workbench</p></figcaption></figure>

{% hint style="info" %}
For this application, there are no coding skills required.
{% endhint %}

The following code can be copy-pasted with minor changes for the **WiFi credentials** and the **MQTT connection string**.

<pre><code>package main;
import platforms.xdk110;
// resource for accelerometer
setup accelerometer {
    bandwidth = BW_500Hz;
    range = Range_8G;
}

// resource for gyroscope
setup Gyroscope_BMI160 {
    bandwidth = BW_10_7Hz;
    range = Range_250s;
}

// resource for light sensor
setup light {
    manual_mode = false;
}

// resource for environment sensor
setup environment {
    power_mode = Normal;
    standby_time = 1000;
    temperature_oversampling = OVERSAMPLE_1X;
}

// resource for WiFi connectivity
setup wifi : WLAN {
    ssid = 'WLAN_SSID';
    authentication = Personal(psk = 'WLAN_PASSWORD');
}

<strong>setup led : LED {
</strong>    var red = light_up(Red);
    var yellow = light_up(Yellow);
    var orange = light_up(Orange);
}

// resource for Akenza MQTT Broker
setup akenzaBroker : MQTT {
    transport = wifi;
    cleanSession = true;
    url = 'MQTT_BROKER';
    clientId = 'USERNAME';
    authentication = Login('USERNAME', 'PASSWORD');
    var akenzaTopic = topic('UPLINK_TOPIC', 0);
<strong>}
</strong>
// create event: every 5 sec read sensors &#x26; send to Akenza
every 300 seconds {
    // read sensors
    var accx = accelerometer.x_axis.read();
    var accy = accelerometer.y_axis.read();
    var accz = accelerometer.z_axis.read();
    var gyx = gyroscope.x_axis.read();
    var gyy = gyroscope.y_axis.read();
    var gyz = gyroscope.z_axis.read();
    var light = light.intensity.read() / 1000.0;
    var temperature = environment.temperature.read() / 1000.0;
    temperature = temperature - 6.5975;
    var humidity = environment.humidity.read() / 1000.0;
    var pressure = environment.pressure.read() / 1000.0;

    // create JSON string
    var json = `{
        "accx": ${accx}, "accy": ${accy}, "accz": ${accz},
        "gyx": ${gyx}, "gyy": ${gyy}, "gyz": ${gyz},
        "light": ${light}, "temperature": ${temperature},
        "humidity": ${humidity}, "pressure": ${pressure}
    }`;

    // publish data to Akenza
    akenzaBroker.akenzaTopic.write(json);
    println(json);
    led.orange.write(true);
}
</code></pre>

To find the **MQTT credentials** needed, head over to the Asset inventory and select the device that you want to use. Under the tab *API-Configuration,* you can find the credentials. Edit the lines accordingly:

```
url = 'MQTT_BROKER URL'; (use the broker name with the ending 1883)
clientId = 'USERNAME';
authentication = Login('USERNAME', 'PASSWORD');
var akenzaTopic = topic('UPLINK_TOPIC', 0);
```

{% hint style="info" %}
Note that the clientId should be unique for each broker. Picking the username in this tutorial is fine, but for production we recommend using the Device ID.
{% endhint %}

{% hint style="info" %}
Note for the MQTT URL: In order for the XDK-Workbench to accept the URL, it has to start with `mqtt://...`

Please change the URL provided by Akenza from `tcp://...` to `mqtt://...`
{% endhint %}

Also, edit this section with your WiFi-Credentials:

```
ssid = 'WLAN_SSID';
authentication = Personal(psk = 'WLAN_PASSWORD');
```

After adding all the needed credentials, you can now save the project. Then right-click on the Project you are working on in *Project Explorer* and clean it. After the Device console throws out the message *Build Finished*, right-click on the project again and build it again. This might take a moment.

After the device console shows that the build is finished, you can now flash your device. For that, please connect your XDK device to your PC and turn the XDK on while pressing button 1 (the button with one dot). By turning the device on this way, you activate the bootloader mode. After that, press *Flash* in the Device manager as shown below.

<figure><img src="https://2165942204-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMKXTFIN5ZlLOjBlfC4%2Fuploads%2FYMmoxIjbcoa2ecQeFKCP%2Fscreely-1666603356688.png?alt=media&#x26;token=e9a8420e-fe1f-4f18-a04b-01bb62daab4e" alt=""><figcaption></figcaption></figure>

After the device is flashed, the data will be streamed to akenza.&#x20;

The incoming data can be viewed on the **Device Detail Page** in both the Message Logs and Data Overview Tabs:

<figure><img src="https://2165942204-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMKXTFIN5ZlLOjBlfC4%2Fuploads%2FfrKLKW3SN2SFShtv19Yy%2Fimage.png?alt=media&#x26;token=87963b2a-b960-40b5-bd83-585ee4f95bfa" alt=""><figcaption><p>Message Logs</p></figcaption></figure>

<figure><img src="https://2165942204-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMKXTFIN5ZlLOjBlfC4%2Fuploads%2F6mojRjqhtDGbakTAkpWc%2Fscreely-1666258260993.png?alt=media&#x26;token=2a5297fc-6d74-4903-8179-2c26e94c46e6" alt=""><figcaption><p>Data Overview</p></figcaption></figure>

**Congratulations**, you have successfully integrated the XDK device from Legic on akenza.

{% hint style="info" %}
**Optionally**: Check out the [Rule engine](https://docs.akenza.io/akenza.io/get-started/rule-engine) in order to apply a business logic to your IoT- infrastructure.&#x20;
{% endhint %}
