We will use an Android phone that has ton of sensors such as GPS, accelerometer, gyro, barometer, light sensor, camera, mic, etc. to generate the data, Node-RED to capture and process the data with least amount of coding, and use Power BI services to visualize the data in a distributable dashboard.
To follow this tutorial you will need some very basic experience in bash programming and basic knowledge of JavaScript.
Step 1: Install Termux & Termux APIDownload these two required apps from the play store, following are the links.
https://play.google.com/store/apps/details?id=com.termux
https://play.google.com/store/apps/details?id=com.termux.api
Step 2: Install Node-RED & Termux API Module on TermuxNow open the Termux App and run the following commands to install Node-RED
apt update
apt upgrade
apt install termux-api
apt install coreutils nodejs
npm i -g --unsafe-perm node-red
If you are running the latest version of Android you might face an error on the “npm I -g --unsafe-perm node-red” line saying “cannot read property ‘length’ of undefined”. Run the following commands if you face them.
apt-get install yarn
yarn global add npm
Step 3: Launch Node-REDNow launch Node-RED:
node-red
Node-RED is now running on localhost on the Android phone; it will be showing the IP address and port it is running on in Termux.
On your phone you can point a browser to localhost:1880 and it will open up Node-RED flows editor. But for practical purposes you'll have to open it up on a bigger screen such as a laptop.
To access the flows editor on a browser from a laptop running on the same network as the phone, you can do it in two ways.
One, your laptop/desktop has to be connected to the same WiFi router as the phone is connected to.
Two, you can run a WiFi hotspot from your phone and connect to the hotspot from your laptop.
Now type in ifconfig in Termux and get your phone's IP address.
Add a colon and port number of “1880” to the ip address and enter it into a browser from the laptop/desktop. In my case it was 192.168.100.2:1880.
Open a new session in Termux; swipe left drawer and click new session to open a new session. Switch between the session from the same left drawer menu. You can discontinue a termux-process running in the current session by pressing phone's Volume Down + c in the keyboard.
To get a list of available sensor in the phone, type in Termux:
termux-sensor -l
For Nexus 5X I got the following list:
This list will vary based on handset model.
Termux will print the value of all the sensors continuously if you type:
termux-sensor -a
This eats up a lot of battery resource though, so better not keep it running for long. To print it once, type in:
termux-sensor -a -n 1
To get all the list of options available, just type in:
termux-sensor
Step 5: Filter Light SensorLight sensor is typically placed beside front camera (selfie camera) along with proximity sensor, and pretty much available in all handset.
I’ll take only the light sensor data to showcase how to extract and pipe this data.
To filter out the light sensor in termux-sensor, find the name from the list of printed sensor, in my case I picked "RPR0521 Light Sensor" in my Nexus 5X. To get a filtered data with this sensor, type in:
termux-sensor -s "RPR0521 Light Sensor" # User your sensor name here
You will see data from the selected sensor.
Step 6: Power BI SetupYou can open up a Power BI Service account with work or school email address. That detailed process is described here, after signing up you can just go here and sign in. We will not be using the Power BI desktop product in this tutorial.
After logging in, select My Workspace from the left panel. From the top right of the screen select Create > Streaming Dataset.
Select API as the data source. Next, put a name for the dataset, and define the json values and their data type that Power BI will look for. Make sure you hit Historic Analysis on. This way the data will be retained in the Power BI service, otherwise the data will be in a temporary cache which quickly expires.
Make not of the Public URL when the streaming dataset is created.
We are going to use Node-RED's "exec" node to run a terminal command and get the sensor data into Node-RED's flow editor, then convert the data into JSON, and finally post the data to Power BI service "Push URL" that we have noted before.
To import any external flow into Node-RED, click on the Hamburger Menu on the Top Right, then click Import > Clipboard, you'll get an import dialogue box, then copy paste the following JSON content into the box and hit Import.
[{"id":"c12e9e18.651ae","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"bec0b05.54ba45","type":"inject","z":"c12e9e18.651ae","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":true,"onceDelay":"","x":170,"y":200,"wires":[["a9c3b25c.f6cc1"]]},{"id":"a9c3b25c.f6cc1","type":"exec","z":"c12e9e18.651ae","command":"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":210,"y":300,"wires":[["dda5983.8467f68"],[],[]]},{"id":"dda5983.8467f68","type":"json","z":"c12e9e18.651ae","name":"","property":"payload","action":"","pretty":false,"x":550,"y":380,"wires":[["3fed63c8.f80ccc"]]},{"id":"3fed63c8.f80ccc","type":"function","z":"c12e9e18.651ae","name":"Get the number","func":"msg.payload.light_sensor = msg.payload[\"RPR0521 Light Sensor\"][\"values\"][0];\nreturn msg","outputs":1,"noerr":0,"x":610,"y":260,"wires":[["a8cd33f3.1c049"]]},{"id":"6d1fabef.8f4bf4","type":"http request","z":"c12e9e18.651ae","name":"","method":"POST","ret":"txt","url":"","tls":"","x":370,"y":60,"wires":[["e7c0faa5.356a38"]]},{"id":"a8cd33f3.1c049","type":"function","z":"c12e9e18.651ae","name":"Format for PowerBI Stream","func":"var timestamp = new Date()\nmsg.payload = JSON.stringify([{\"light_sensor\":msg.payload.light_sensor,\"datetime\":timestamp}]);\nreturn msg;","outputs":1,"noerr":0,"x":500,"y":140,"wires":[["6d1fabef.8f4bf4"]]},{"id":"e7c0faa5.356a38","type":"debug","z":"c12e9e18.651ae","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","x":640,"y":60,"wires":[]}]
After Importing you should get a Node-RED flow diagram like this,
Now, click on exec node and put in termux-sensor -s "Light Sensor Name"-n 1, make sure to put in your light sensor name. Also, because this command outputs a value with a single number, the rest of the flow nodes are designed to handle that single value; if you call for any sensor that return multiple values, the later nodes will break.
On the HTTP request node, set method to "Post" and paste the Push URL that we got from creating the streaming dataset in Power BI.
Click on the Deploy button on the upper right corner of Node-RED flows window.
If you click on the bug icon on the upper right corner you should see the debug window and all the debug messages.
Step 8: Visualize Data in Power BIIn Power BI Service, go to My Workspace > Dataset > click on the view report icon under Action column.
From the Visualization panel on the right click on the line chart icon, then select dateitme and light_sensor column. Now you have a visualization. You can add this viz into a dashboard and edit in many different ways.
You can keep hitting the refresh button on the upper right to see the updates of the data, if you add this chart in a dashboard, you will see the changes live.
Put your hands on the light sensor besides the front camera, block the light, and see the changes happening in the chart.
ConclusionYou can try the same with all the other sensors available in the android phone. Let me know what sort of trouble you face with Node-RED trying this.
The major Reason why I used Power BI is because it provides you with the ability to Visualize and Analyze data using multiple language stack(R, Python) and reduces any DevOps work. In the future I will write more about how to use this collected data to do different type of visualization and run machine learning algorithms on this data.
Sourceshttps://nodered.org/docs/platforms/android
Comments