Skip to content

Stream real-time Android sensor data over UDP with low-latency

License

Notifications You must be signed in to change notification settings

umer0586/SensaGram

Repository files navigation

SensaGram

GitHub License Android Badge Jetpack Compose Badge Static Badge GitHub Actions Workflow Status GitHub Release

Stream real-time Android sensor data using UDP (User Datagram Protocol), a connectionless transport layer protocol designed for low-latency, fast data transmission without establishing a persistent connection.

Usage

In the app, select the desired sensors from the list and tap the "Stream" button. This will begin transmitting sensor data to the specified address. To receive the data, you'll need to set up a UDP server. The app sends the data in JSON format.

Note : You can dynamically control the data stream by selecting or deselecting sensors from the list during active streaming.This allows for flexible management of sensor data transmission.

{
 "type": "android.sensor.accelerometer",
 "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.

Note : Refer to the following official Android documentation links to understand what each value in the values array represents.

Python example

The following Python code allows you to receive sensor data from the Sensagram app. The server will listen on all available network interfaces, which is indicated by the address 0.0.0.0. To set up the server, follow these steps:

1. Clone the Gist repository:

see : udpserver.py

git clone https://gist.github.com/umer0586/1331ac524c525bae7b1c94667ed571de example
cd example

2. Create a Python file (e.g., server.py) in the example directory and add the following code:

from udpserver import UDPServer
import json


def onData(data):
    jsonData = json.loads(data)
    sensorType = jsonData["type"]
    timestamp = jsonData.get("timestamp")
    values = jsonData.get("values")
    
    if sensorType == "android.sensor.accelerometer":
        x, y, z = values
        print(f"accelerometer : x = {x}, y = {y}, z = {z} timestamp = {timestamp} ")
    
    if sensorType == "android.sensor.gyroscope":
        x, y, z = values
        print(f"gyroscope : x = {x}, y = {y}, z = {z} timestamp = {timestamp} ")

    if sensorType == "android.gps":
        longitude, latitude, altitude = jsonData["longitude"], jsonData["latitude"], jsonData["altitude"]
        bearing, accuracy, speed, time	= jsonData["bearing"], jsonData["accuracy"], jsonData["speed"], jsonData["time"]
        
        print(f"longitude = {longitude} latitude = {latitude} altitude = {altitude}")
        
        # Fields only for Android 8.0 and above.
        speedAccuracyMetersPerSecond = jsonData.get("speedAccuracyMetersPerSecond")
        bearingAccuracyDegrees = jsonData.get("bearingAccuracyDegrees")
        elapsedRealtimeNanos = jsonData.get("elapsedRealtimeNanos")
        verticalAccuracyMeters = jsonData.get("verticalAccuracyMeters")
                

# Initialize the server to listen on all network interfaces (0.0.0.0) and port 8080
server = UDPServer(address=("0.0.0.0", 8080))
server.setDataCallBack(onData)
server.start()

for GPS data description see GPS Data Description

3 Run the script on the machine you want to receive data on:

python server.py

4. Configure the Sensagram app:

In the app's settings, enter the IP address of the machine running this script. To find your machines's IP address:

  • On Windows, use the ipconfig command.
  • On Linux, use the ifconfig command.

Stream On Boot

The app can stream sensor data to a specified address upon device boot. For devices running Android 9 or lower, the app automatically enables Wi-Fi on boot and starts the data stream. However, for devices running Android 10 and later, apps cannot directly control Wi-Fi settings. Therefore, you need to ensure your device's Wi-Fi is enabled before shutting down or restarting. This allows the system to automatically restore the Wi-Fi state when the device boots up. Additionally, make sure the app is not restricted from performing background activities, which you can adjust through your device's system settings.

Video Demo

A demonstration of Sensagram's sensor data streaming capabilities via UDP, utilizing a Python script within Blender to map phone orientation and dynamically control the rotation of a 3D object. See rotate.py

IMAGE ALT TEXT HERE

Installation

Get it on F-Droid

OR

Download apk from release section.