Python Socket transfer speed too low - android

I'm running Python on my Android 2.3 powered HTC phone using SL4A ... so I wrote a code to retrieve accelerometer values and send them to my pc via Sockets ! The problem is that on phone the code gets values each 100ms and I'm using a while loop to keep sending them to PC. But the pc doesn't get the values at the same speed, I mean it's slower, and just to keep in mind that I modified the values to be like so :
> [0.0,1.0,-5.0]
> [0.0,2.0,-2.0] a list of Rounded floats
> ...
and here is my server code (running on pc):
import socket
HOST = ""
PORT = 55600
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
print data
if not data: break
conn.close()
Any help to make it faster ?

The first thing I would change is getting rid of the print statement in your loop. IO is very slow. This is, of course, assuming you're planning on doing something with these values besides display them to the screen ;)

Related

ELM327 Bluetooth communication issue

I made an Android app to communicate with an ELM327 OBD-II dongle via bluetooth.
When I test the app with a bluetooth serial terminal (CoolTerm / macOS) the app is receiving and sending data without an issue.
Using my app to transfer commands to the ELM will result in broken and splitted answers.
Output:
DataReceivedHandler: 9V
DataReceivedHandler: 9V>
DataReceivedHandler: 11.
DataReceivedHandler: 9V>
The example above should show the result of the AT RV command which queries the battery's voltage of the vehicle (I sent the command multiple times).
Expected (good) result should look like this: >11.9V
Before I send the first command to query OBD values the ELM is initialized by these AT-commands:
AT D
AT Z
AT E0
AT L0
AT S0
AT H0
AT SP 0
If you have any idea how to get clear answers from the ELM, please let me know.
Thanks in advance!
Found the solution..
The terminal program was sending \n and the ELM sends \r for the termination of the statement.

Getting Raspberry pi events from android/iOS app

I just got started with Raspberry pi and I wanted to make a program on my Raspberry which gets input from an app on my Android/iOS device over bluetooth. I wanted to first check if something like this is possible and second if you have any clues on how to do something like this.
Thanks
PS: Since I just got started I'm only looking for clues and I don't want anyone to write and app for me so don't down vote
You would likely need to establish a network communication between the Raspberry Pi and the device.
For the server:
import socket
HOST = '' # This should receive from all available interfaces.
PORT = 1111 # Random port number.
data = "Test" # Data to send to the client.
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((HOST, PORT))
while True:
s.sendto(data, (HOST, PORT))
print data
On the client, very similar code but add:
data, addr = s.recvfrom(1024)
print addr
print "Message received: ", data
Under the while True
Reference the following for setting up RPI wireless hotspot:
http://elinux.org/RPI-Wireless-Hotspot
The HOST for each is going to be the device IPv4 address, usually in format:
192.168.x.x.
I have not personally found a way without using serials for data communication. The most common way to communicate information between devices is over network. Look into peer-to-peer network solutions.
The code may not work as is, you will need to make client/server-side code specific for your needs.
Reference the following for setting up sockets and a low-level network interface: https://docs.python.org/2/howto/sockets.html
Hopefully this helps.
EDIT:
There is a Bluetooth method for RPI.
Here's a good branch in Github that contains example of the Bluetooth library used in Python:
https://github.com/karulis/pybluez/blob/master/examples/simple
Good references:
Bluetooth programming with Python.
http://people.csail.mit.edu/albert/bluez-intro/c212.html
How to create a Bluetooth tag with RPI.
https://www.raspberrypi.org/magpi/create-a-raspberry-pi-3-bluetooth-tag/

Running Python socket script through ADB

