April 11, 2021

Android phone sensor data into Socket.io / Node.js

I discovered the Android app Termux that I think is really interesting from many perspectives. Termux gives you a linux terminal access to a decent portion of your android phone without having to crack it completely.

On the one hand I think this is cool as with a hardware keyboard this is the cheapest laptop since many people already have it. Also it is essentially a raspberry-pi, already put in a tight package with integrated bluetooth, wifi, battery and HDMI screen. And it can obviously do so much more than just what I am thinking of.

So first two things I did was to get Node js running in on my phone, and test out the sensor output data. Then I combined the two in a first project where node reads the sensor data and then sends it with socket.io. See the git project with first (rough) documentation.

git project :
Android sensor socket.io

This first project demonstrates node.js using the termux API Android-commands to read sensor data and send it via socket.io. But this could be expanded for all the API commands, of which there are many.

This also made me realise that it would be easy with socket.io and node-cmd to make a primitive browser console interface to a machine. That would of course be completely the same in the end as just connecting with SSH, but in our reviews this was usually the last option, so an emergency terminal interface to run a restart command could be a lifesaver.

i. intertial motion sensor data to socket server

So currently this runs node.js through termux on the android phone to read sensor data and send as socket.io client. A socket.io and web server receives this, runs sensor fusion algorithm and forwards the data, a demo web-gui displays this data as a first test.
This could probably be reduced to the android phone in termux too but I am not yet sure how processing intensive the algorithm is so data-rate might need to be reduced / adjusted.

This is the demo / test page for sensor data.

ii. android node is socket server

The second iteration is way simpler for now with 1 android device and different GUIs. The node script on the android phone reads the data and sets up a socket.io server. Any socket client that connects to it will receive the sensor data. For this check the /termux_node folder.
Copy the ‘termux_node.js’ script onto the android device and run with node.js. Then try out the different ‘_.html’ GUI’s to receive the data and display in HTML, paper and p5.

iii. Three.js

It finally occured to me to use the three.js 3D library to animate a real cube.
See the box demo from three.js documentation. My box demo is here.

I then simply combined the two codes to produce : three_test.html

This runs socket.io client and connects to the socket node running on your android device. Simply correct the IP address. The Termux_node sends it the Euler angles of the Madgwick sensor fusion algorithm and these are used to orient the box. Simples.

Leave a Reply