在 运行 时使用来自 Android 手机的 Websocket 数据
Utilizing the Websocket Data from Android cellphone while running
使用 SensorServer,我在 android phone 和 Python 之间建立连接并利用发送的数据。 Android 设备的加速度计用于确定多边形位置。
传感器服务器代码:
import websocket
import json
def on_message(ws, message):
values = json.loads(message)['values']
x = values[0]
y = values[1]
z = values[2]
print('x =',x,'y = ',y,'z = ',z)
def on_error(ws, error):
print("error occurred")
print(error)
def on_close(ws, close_code, reason):
print("connection close")
print("close code : ", close_code)
print("reason : ", reason )
def on_open(ws):
print("connection open")
if __name__ == "__main__":
ws = websocket.WebSocketApp("ws://192.168.0.101:8081/sensor/connect?type=android.sensor.accelerometer",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()
'x-axis'、'y-axis' 和 'z-axis' 是我的主要用途。但由于两个主要限制,我很难使用这些数据:
首先,是在程序运行时处理数据 运行ning - 像这样说:
#Ensure that the polygon will not cross the 2:-2 outline
if x > 2:
x = 2
if x < -2:
x = -2
其次,由于 ws.run_forever()
的无限循环,我无法 运行 定义多边形位置的任何其他代码:
polygon = visual.ShapeStim(
win=win, name='polygon',
size=(z), vertices='triangle',
ori=0.0, pos=[x,y], anchor='center',
lineWidth=1.0, colorSpace='rgb', lineColor='white', fillColor='white',
opacity=None, depth=-2.0, interpolate=True)
所以,我需要帮助来解决这些并发症
预先感谢评论。
我使用了这个代码:
ws = websocket.WebSocket()
ws.connect("ws://10.100.102.17:8081/sensor/connecttype=android.sensor.gyroscope_uncalibrated")
while True:
if ws.getstatus() == None:
pass
else:
break
rc = ws.recv()
rc = json.loads(str(rc))
rc = rc['values']
polygon = visual.ShapeStim(
win=win, name='polygon',
size=(rc[0]), vertices='triangle',
ori=0.0, pos=[rc[1],rc[2]], anchor='center',
lineWidth=1.0, colorSpace='rgb', lineColor='white', fillColor='white',
opacity=None, depth=-2.0, interpolate=True)
使用 SensorServer,我在 android phone 和 Python 之间建立连接并利用发送的数据。 Android 设备的加速度计用于确定多边形位置。
传感器服务器代码:
import websocket
import json
def on_message(ws, message):
values = json.loads(message)['values']
x = values[0]
y = values[1]
z = values[2]
print('x =',x,'y = ',y,'z = ',z)
def on_error(ws, error):
print("error occurred")
print(error)
def on_close(ws, close_code, reason):
print("connection close")
print("close code : ", close_code)
print("reason : ", reason )
def on_open(ws):
print("connection open")
if __name__ == "__main__":
ws = websocket.WebSocketApp("ws://192.168.0.101:8081/sensor/connect?type=android.sensor.accelerometer",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()
'x-axis'、'y-axis' 和 'z-axis' 是我的主要用途。但由于两个主要限制,我很难使用这些数据:
首先,是在程序运行时处理数据 运行ning - 像这样说:
#Ensure that the polygon will not cross the 2:-2 outline
if x > 2:
x = 2
if x < -2:
x = -2
其次,由于 ws.run_forever()
的无限循环,我无法 运行 定义多边形位置的任何其他代码:
polygon = visual.ShapeStim(
win=win, name='polygon',
size=(z), vertices='triangle',
ori=0.0, pos=[x,y], anchor='center',
lineWidth=1.0, colorSpace='rgb', lineColor='white', fillColor='white',
opacity=None, depth=-2.0, interpolate=True)
所以,我需要帮助来解决这些并发症 预先感谢评论。
我使用了这个代码:
ws = websocket.WebSocket()
ws.connect("ws://10.100.102.17:8081/sensor/connecttype=android.sensor.gyroscope_uncalibrated")
while True:
if ws.getstatus() == None:
pass
else:
break
rc = ws.recv()
rc = json.loads(str(rc))
rc = rc['values']
polygon = visual.ShapeStim(
win=win, name='polygon',
size=(rc[0]), vertices='triangle',
ori=0.0, pos=[rc[1],rc[2]], anchor='center',
lineWidth=1.0, colorSpace='rgb', lineColor='white', fillColor='white',
opacity=None, depth=-2.0, interpolate=True)