I have a somewhat unusual problem. I want to run some code on my android smartphone via the ADB (which along with SL4A is already set up) and then I want it to act as a server and respond with the program's results while my host machine acts as a client and receives this data. However, I am not an expert in socket operations and I'm getting results I don't understand. My code is as follows:
from device import android
import socket
droid = android.Android()
# Open socket
s = socket.socket()
HOST = socket.gethostname()
PORT = 8000
s.bind((HOST, PORT))
s.listen(5)
print "HOST = ", HOST, ", PORT = ", PORT
# Accept instructions
c, addr = s.accept()
print "Got connection from", addr
data = ""
while True:
chunk = c.recv(1024)
if (not chunk):
break
else:
data.extend(chunk)
s.close()
# DO STUFF WITH RECEIVED INPUT
# Send result
c, addr = s.accept()
print "Got connection from", addr
while True:
if (c.send(str(result)):
break
s.close()
The code I'm running on the client, my host machine, is as follows:
s = socket.socket()
HOST = "localhost"
PORT = 8000
s.connect((HOST, PORT))
s.send(data)
s.close()
# IN A DIFFERENT FUNCTION
s = socket.socket()
HOST = "localhost"
PORT = 8000
try:
s.connect((HOST, PORT))
results = s.recv(1024)
s.close()
return results
except Exception:
return False
The environment has been set up this way:
set AP_PORT=54023
adb forward tcp:%AP_PORT% tcp:%AP_PORT%
So, how this is supposed to work is this: the server is set up to run on my Android smartphone and accept input to run the program on my Android smartphone from another program running on my PC. After the Android program has finished running, it should send the result back to my PC.
However, I'm having several problems. I can't seem to get the first connection open. I either get 10061's or 10049's and I'm not sure why that is. I've looked at the existing documentation available and I'm still not getting results. I'm also not sure if this code is actually running on the Android device and this is very important to my application so I really need some sort of confirmation on this.
Any enlightenment would be greatly appreciated.
The server program never ends until it reaches an exception, because you set up an inf loop: while True.
c.close() close the current connection, then the loop starts again and the program waits for another connection to accept.
The host program instead should end after the message is sent or when it reaches an exception.
What do you mean with: " I'm getting results I don't understand... This code runs without error... The problem I'm having is that I'm not getting any results back"?
What results do you get? Have you checked the data var?

TCP/IP on an Android Device using Python Socket

I have a Python script that is running on Android-SL4A that returns the values from the orientation sensor. I would like to transfer those values as text to a raspberry pi 2B+. While I have some Python skills I am unfamiliar with TCP/IP but I found this code below, and similar code to run on the receiving device. While testing the code below, the listening code on a Windows PC appears to be listening just fine but when I execute the sending code (posted below) on the Android device it gives a error at
s.connect((TCP_IP, TCP_PORT))
I receive an error(Errno 111) that the connection was refused.
Any help debugging would be great.
import time, math, sys, traceback
import socket
print "imports done"
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 1024
MESSAGE = "Hello, World!"
print "settings defined"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "socket created"
s.connect((TCP_IP, TCP_PORT))
print "connection made"
print "message sent"
data = s.recv(BUFFER_SIZE)
s.close()
print "received data:", data
print "All Done"
If you don't want to learn how low-level sockets work, you can always use someone else's library.
The popular ones with TCP support are Tornado and Twisted, but I suggest you use simpleTCP, as it doesn't require you learn an entire framework to simply send information over TCP. The project website has the 5-line program for an echo server and an even smaller example of client sockets on its front page.

Send AT-command through bluetooth with python

I try to send AT commands from my computer (ubuntu 13.04) to my phone (Android 5.1) via bluetooth. I want to read the SMS.
I retrieve the MAC address of my phone with :
hcitool scan
I browse all available services on the device with :
sdptool browse XX:XX:XX:XX:XX:XX
I get the good RFCOMM channel for SMS/MMS service and now I'm trying to send the AT command.
I tried with pySerial with a bound and connected rfcomm to my phone but no response :
import serial
phone = serial.Serial('/dev/rfcomm0', 115200, timeout=2)
phone.write(b'AT\r')
data = phone.readall()
print data
I tried the same code on a USB serial port and I have a response :
import serial
phone = serial.Serial('/dev/ttyACM0', 115200, timeout=2)
phone.write(b'AT\r')
data = phone.readall()
print data
# *EMRDY: 1
# AT
# OK
I tried with pyBluez but same problem, no response of my AT command :
import bluetooth
client_sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
client_sock.connect(('XX:XX:XX:XX:XX:XX', 4))
client_sock.send(b'AT\r')
data = client_sock.recv(1024)
print "received [%s]" % data
And I finally tried with native python sockets, but no response :
import socket
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.connect(('XX:XX:XX:XX:XX:XX',4))
s.send(b'AT\r')
data = s.recv(1024)
s.close()
print('Received', repr(data))
Note: The phone displays a prompt window to accept that my computer accesses my sms. Of course I accepted.
Can anyone tell me what is wrong and what I can try?
Well, for starter it is better to check first that you have a two-way communication between your Host computer and your Phone on bluetooth, like you said, it did work with USB, then there should be no reason it does not with bluetooth unless you didn't yet established a good communication, so I think it is better to try first that you have good communication by just sending and replying with the same string (kinda hand-shaking protocol) and make sure that you know what your python code is actually sending, may be unseen extra characters using bluetooth that you don't pay attention to, which makes your AT command unrecognizable by your phone.

Categories

Resources