-
Notifications
You must be signed in to change notification settings - Fork 32
Using Multiple Sensors On Single Websocket Connection
Umer Farooq edited this page Jul 29, 2024
·
2 revisions
To use multiple sensors over single websocket connection use following URL.
ws://<ip>:<port>/sensors/connect?types=["<type1>","<type2>","<type3>"...]
By connecting using above URL you will receive JSON response containing sensor data along with a type of sensor
{
"accuracy": 2,
"timestamp": 3925657519043709,
"values": [0.31892395,-0.97802734,10.049896]
"type" : "sensor type here"
}
import websocket
import json
def on_message(ws, message):
values = json.loads(message)['values']
type = json.loads(message)['type']
print("type = ", type)
print("values = ",values)
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("connected")
def connect(url):
ws = websocket.WebSocketApp(url,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()
connect('ws://192.168.0.101:8080/sensors/connect?types=["android.sensor.accelerometer","android.sensor.gyroscope","android.sensor.light","android.sensor.magnetic_field"]')
Also See Connecting to Multiple Sensors Using Threading in Python