Getting started with Python SDK for Labeeb IoT Devices
The main purpose of this SDK is to provide the simplest way to connect your device to Labeeb IoT Platform.With this Python SDK ,connecting to Labeeb will be simply functions calls.
What can be done with Labeeb IoT Python SDK for devices
Using this SDK ,you will be able to communicate and publish data to LAbeeb IoT through MQTT , COAP protocols ,or booth at the same time!
MQTT connectivity :
Client initialization : where you can specify the server address and access credentials.
Connect to Labeeb IoT server: where you can establish the connection with Labeeb IoT.
Publish data to Labeeb IoT platform: where you can send your data to Labeeb IoT.
Close the MQTT connection: to close the connection,when you finish sending you data.
CoAP protocol :
Client initialization: where you can specify the server address and access credentials.
Put data to Labeeb IoT: where you can send your data to Labeeb IoT.
Close the connection: to close the connection,when you finish sending you data.
SDK download and import :
Tip | |||||||
---|---|---|---|---|---|---|---|
| |||||||
Before you start using this SDK you need to install : 1)Download SDK rar file , you can find it here( python_SDK.rar ) . 2) extract compressed file and execute the following command to run (python_sdk_install.sh ) script , it's inside SDK folder. that will automatically install required dependencies :
*you may be asked to enter root password. |
SDK files :
Extract the compressed file, you will find a folder called SDK, create new empty .py file inside it,for example : (PushToLAbeeb.py).
Importing the SDK :
Start the code by importing the python library , use the following line :
Code Block language py theme Eclipse firstline 1 title Program Language Code 1 linenumbers true import labeebDevice
Creat an object instance from labeebDevice , like :
Code Block language py theme Eclipse firstline 1 title Program Language Code 1 linenumbers true connection = labeebDevice.labeebDevice()
Hands on Using the SDK to push data to Labeeb through MQTT
Initialize the MQTT connection :
You need to have credentials so you can connect to Labeeb IoT , don't have ? Get one now!
Use the following code line to connect :Code Block language py theme Eclipse firstline 1 title Program Language Code 1 linenumbers true connection.mqtt_client("demomea.labeeb-iot.com","enterprise","deviceUserName","devicePassword")
Establish the connection:
Call mqtt_connect function to establish the connection with Labeeb IoT using the credentials provided in the Initialization step :
Code Block language py theme Eclipse firstline 1 title Program Language Code 1 linenumbers true connection.mqtt_connect()
Put your data in (key:value) pairs :
follow the format , you can add all your key:value pairs at the same object :
Code Block language py theme Eclipse firstline 1 title Program Language Code 1 linenumbers true D = {'temp': "23","humidity" :"46"}
Publish the data to a specific topic :
Use the topic of the device , data model name to publish data :
Code Block language py theme Eclipse firstline 1 title Program Language Code 1 linenumbers true connection.mqtt_publish ("/topic/of/device/to/publish","DataModelName" , D )
Publish a file to a specific topic :
Use the topic of the device,data model name to publish a file,the file will be converted to base64 encryption before publishing :
Code Block language py theme Eclipse firstline 1 title Program Language Code 1 linenumbers true connection.mqtt_publish_file ("/topic/of/device/to/publish","DataModelName" , "FileName" )
Close the the connection :
you can close the connection with Labeeb IoT by calling :
Code Block language py theme Eclipse firstline 1 title Program Language Code 1 linenumbers true connection.mqtt_disconnect()
Hands on Using the SDK to push data to Labeeb through CoAP
Initialize the CoAP connection :
You need to have credentials so you can connect to Labeeb IoT , don't have ? Get one now!
Use the following code line to connect :Code Block language py theme Eclipse firstline 1 title Program Language Code 1 linenumbers true connection.coap_client ("demo.labeeb-iot.com","enterprise","deviceUserName","device password")
Put your data in (key:value) pairs :
follow the format , you can add all your key:value pairs at the same object :
Code Block language py theme Eclipse firstline 1 title Program Language Code 1 linenumbers true D = {'temp': "23","humidity" :"46"}
Put data to labeeb IoT :
Use Template name ,Data Model name , Device name to send data data over CoAP :
Code Block language py theme Eclipse firstline 1 title Program Language Code 1 linenumbers true connection.coap_put ( "deviceTemplate","datamodel","deviceName",D)
What's next ?
After you push data to your Labeeb IoT account , now you can check the Data on the portal , retrieve it through our Labeeb IoT APIs and Web Services and then use this data in your mobile apps.Check one of our Getting Started.