Child pages
  • Tutorial N°5: Labeeb IoT Applications
Skip to end of metadata
Go to start of metadata

Applications enables the management of external discovery and subscription to platform / devices resources. Applications can subscribe to certain devices, device templates, data models, data types. Labeeb IoT Applications is made to manage the access to devices' resources by having ACL ( Access Control List ), which allows the enterprise to limit access to specific resources either as a  Read or Read, Write.

In this tutorial we will provide you with a step by step guide to use Labeeb IoT Applications to track the CPU usage (in Ubuntu OS). 


Before starting

Required:

  • 2 Linux  machines , we will use Ubuntu( 1 for sending and the other to receive ). 
  • MQTT Client.

  • STRESS Linux library.


Installing requirements : 

  • MQTT can be installed on any Debian or Ubuntu based Linux operating systems using the following command:

    MQTT client install
    apt-get install mosquitto-clients
  • STRESS Linux library.

    CPU Stress library
    apt-get install stress

Assumptions:

  • We assume you have already been granted an account on Labeeb IoT platform, if not , click here to get your account, or visit this page to get help. 
  • We assume your created a device with the following specifications  : 
    • Data types: cpu_level (Integer) , cpu_control (Text).
    • Data model: cpu
    • Device Template: os.
    • Device Name :  ubuntu_cpu

          If you need some help creating the device please visit this page.


Step by step Guide


 Step 1: Create new Application

1) Login to Labeeb IoT platform and from the left menu select Settings>>Applications.

2) Create new Application , name "it cpu_app" with the following Access Control List (ACL) :

3) Chose a password for the new Application and click save.

 Step 2: Script to publish CPU data to the Application

On the first Ubuntu machien , use the following bash script to publish CPU usage to the Application :


#!/bin/bash


while :
do

        cpu_used=$(top -bn 2 -d 0.01 | grep 'Cpu' | tail -n 1 | tr ":" " " | tr "%" " " | awk '{print $2}')
	sleep 1
        mosquitto_pub -h mea.labeeb-iot.com -p 1883 -u "[ENTREPRISE-NAME_APPLICATION-NAME]" -P "[APPLICATIO PASSWORD]" -t "/ENTREPRISE-NAME/DEVICE-TEMPLATE/DEVICE-NAME/request" -m "$cpu_used"
	
        echo "publishing cpu usage : "

	echo "Press [CTRL+C] to stop: $cpu_used"
	sleep 1
done


 Step 3: Script to subscribe to Application's data :

On the second Ubuntu machien , use the following bash script to subscribe to CPU usage to the Application :


#!/bin/bash
mosquitto_sub -h mea.labeeb-iot.com -p 1883 -u "[ENTREPRISENAME_APPLICATIONNAME]" -P "[APPLICATIO PASSWORD]" -c -q 1 -i ubuntu_cpu -t "/ENTREPRISE-NAME/DEVICE-TEMPLATE/DEVICE-NAME/request"
 
 Step 4: Rub both scripts:

Navigate to scripts derictory and run the scripts as following (for tutorial purposes we run the two scripts on the same machien, but you can run it on diffrent devices or microcontroller or any MQTT supported language) :

sudo chmod a+x publish.sh 
sudo chmod a+x subscribe.sh 


./publish.sh 
./subscribe.sh 

 Step 5: Stress the CPU to change value :

On the publisher machien, use the following code to stress the cpu so you can notice the change on reciver side terminal :

sudo stress --cpu 5