/SensorServer

Android app which stream phone's motion sensors to websocket clients ( PC , raspberry pi , arduino , webbrowser etc ) over Wi-Fi and USB

Primary LanguageJavaGNU General Public License v3.0GPL-3.0

SensorServer

Android app which streams phone's motion sensors to Websocket clients over Wi-fi or USB.

server connections sensors

This app streams phone's motion sensors to Websocket clients in realtime. A websocket client could be a web browser or any application running on a PC or a mobile device which uses Websocket Client API. You can easily get a Websocket library for your favourite programming language.

Usage

To receive sensor data, Websocket client must connect to the app using following URL.

             ws://<ip>:<port>/sensor/connect?type=<sensor type here> 

Value for the type parameter can be found by navigating to Available Sensors in the app.

For example

  • For accelerometer /sensor/connect?type=android.sensor.accelerometer .

  • For orientation /sensor/connect?type=android.sensor.orientation .

  • For step detector /sensor/connect?type=android.sensor.step_detector

  • so on...

Once connected, client will receive sensor data in JSON Array (float type values) through websocket.onMessage. Description of each data value at index in an array can be obtain from https://developer.android.com/guide/topics/sensors/sensors_motion

A snapshot from accelerometer

{
 "accuracy": 2,
 "timestamp": 3925657519043709,
 "values": [0.31892395,-0.97802734,10.049896]
}

where

Array Item Description
values[0] Acceleration force along the x axis (including gravity)
values[1] Acceleration force along the y axis (including gravity)
values[2] Acceleration force along the z axis (including gravity)

And timestamp is the time in nanoseconds at which the event happened

Use JSON parser to get these individual values.

Supports multiple connections to multiple sensors simultaneously

Many Websocket clients can connect to one type of a Sensor. So connecting to /sensor/connect?type=android.sensor.accelerometer three times will create three different connections to the accelerometer sensor and each connected client will then receive accelerometer data at the same time.

Moreover, from one or different machines you can connect to different types of sensors as well i-e one Websocket Client object could connect to accelerometer and other Websocket Client object to gyroscope. All active connections can be viewed by selecting Connections navigation button.

Test with Websocket testing tools

Before writing your own websocket client, test this app with any websocket testing tools available on the web or playstore. You can use http://livepersoninc.github.io/ws-test-page/ for testing purpose.

Sample Websocket client (python)

Here is a simple websocket client in python using websocket-client api which receives live data from accelerometer sensor.

import websocket
import json

def on_message(ws, message):
    accelerometer = json.loads(message)['values']
    x = accelerometer[0]
    y = accelerometer[1]
    z = accelerometer[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()

There is another python websocket API which is based on asyncio https://github.com/aaugustin/websockets

import asyncio
from websockets import connect

async def accelerometer(uri):
    async with connect(uri) as websocket:
        while True:
            data = await websocket.recv()
            print(data)
            
URI = "ws://192.168.0.101:8081/sensor/connect?type=android.sensor.accelerometer"
asyncio.run(accelerometer(URI))

Connecting over USB (using ADB)

To connect over USB make sure USB debugging option is enable in your phone and adb is available in your machine

  • Step 1 : Enable Local Host option in app
  • Step 2 : Run adb command adb forward tcp:8081 tcp:8081 (8081 is just for example)
  • Step 3 : use address ws://localhost:8081:/sensor/connect?type=<sensor type here> to connect

APK Download ⏬

Download latest APK from Release page (requires Android 5.0) . Don't forget to ⭐ to this repo if you found this helpful

Issues and Contributions

Post a new issue if you encounter any. To contribute, fork this repo then create feature/fix branch and send pull request for that branch.