Welcome to the Labeeb IoT Quick Starts space!
This page is dedicated to the Labeeb IoT Platform Quick Starts space.
Labeeb IoT provides a set of comprehensive APIs and Java SDKs which can be used to:
- Integrate existing or new IoT devices with Labeeb IoT: 1) connection management ; 2) pushing data records to Labeeb IoT platform;
- Develop new IoT Services and Applications: 1) connection management; 2) received real-time data from remote IoT devices, through the Labeeb IoT platform.
This page describes the prerequisites and some detailed examples on how to develop the above features.
Target Scenario
We assume that the user has already access to its enterprise space on the Labeeb IoT Portal (more info), for example, by using the below authentication credentials:
- Enterprise Name: Enterprise1
- Login: admin
- Password: admin
We assume that the user or developer would like to monitor and manage a remote Linux Gateway using Labeeb IoT. This gateway will be sending periodically (every second) its current CPU level and memory utilization to Labeeb IoT.
To that end, the user should create the below models on the Labeeb IoT Portal (more info):
Portal Page | Models | Parameters |
---|---|---|
Platform Management / Data Models | Data Model and Data Types (click Add New Data Model) |
|
Platform Management / Device Templates | Device Template (click Add New Device Template) |
|
Devices | Device (click Add New Device) |
|
Once the above models are properly defined on the Labeeb IoT Portal level, the next step consists in integrating the device (Linux Gateway) with the Labeeb IoT platform.
Several options are discussed in the below sections.
How to push data from an IoT device to the Labeeb IoT platform using commands ?
Prerequisites:
- A properly configured Linux machine (board, sensor, device, etc.) with root access
- A connection to Internet (e.g. 3G, GPRS, 4G, WiFi, etc.)
Installation of MQTT:
- MQTT can be installed on any Debian or Ubuntu based Linux operating systems using the following command:
apt-get install mosquitto-clients
- For other Windows or Linux based operating systems, please refer to the following documentation: http://mosquitto.org/download/
- Once mosquitto-clients is properly installed, message could be pushed to Labeeb IoT using the below command:
mosquitto_pub -h mea.labeeb-iot.com -u USERNAME -P PASSWORD -t TOPIC -m MESSAGE
where:
- USERNAME corresponds to the device template's login which should be formatted as follows: ENTERPRISE_NAME+"_device_template_"+DEVICE_TEMAPLTE. In our scenario, USERNAME is equal to Enterprise1_device_template_linux.
- PASSWORD corresponds to the device template's password which is in our case equal to linux$123
- TOPIC corresponds to the device's MQTT topic name to publish data records. This information is shown on the portal when adding or editing the corresponding device. In our case, the TOPIC is equal to /Enterprise1/linux/device1/dataCSV
- MESSAGE corresponds to the data records that device will push to the Labeeb IoT platform and which should be formatted as follows (CSV format, without spaces, except in data models, devices and templates names):
TIMESTAMP,LONGITUDE:LATITUDE:ALTITUDE,DATA_MODEL_NAME,DATA_TYPE_NAME:DATA_TYPE_VALUE,…,DATA_TYPE_NAME:DATA_TYPE_VALUE
- Here, TIMESTAMP corresponds to the UNIX timstamp (i.e. number of seconds since 1 January 1970)
- For more information on protocol formart, please refer to How to Connect Your Device to Labeeb IoT
Data Acquisition and Transmission:
- The below Linux Bash script will acquire periodically (every 1 second) the CPU level and memory usage and transmit them to the Labeeb IoT Platform using the previously created data models:
#!/bin/bash LABEEB_IOT_ADDRESS="mea.labeeb-iot.com" ENTERPRISE_ID="Enterprise1" DEVICE_TEMPLATE_NAME="linux" DATA_MODEL_NAME="system" DEVICE_NAME="device1" LOGIN=$ENTERPRISE_ID"_device_template_"$DEVICE_TEMPLATE_NAME PASSWORD="linux$123" TOPIC="/"$ENTERPRISE_ID"/"$DEVICE_TEMPLATE_NAME"/"$DEVICE_NAME"/dataCSV" echo "Starting IoT Device: "$DEVICE_NAME while true do cpu_used=$(top -bn 2 -d 0.01 | grep '^Cpu' | tail -n 1 | tr ":" " " | tr "%" " " | awk '{print $2}') mem_used=$(free | grep Mem | awk '{print $3/$2 * 100.0}') timestamp=$(date '+%s') DATA=$timestamp",,"$DATA_MODEL_NAME",cpu_used:"$cpu_used",mem_used:"$mem_used echo "Pushing data to Labeeb IoT: "$TOPIC mosquitto_pub -h $LABEEB_IOT_ADDRESS -u $LOGIN -P $PASSWORD -t "$TOPIC" -m "$DATA" sleep 1 done
How to push data from an IoT device to the Labeeb IoT platform using the Java SDK ?
Prerequisites:
- A properly configured Linux or Windows desktop machine
- A properly configured Java or Android development environment (e.g. Eclipse, Netbeans, Android Studio)
- A connection to Internet (e.g. 3G, GPRS, 4G, WiFi, etc.)
- The Labeeb IoT Java SDK (JAR file) should be copied to a local folder
Development Steps:
Without loss of generality, we will describe the usage of the Labeeb Iot Java SDK using the Netbeans development environment.
- Under Netbeans, create a new java application project and click on Next, as shown below:
- Define a project name, named "IoTDevice", and click on Finish, as shown below:
- Under the created project "IoTDevice", right click on the folder Libraries, click on Add JAR/Folder, locate the Labeeb IoT SDK JAR file (labeeb-iot-sdk.jar), and click on Open.
- Once the Labeeb IoT SDK JAR file is properly linked to the project "IoTDevice", modify the class "IoTDevice.java" as follows:
package iotdevice; import org.qmic.sdk.iot.common.exceptions.IoTConnectionException; import org.qmic.sdk.iot.common.exceptions.IoTException; import org.qmic.sdk.iot.common.exceptions.IoTMessagingException; import org.qmic.sdk.iot.device.Device; import org.qmic.sdk.iot.device.beans.DataRecord; import org.qmic.sdk.iot.device.beans.Request; import org.qmic.sdk.iot.device.interfaces.DeviceListener; public class IoTDevice implements DeviceListener { Device con; public static void main(String[] args) throws IoTException, IoTConnectionException, IoTMessagingException, InterruptedException { new IoTDevice().start(); } public void start() throws IoTException, IoTConnectionException, IoTMessagingException, InterruptedException { // connect to the remote Labeeb IoT Platform con = new Device("Enterprise1", "linux", "linux$123"); con.setSsl(true); con.connect(); while(true) { // create a data record DataRecord data = new DataRecord("system", "cpu_used", "50"); data.addDataType("mem_used", "90"); // push the data record to Labeeb IoT con.pushData(data); // sleep for 1 second Thread.sleep(1000); } } @Override public void onRequestReceived(Request rqst) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void onConnectionLost(Throwable thrwbl) { throw new UnsupportedOperationException("Not supported yet."); } }
How to call Restfull Web Services using the Java SDK ?
Prerequisites:
- The Labeeb IoT Java SDK (JAR file) properly installed
Development:
User Services:
import java.io.IOException; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.qmic.sdk.iot.common.exceptions.IoTServiceException; import org.qmic.sdk.iot.services.UserServices; public class Test { public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException, IoTServiceException{ // authenticate Context ctx = UserServices.authenticate("username", "password", "enterprise"); System.out.println(ctx.getSessionId()); // logout UserServices.logout(); } }
Data Services:
package org.qmic; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.qmic.iot.drl.ws.service.ObjectTypeEnum; import org.qmic.mdd.behavior.service.AlertRecord; import org.qmic.mdd.behavior.service.AlertSeverEnum; import org.qmic.sdk.iot.common.exceptions.IoTServiceException; import org.qmic.sdk.iot.services.DataServices; import org.qmic.sdk.iot.services.UserServices; import org.qmic.sdk.iot.services.commons.ApplicationContext; import org.qmic.sdk.iot.services.commons.Context; import org.qmic.uam.service.ActivityLogSearchCriteria; import org.qmic.uam.service.DashBoardDetails; public class Test2 { public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException, IoTServiceException { // authenticate ApplicationContext.setServerAddress("demo.labeeb-iot.com"); Context ctx = UserServices.authenticate("demo", "demo", "demo"); System.out.println("ctx" + ctx.getSessionId()); Date startDate = new Date(System.currentTimeMillis() - (1000*60*60*60*24)); Date endDate = new Date(System.currentTimeMillis()); ActivityLogSearchCriteria criteria = new ActivityLogSearchCriteria(); criteria.setSeverityEnum(AlertSeverEnum.NOTICE); List<AlertRecord> lstAlerts = DataServices.getActivityLogs(criteria); System.out.println(lstAlerts.get(0).getDeviceId()); // Get the dashboard data of the last 24 hours List<ObjectTypeEnum> lstObject = new ArrayList<ObjectTypeEnum>(); lstObject.add(ObjectTypeEnum.ALL); DashBoardDetails dashboard = DataServices.getDashBoardData(startDate, endDate, lstObject); System.out.println(dashboard.getDevicesCount()); // logout UserServices.logout(); } }
Device Services:
import java.io.IOException; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.qmic.iot.drl.ws.service.PrimitiveDataTypeEnum; import org.qmic.mdd.behavior.service.ActionEnum; import org.qmic.mdd.behavior.service.TimeMeasureEnum; import org.qmic.sdk.iot.common.exceptions.IoTServiceException; import org.qmic.sdk.iot.services.DeviceServices; import org.qmic.sdk.iot.services.UserServices; public class Test { public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException, IoTServiceException{ // authenticate Context ctx = UserServices.authenticate("username", "password", "enterprise"); //create new data type DeviceServices.createDataType("dataType1", PrimitiveDataTypeEnum.INTEGER); // create new data model DeviceServices.createDataModel("datamodel1", 90, 2, TimeMeasureEnum.HOUR, ActionEnum.BLACKLIST_DEVICES); // logout UserServices.logout(); } }