They're watching your every move

There are lots of fun ways to play with your phone: Angry Birds, Snapchat, Facebook, Face-swapping. Maybe you like to play with sensors? Anyone? Anyone? Well, in this post I will share some Python code and a video of how you can stream your Android's accelerometer data to your laptop and then visualize it. In fact, you can do this with any of the sensors in the unit, the accelerometer, gyroscope or the magnetometer.

An accelerometer is a sensor that every phone has. It measures the acceleration felt along three orthogonal axes. The left-right axis, the back-and-forth axis and the up-down axis. This forms a basis with which we can describe any acceleration felt in a three-dimensional space. Since gravity is always present, an accelerometer at rest will only measure the acceleration due to gravity. The figure below shows the grid present on my Android phone, and I assume an iPhone as well.

device_axesIn this configuration the readings of the accelerometer will be

X = 0.00, Z = 0.00, Y = 9.81

This is because gravity is pulling down on the y-axis with an acceleration of -9.81 m/s^2, so, in order to be still, the positive y-axis reading must be equal of strength and opposite of sign.



Most of use accelerometers every day and we don't know it. An accelerometer is the sensor in a phone that tells the display when to switch orientations from normal to landscape.rotate_screen_icon

Accelerometers are used in internal navigation of vehicles and airplanes. They help with autopilot of airplanes and determining the position of cars. They are also used to determine when to deploy an airbag by sensing a dramatic change in acceleration. In some washing machines they are used to detect load imbalances and are used to prevent your load from spinning out of control.

There are many applications in medicine.  Recently, I came across a paper where the authors used an accelerometer in a catheter to try and determine urological diagnosis and treatment. The ideas was to "see" how the urethra is moving when there is activity. Be warned:

They're watching your urethra.

In fact, your phone, and it's sensors may be quite insidious. Using your phone and it's sensors, it is possible to recreate your body's physical movements while you drive. Texting and driving? They can also be used to see through walls and potentially tell where you are in your own home. Here is a story where an accelerometer is used to deduce your ATM pin code. Pretty amazing!

Real-Time streaming of a phone's sensor

Using the android app Sensorstream, an app that streams your phone's data over WiFi, Python can be used to capture the data and visualize it. Python's socket package captures the data while matplotlib and numpy help us visualize it.

 ### make connection with phone 
host = ''
port = 5555 

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) 
s.bind((host, port))

On the app use the port settings need to match and the IP address needs to be the IP address your machine is logged onto. If you only want the Stream and no logs check the "UDP Stream." Once you switch it on the socket s will be capturing the data.

Once we capture the data we simply need to visualize the data. Python's Matplotlib is maybe not the best solution for this but we make it work 😉

The full code can be found at my GitHub page:

The relevant code to stream with is this:

while 1:
     message, address = s.recvfrom(8192) 
     messageString = message.decode("utf-8")
     Acc = messageString.split(',')[2:5]
     Acc = [float(Acc[i])/10.0 for i in range(3)]

Matplotlib's ability to plot fast streaming data is limited unless there are a few tweaks. Most of what I did came from this source. The code I have posted displays three graphs: the raw accelerometer data, the filtered accelerometer data, and the tilt angle. animated_accelerometer_phone_tilt_angles

Feel free to download and improve the code. Accelerometers are fun to play with and this is a great toy problem to learn sensor fusion and digital filtering. Enjoy!

Join over 5 people that are waiting day and night for latest from THE HOLY MATH.
We hate spam. Your email address will not be sold or shared with anyone else.