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

In this tutorial you will learn how to connect the XDK device via MQTT on akenza

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.

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.

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.

For this application, there are no coding skills required.

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

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');
}

setup led : LED {
    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);
}

// create event: every 5 sec read sensors & 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);
}

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);

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.

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://...

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.

After the device is flashed, the data will be streamed to akenza.

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

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

Optionally: Check out the Rule engine in order to apply a business logic to your IoT- infrastructure.

Last updated