$ sudo pip install git+http://github.com/ramonhpr/knot-lib-python@master
If 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.
credentials
dict The credentials used to authenticate with each cloudservername
string Cloud instance hostnameport
int Cloud instance port- [
uuid
] string uuid of Meshblu user - [
token
] string token of Meshblu user
cloud
string One of the cloud supported by KNoT platform. Default: Meshblu .Supports: ['meshblu', 'Fiware']protocol
string 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.
devices
list devices registered on the cloud or an empty list. Each device is an object in the following format:id
String device ID (KNoT ID).name
String device name.online
Boolean 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.
id
String device ID (KNoT ID).
sensors
list list of intid
int 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.
id
String device ID (KNoT ID).sensor_id
int sensor ID.
schema
list schema items, each one formed by:sensor_id
int sensor ID.value_type
int|float|bool semantic value type (voltage, current, temperature, etc).unit
int sensor unit (V, A, W, etc).type_id
int data value type (boolean, integer, etc).name
string 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.
id
String device ID (KNoT ID).limit
String (Optional) the maximum number of data that you want, default=10
(the value*
returns all data)start
String (Optional) the start date that you want your set of data (format=YYYY/MM/DD HH:MM
)finish
String (Optional) the finish date that you want your set of data (format=YYYY/MM/DD HH:MM
)
data_items
list data items published by the device or an empty list. Each data item is an object in the following format:data
dict data published by the device, in the following format:sensor_id
int sensor ID.value
String|Boolean|float|int value published.
timestamp
Date 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.
id
String device ID (KNoT ID).sensorId
String sensor ID.value
String|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.
id
String device ID (KNoT ID).sensorId
String 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
id
String device ID (KNoT ID).sensorId
String sensor ID.event_flags
int 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_sec
int the time in seconds when a sensor will publish a data. IfFLAG_TIME
is set, this field is mandatorylower_limit
int the threshold value when a sensor will publish bellow this value. IfFLAG_LOWER
is set, this field is mandatoryupper_limit
int the threshold value when a sensor will publish above this value. IfFLAG_UPPER
is 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.
id
stringon_receive
function Callback function called with device updates. Receives:event
dict published event, object in the following format:source
String device ID (KNoT ID).data
dict data published by the device, in the following format:sensor_id
Number sensor ID.value
String|Boolean|Number value published.
timestamp
Date 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.