# 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.

In 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.

### Applications

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.

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:

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.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: https://github.com/theholymath/acceleromter-filter-testing/blob/master/sensorstream_live_stream_acceleration.py.

The relevant code to stream with is this:


while 1: