2

I am looking to transmit sensor data (eg accelerometer data) from my android phone over to my pc wirelessly.

I have currently created a very basic app that displays the accelerometer data on screen. I have seen the answers in this thread How to transmit Android real-time sensor data to computer?.

However, I wish to be able to fit it into the basic current accelerometer app I have, which is edited from the basic activity template as well as code from this link https://www.javatpoint.com/android-sensor-tutorial, and I would also like to look at the python code itself.

Apologies in advanced if the information is insufficient. This is my first time posting here.

Shrikant Havale
  • 1,250
  • 1
  • 16
  • 36
fatbringer
  • 348
  • 2
  • 9
  • 19
  • A client and a server using sockets i would say. What else? – blackapps Jun 25 '21 at 14:28
  • Will the android phone be hosting the server ? And say.. the python script subscribes to it ? – fatbringer Jun 26 '21 at 15:03
  • You can do what you want. But the first thing that comes ro mind is that the pc would host the server. – blackapps Jun 26 '21 at 16:04
  • I need to use the android device as the host actually. If i want to send data to a pc wirelessly, do i send it to the server (itself), or the pc ? – fatbringer Jun 30 '21 at 07:15
  • You cannot just send data to a device. To a pc. Only to an app or program running on that device. You want a client on your pc. So the client has to connect to the server app on your Android device before the server app can send data to that client. – blackapps Jun 30 '21 at 07:54
  • I have found something like a server app that i can adapt in this link. https://github.com/sonuauti/Android-Web-Server. But i am confused whether do i have to connect a socket to the server on my android device ? How do i write the data into the server ? – fatbringer Jun 30 '21 at 08:14
  • `something like a server app` Well that is a webserver. Clients af webservers are browsers. – blackapps Jun 30 '21 at 09:20
  • How should i go about posting the sensor data on the webserver webpage ? So that maybe i can use the pythons script to scrape it from there ? – fatbringer Jul 01 '21 at 09:51
  • You should not use a webserver to begin with. And further you should first read and read and read about such things. – blackapps Jul 01 '21 at 17:06
  • Could you provide some pointers about what are some of such things I should read and read up on ? – fatbringer Jul 03 '21 at 01:15
  • About clients, servers, sockets and exchanging data. – blackapps Jul 03 '21 at 09:26
  • ok i got it settled. Thanks for the advice :) – fatbringer Jul 14 '21 at 04:24

3 Answers3

3

You can use Sensor Server app on Github, that runs Websocket server and sends live sensor data to WebSocket clients.

To receive live data from Accelerometer sensor, you simply need to connect to the app using following URL

ws://ip:port/sensor/connect?type=android.sensor.accelerometer

To receive live data in Python script you can use WebSocket client for Python

import websocket
  
def on_message(ws, message):
    print(message) # sensor data here in JSON format

def on_error(ws, error):
    print("### error ###")
    print(error)

def on_close(ws, close_code, reason):
    print("### closed ###")
    print("close code : ", close_code)
    print("reason : ", reason  )

def on_open(ws):
    print("connection opened")
    

if __name__ == "__main__":
    ws = websocket.WebSocketApp("ws://192.168.0.102:8082/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()

Sensor Server app and Websocket client must be connected to the same network

Umer Farooq
  • 762
  • 1
  • 8
  • 17
3

I don't know if you've found the solution yet. But for anyone looking for directions. Now there are 2 ways to communicate between 2 machines wirelessly (in your case Android and PC), UDP and TCP. If you want to ensure data integrity and transmission, use TCP. Socket is the class that you need. In this case it doesn't matter who is client and server, as the communication is both ways, like a chat application. If you use UDP, DatagramSocket is the class to go to. It doesn't guarantee data integrity and transmission but is faster than the former.

It's recommended to use UDP for sensor data transmission as the data generated by sensor is a continuous stream and must be transmitted immediately to avoid blocking. Read up on the official documents and you will be good to go.

TCP official docs

UDP official docs

Batman
  • 196
  • 1
  • 8
  • Hi yes. So sorry for not updating. Yes i have found the answer, and it is using socket for both sides on the android app as well as a python script on my pc. – fatbringer Nov 05 '21 at 05:51
2

So i found out awhile back, sockets and servers were the way to go

On python side:

import socket
import sys

HOST="your ip address"
PORT= some_port

ss=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print('Socket created')

ss.bind((HOST,PORT))
print('Socket bind complete')

ss.listen(10)
print('Socket now listening')

conn, addr = ss.accept()
print("socket accept")

On the android side:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

...
...

        Runnable threadname = new Runnable() {
        @Override
        public void run() {
            try {
                textView2.append("threadname is running\n");
                String SERVER_ADDRESS = "your ip address";
                int SERVER_PORT = your port;

                if (clientSocket == null) {
                    clientSocket = new Socket(InetAddress.getByName(SERVER_ADDRESS), SERVER_PORT);
                    SocketAddress socketAddress = new InetSocketAddress(SERVER_ADDRESS, SERVER_PORT);

                    if (!clientSocket.isConnected() ) {
                        clientSocket.connect(socketAddress);
                    }
                    if (!clientSocket.isBound()) {
                        clientSocket.bind(socketAddress);
                    }
                    if (clientSocket.isClosed()) {
                    }
                }

                DataOutputStream DOS = new DataOutputStream(clientSocket.getOutputStream());
                DOS.writeBytes(stufftosend);

fatbringer
  • 348
  • 2
  • 9
  • 19