Python sensor poll and callback examples
This commit is contained in:
parent
894997d8f0
commit
c1f3579a72
2 changed files with 97 additions and 0 deletions
48
examples/python/sensors/sensorscallback.py
Normal file
48
examples/python/sensors/sensorscallback.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
|
||||||
|
from ctypes import c_int, c_ubyte, c_void_p, POINTER, string_at, create_string_buffer, c_char_p, c_int, byref #imports allowing the use of our library
|
||||||
|
import time
|
||||||
|
import platform
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
#platform specific imports:
|
||||||
|
if (platform.system() == 'Windows'):
|
||||||
|
#Windows
|
||||||
|
from ctypes import windll, WINFUNCTYPE
|
||||||
|
lib = windll.LoadLibrary('TelldusCore.dll') #import our library
|
||||||
|
else:
|
||||||
|
#Linux
|
||||||
|
from ctypes import cdll, CFUNCTYPE
|
||||||
|
lib = cdll.LoadLibrary('libtelldus-core.so.2') #import our library
|
||||||
|
|
||||||
|
TELLSTICK_TEMPERATURE = 1;
|
||||||
|
TELLSTICK_HUMIDITY = 2;
|
||||||
|
|
||||||
|
#function to be called when a sensor event occurs
|
||||||
|
def callbackfunction(protocol, model, id, dataType, value, timestamp, callbackId, context):
|
||||||
|
|
||||||
|
print "Sensor:", string_at(protocol), string_at(model), "id:", id
|
||||||
|
if(dataType == TELLSTICK_TEMPERATURE):
|
||||||
|
print "Temperature:", string_at(value), "C,", datetime.fromtimestamp(timestamp)
|
||||||
|
elif(dataType == TELLSTICK_HUMIDITY):
|
||||||
|
print "Humidity:", string_at(value), "%,", datetime.fromtimestamp(timestamp)
|
||||||
|
print ""
|
||||||
|
|
||||||
|
|
||||||
|
if (platform.system() == 'Windows'):
|
||||||
|
CMPFUNC = WINFUNCTYPE(c_void_p, POINTER(c_ubyte), POINTER(c_ubyte), c_int, c_int, POINTER(c_ubyte), c_int, c_int, c_void_p) #first is return type
|
||||||
|
else:
|
||||||
|
CMPFUNC = CFUNCTYPE(c_void_p, POINTER(c_ubyte), POINTER(c_ubyte), c_int, c_int, POINTER(c_ubyte), c_int, c_int, c_void_p)
|
||||||
|
|
||||||
|
cmp_func = CMPFUNC(callbackfunction)
|
||||||
|
|
||||||
|
lib.tdInit()
|
||||||
|
callbackid = lib.tdRegisterSensorEvent(cmp_func, 0)
|
||||||
|
|
||||||
|
print "Waiting for events..."
|
||||||
|
try:
|
||||||
|
while(1):
|
||||||
|
time.sleep(0.5) #don't exit
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print "Exiting"
|
||||||
|
lib.tdUnregisterCallback(callbackid)
|
||||||
|
lib.tdClose()
|
49
examples/python/sensors/sensorspoll.py
Normal file
49
examples/python/sensors/sensorspoll.py
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
|
||||||
|
from ctypes import c_int, c_ubyte, c_void_p, POINTER, string_at, create_string_buffer, c_char_p, c_int, byref #imports allowing the use of our library
|
||||||
|
from threading import Timer
|
||||||
|
import time
|
||||||
|
import platform
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
#platform specific imports:
|
||||||
|
if (platform.system() == 'Windows'):
|
||||||
|
#Windows
|
||||||
|
from ctypes import windll, WINFUNCTYPE
|
||||||
|
lib = windll.LoadLibrary('TelldusCore.dll') #import our library
|
||||||
|
else:
|
||||||
|
#Linux
|
||||||
|
from ctypes import cdll, CFUNCTYPE
|
||||||
|
lib = cdll.LoadLibrary('libtelldus-core.so.2') #import our library
|
||||||
|
|
||||||
|
TELLSTICK_TEMPERATURE = 1;
|
||||||
|
TELLSTICK_HUMIDITY = 2;
|
||||||
|
|
||||||
|
def poll():
|
||||||
|
print "getting sensors"
|
||||||
|
|
||||||
|
protocollength = 20
|
||||||
|
modellength = 20
|
||||||
|
valuelength = 20
|
||||||
|
|
||||||
|
protocol = create_string_buffer(protocollength)
|
||||||
|
model = create_string_buffer(modellength)
|
||||||
|
idvalue = c_int()
|
||||||
|
dataTypes = c_int()
|
||||||
|
while(lib.tdSensor(protocol, protocollength, model, modellength, byref(idvalue), byref(dataTypes)) == 0):
|
||||||
|
print "Sensor: ", protocol.value, model.value, "id:", idvalue.value
|
||||||
|
value = create_string_buffer(valuelength)
|
||||||
|
timestampvalue = c_int()
|
||||||
|
|
||||||
|
if((dataTypes.value & TELLSTICK_TEMPERATURE) != 0):
|
||||||
|
success = lib.tdSensorValue(protocol.value, model.value, idvalue.value, TELLSTICK_TEMPERATURE, value, valuelength, byref(timestampvalue))
|
||||||
|
print "Temperature: ", value.value, "C,", datetime.fromtimestamp(timestampvalue.value)
|
||||||
|
|
||||||
|
if((dataTypes.value & TELLSTICK_HUMIDITY) != 0):
|
||||||
|
success = lib.tdSensorValue(protocol.value, model.value, idvalue.value, TELLSTICK_HUMIDITY, value, valuelength, byref(timestampvalue))
|
||||||
|
print "Humidity: ", value.value, "%,", datetime.fromtimestamp(timestampvalue.value)
|
||||||
|
|
||||||
|
print " "
|
||||||
|
|
||||||
|
lib.tdInit()
|
||||||
|
poll()
|
||||||
|
lib.tdClose()
|
Loading…
Add table
Add a link
Reference in a new issue