$ sudo pip install git+http://github.com/ramonhpr/knot-lib-python@masterIf you don't have sudo access:
$ sudo pip install virtualenv
$ virtualenv ~/envname
$ . ~/envname/bin/activate
$ pip install .pip install -e .['dev']
This API can access the cloud selected in platform KNoT and find data about KNoT devices by a simple client connection.
KNoT can now support two protocol to get data from things:
- http
- socketio
KNoT can now support two clouds:
- Meshblu
- Fiware (just support to IoT agent and orion GE's)
In KNoT webui you can find your credentials
from knotpy import KnotConnection
credentials = {
'servername': 'knot-test.cesar.org.br',
'port': 3000,
'uuid': 'bf3aed9f-c52e-4bc5-9021-f5065acc0000',
'token': '0bd12a4c31de141909c3dd955d6b881d5ca5fa5b'
}
conn = KnotConnection(credentials)
myThings = conn.get_devices()Create a client object that will connect to a KNoT Cloud instance.
credentialsdict The credentials used to authenticate with each cloudservernamestring Cloud instance hostnameportint Cloud instance port- [
uuid] string uuid of Meshblu user - [
token] string token of Meshblu user
cloudstring One of the cloud supported by KNoT platform. Default: Meshblu .Supports: ['meshblu', 'Fiware']protocolstring One of the protocol supported. Default: Socket.IO. Supports: ['http', 'socketio']
from knotpy import KnotConnection
credentials = {
'servername': 'knot-test.cesar.org.br',
'port': 3000,
'uuid': 'bf3aed9f-c52e-4bc5-9021-f5065acc0000',
'token': '0bd12a4c31de141909c3dd955d6b881d5ca5fa5b'
}
conn = KnotConnection(credentials)Gets the devices associated to the connected user.
deviceslist devices registered on the cloud or an empty list. Each device is an object in the following format:idString device ID (KNoT ID).nameString device name.onlineBoolean whether this device is online or not.
from knotpy import KnotConnection
credentials = {
'servername': 'knot-test.cesar.org.br',
'port': 3000,
'uuid': 'bf3aed9f-c52e-4bc5-9021-f5065acc0000',
'token': '0bd12a4c31de141909c3dd955d6b881d5ca5fa5b'
}
conn = KnotConnection(credentials)
try:
my_devices = conn.get_devices()
except Exception as err:
print(err)
# [ { online: true,
# name: 'Door lock',
# id: '7e133545550e496a',
# schema: [ [Object], [Object] ] } ]List the sensor by a device identified by id.
idString device ID (KNoT ID).
sensorslist list of intidint sensor ID
from knotpy import KnotConnection
credentials = {
'servername': 'knot-test.cesar.org.br',
'port': 3000,
'uuid': 'bf3aed9f-c52e-4bc5-9021-f5065acc0000',
'token': '0bd12a4c31de141909c3dd955d6b881d5ca5fa5b'
}
conn = KnotConnection(credentials)
try:
sensors = conn.list_sensors('7e133545550e496a')
except Exception as err:
print(err)
# [1,2]Gets the sensor details from a specific sensor identified.
idString device ID (KNoT ID).sensor_idint sensor ID.
schemalist schema items, each one formed by:sensor_idint sensor ID.value_typeint|float|bool semantic value type (voltage, current, temperature, etc).unitint sensor unit (V, A, W, etc).type_idint data value type (boolean, integer, etc).namestring sensor name.
from knotpy import KnotConnection
credentials = {
'servername': 'knot-test.cesar.org.br',
'port': 3000,
'uuid': 'bf3aed9f-c52e-4bc5-9021-f5065acc0000',
'token': '0bd12a4c31de141909c3dd955d6b881d5ca5fa5b'
}
conn = KnotConnection(credentials)
try:
sensors = conn.list_sensors('7e133545550e496a')
for sensor_id in sensors:
print(conn.get_sensor_details('7e133545550e496a', sensor_id))
except Exception as err:
print(err)
# { sensor_id: 1,
# value_type: 3,
# unit: 0,
# type_id: 65521,
# name: 'Lock' }
# { sensor_id: 2,
# value_type: 1,
# unit: 2,
# type_id: 9,
# name: 'Card reader' }Gets the last 10 data items published by the device identified by id.
idString device ID (KNoT ID).limitString (Optional) the maximum number of data that you want, default=10(the value*returns all data)startString (Optional) the start date that you want your set of data (format=YYYY/MM/DD HH:MM)finishString (Optional) the finish date that you want your set of data (format=YYYY/MM/DD HH:MM)
data_itemslist data items published by the device or an empty list. Each data item is an object in the following format:datadict data published by the device, in the following format:sensor_idint sensor ID.valueString|Boolean|float|int value published.
timestampDate moment of publication.
from knotpy import KnotConnection
credentials = {
'servername': 'knot-test.cesar.org.br',
'port': 3000,
'uuid': 'bf3aed9f-c52e-4bc5-9021-f5065acc0000',
'token': '0bd12a4c31de141909c3dd955d6b881d5ca5fa5b'
}
conn = KnotConnection(credentials)
try:
my_devices = conn.get_data('7e133545550e496a')
except Exception as err:
print(err)
# [ { data: { sensor_id: 2, value: 0 },
# timestamp: '2018-08-25T05:29:43.519Z' },
# { data: { sensor_id: 1, value: true },
# timestamp: '2018-08-25T05:29:43.520Z' },
# ... ]Sets a value to a sensor.
idString device ID (KNoT ID).sensorIdString sensor ID.valueString|Boolean|Number value to attribute to the sensor.
from knotpy import KnotConnection
credentials = {
'servername': 'knot-test.cesar.org.br',
'port': 3000,
'uuid': 'bf3aed9f-c52e-4bc5-9021-f5065acc0000',
'token': '0bd12a4c31de141909c3dd955d6b881d5ca5fa5b'
}
conn = KnotConnection(credentials)
try:
my_devices = conn.set_data('7e133545550e496a', 1, True)
except Exception as err:
print(err)Requests the device to publish its current value of a sensor. The value can be retrieved using get_data() or by listening to device updates.
idString device ID (KNoT ID).sensorIdString sensor ID.
from knotpy import KnotConnection
credentials = {
'servername': 'knot-test.cesar.org.br',
'port': 3000,
'uuid': 'bf3aed9f-c52e-4bc5-9021-f5065acc0000',
'token': '0bd12a4c31de141909c3dd955d6b881d5ca5fa5b'
}
conn = KnotConnection(credentials)
try:
my_devices = conn.request_data('7e133545550e496a', 1)
except Exception as err:
print(err)Send configuration from the sensor of your thing if it is online
idString device ID (KNoT ID).sensorIdString sensor ID.event_flagsint You can use the event flags macro bellow:FLAG_TIME: Send data every period of time, in seconds. Needs a value greater than 0 to be passed on time_secFLAG_LOWER: Send data every time that the item is below a threshold. The value to be compared with is the one passed on lower_limit. If combined withFLAG_UPPER, it is mandatory that lower_limit is smaller than upper_limit.FLAG_UPPER: Send data every time that the item is above a threshold. The value to be compared with is the one passed on upper_limit. If combined withFLAG_LOWER, it is mandatory that lower_limit is smaller than upper_limit.FLAG_CHANGE: Send data every time the item changes its value. Does not require any additional field.FLAG_MAX: Send all the other above
time_secint the time in seconds when a sensor will publish a data. IfFLAG_TIMEis set, this field is mandatorylower_limitint the threshold value when a sensor will publish bellow this value. IfFLAG_LOWERis set, this field is mandatoryupper_limitint the threshold value when a sensor will publish above this value. IfFLAG_UPPERis set, this field is mandatory
from knotpy import *
credentials = {
'servername': 'knot-test.cesar.org.br',
'port': 3000,
'uuid': 'bf3aed9f-c52e-4bc5-9021-f5065acc0000',
'token': '0bd12a4c31de141909c3dd955d6b881d5ca5fa5b'
}
conn = KnotConnection(credentials)
try:
my_devices = conn.send_config('7e133545550e496a', 1, event_flag=FLAG_TIME+FLAG_CHANGE, time_sec=30)
except Exception as err:
print(err)Subscribes to data published by a device identified by id. To listen to the publish events, register a callback in parameter onReceive.
idstringon_receivefunction Callback function called with device updates. Receives:eventdict published event, object in the following format:sourceString device ID (KNoT ID).datadict data published by the device, in the following format:sensor_idNumber sensor ID.valueString|Boolean|Number value published.
timestampDate moment of publication.
from knotpy import *
credentials = {
'servername': 'knot-test.cesar.org.br',
'port': 3000,
'uuid': 'bf3aed9f-c52e-4bc5-9021-f5065acc0000',
'token': '0bd12a4c31de141909c3dd955d6b881d5ca5fa5b'
}
conn = KnotConnection(credentials)
def callback(event):
print(event)
try:
my_devices = conn.subscribe('7e133545550e496a', callback)
except Exception as err:
print(err)
# { data: { sensor_id: 2, value: 21 },
# timestamp: '2018-08-25T17:46:41.337Z',
# source: '7e133545550e496a' }You can enable two log levels in knotpy, INFO and DEBUG, by adding a environment variable. In INFO log you can enable messages in the package knotpy. And in DEBUG log you can also see the log levels in the other dependencies packages.