Phidgets and WebSockets

Supporting 2.7 and 3.2+
amacdonald
Phidgetsian
Posts: 6
Joined: Wed Nov 30, 2011 10:02 am
Contact:

Phidgets and WebSockets

Postby amacdonald » Fri Jan 13, 2012 1:09 pm

Hello

I am trying out the idea that I can connect phidget sensor output (the circular touch interface to be exact) to be transmitted over a websocket.

So far I have the websocket working just fine - information can be sent and received. However, being new to Python I am having difficulty getting the constant sensor data out of the Event handler and into the websocket message.

So far all I have is a stripped down version of the Python Touch example with the websocket code which works just fine.

Reading the Python documentation I understand that the data is in the PhidgetDataEvent - but how to I access this from outside the EventHandler and send a constant stream of data to the websocket code.

What I am trying to do is use the Phidget Touch sensor (or any sensor data) to feed into a website (using the websockets). This should allow me to manipulate objects on the website (local only for a "kiosk" environment), e.g. make boxes grow and shrink, move images around.

My question (after all that) is twofold. A) Can it be done this way (if not is there a better method for web interaction) and B) If it can be done, how?

Running the system on OS X

Thanks for any help.

Code: Select all


#!/usr/bin/python

#Basic imports
from mod_pywebsocket import msgutil
from datetime import datetime
import time
from ctypes import *
import sys
import random

#Phidget specific imports
from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException
from Phidgets.Events.Events import AttachEventArgs, DetachEventArgs, ErrorEventArgs, InputChangeEventArgs, OutputChangeEventArgs, SensorChangeEventArgs
from Phidgets.Devices.InterfaceKit import InterfaceKit

#Create an interfacekit object
try:
    interfaceKit = InterfaceKit()
except RuntimeError as e:
    exit(1)

#Event Handler Callback Functions
def inferfaceKitAttached(e):
    attached = e.device

def interfaceKitDetached(e):
    detached = e.device

def interfaceKitSensorChanged(e):
    source = e.device
    print("%i" % (e.value))

# Create the Web socket
def web_socket_do_extra_handshake(request):
    print 'Connected.'
    pass  # Always accept.

# Send the information over the socket
def web_socket_transfer_data(request):
    while True:
        time.sleep(1)
        date = datetime.now()
        msgutil.send_message(request, 'clock!%s' % date)

   
#Main Program Code
try:
    interfaceKit.setOnAttachHandler(inferfaceKitAttached)
    interfaceKit.setOnDetachHandler(interfaceKitDetached)
    interfaceKit.setOnSensorChangeHandler(interfaceKitSensorChanged)
except PhidgetException as e:
    exit(1)


try:
    interfaceKit.openPhidget()
except PhidgetException as e:
    exit(1)


try:
    interfaceKit.waitForAttach(10000)
except PhidgetException as e:
   
    try:
        interfaceKit.closePhidget()
    except PhidgetException as e:
        exit(1)
    exit(1)

try:
    interfaceKit.closePhidget()
except PhidgetException as e:
    exit(1)

erik
King of the Lab
Posts: 476
Joined: Fri Mar 06, 2009 12:42 pm
Location: Calgary, Canada
Contact:

Re: Phidgets and WebSockets

Postby erik » Fri Jan 13, 2012 2:21 pm

In the Event for the sensors, assign e.value to a global variable that both the sensor event and the websocket can access.

amacdonald
Phidgetsian
Posts: 6
Joined: Wed Nov 30, 2011 10:02 am
Contact:

Re: Phidgets and WebSockets

Postby amacdonald » Fri Jan 13, 2012 2:54 pm

I tried that but it doesn't seem to work. Then again it could just be my understanding of Python at the moment. I pasted the changed code below.

The only output I get (looking at my console output) is '0' repeated once a second (because of the time.sleep). It seems that the websocket code is reading the variable once (the original value) and never reading the updated version.


Code: Select all

#!/usr/bin/python

#Basic imports
from mod_pywebsocket import msgutil
from datetime import datetime
import time
from ctypes import *
import sys
import random

#Phidget specific imports
from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException
from Phidgets.Events.Events import AttachEventArgs, DetachEventArgs, ErrorEventArgs, InputChangeEventArgs, OutputChangeEventArgs, SensorChangeEventArgs
from Phidgets.Devices.InterfaceKit import InterfaceKit

#Create an interfacekit object
try:
    interfaceKit = InterfaceKit()
except RuntimeError as e:
    exit(1)

#Event Handler Callback Functions
def inferfaceKitAttached(e):
    attached = e.device

def interfaceKitDetached(e):
    detached = e.device

position = 0

def interfaceKitSensorChanged(e):
    global position
    position = e.value

def web_socket_do_extra_handshake(request):
    print 'Connected.'
    pass  # Always accept.

def web_socket_transfer_data(request):
    while True:
        time.sleep(1)
        #date = datetime.now()
        msgutil.send_message(request, 'clock!%s' % position)

#Main Program Code
try:
    interfaceKit.setOnAttachHandler(inferfaceKitAttached)
    interfaceKit.setOnDetachHandler(interfaceKitDetached)
    interfaceKit.setOnSensorChangeHandler(interfaceKitSensorChanged)
except PhidgetException as e:
    exit(1)

try:
    interfaceKit.openPhidget()
except PhidgetException as e:
    exit(1)

try:
    interfaceKit.waitForAttach(10000)
except PhidgetException as e:
   
    try:
        interfaceKit.closePhidget()
    except PhidgetException as e:
        exit(1)
    exit(1)


try:
    interfaceKit.closePhidget()
except PhidgetException as e:
    exit(1)



erik
King of the Lab
Posts: 476
Joined: Fri Mar 06, 2009 12:42 pm
Location: Calgary, Canada
Contact:

Re: Phidgets and WebSockets

Postby erik » Fri Jan 13, 2012 4:41 pm

I think you might be closing your InterfaceKit almost immediately, and that's why the same value is getting passed through the socket. I will admit that I don't really know much about Python, but it seems that the main program will open a connection to the InterfaceKit, but then closes the connection soon afterwards.

Try putting a time.sleep(10000) before the last try block.

When you had "print("%i" % (e.value))" was it constantly displaying the changing values?

amacdonald
Phidgetsian
Posts: 6
Joined: Wed Nov 30, 2011 10:02 am
Contact:

Re: Phidgets and WebSockets

Postby amacdonald » Fri Jan 13, 2012 7:08 pm

"print("%i" % (e.value))" works as it should. The websocket also works; they don't work together very well yet.

I'll have to wait to try until next week when I can get back to this. I will modify the code and give it a try.

amacdonald
Phidgetsian
Posts: 6
Joined: Wed Nov 30, 2011 10:02 am
Contact:

Re: Phidgets and WebSockets

Postby amacdonald » Mon Jan 16, 2012 2:43 pm

I was closing the Phidget immediately, thanks for pointing that out, works like a charm now. Thanks.


Return to “Python”

Who is online

Users browsing this forum: No registered users and 1 guest