Code Monkey home page Code Monkey logo

androidsensorserver's Introduction

SensorServer

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

server connections sensors

This app can stream phone's sensors (motion, environmental and position) 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.

Note : Use this page to know what each value in values array corresponds to

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

axis_device

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.

Undocumented (mostly QTI) sensors on Android devices

Some Android devices have additional sensors like Coarse Motion Classifier (com.qti.sensor.motion_classifier), Basic Gesture (com.qti.sensor.basic_gestures) etc which are not documented on offical android docs. Please refer to this Blog for corresponding values in values array

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):
    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()
 
 # To analyse multiple sensor data simultaneously, you can add as many websocket connections for different sensors as you want. 

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))

# Add more connections to other sensors

Connecting over USB (using ADB)

To connect over USB make sure USB debugging option is enable in your phone and ADB (android debug bridge) 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) from client
  • Step 3 : use address ws://localhost:8081:/sensor/connect?type=<sensor type here> to connect

Make sure you have installed your android device driver and adb devices command detects your connected android phone.

APK Download ⏬

Download latest APK from Release page (requires Android 5.0) .

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.

androidsensorserver's People

Contributors

umer0586 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.