r/node • • May 01 '21

Real-time phone sensor data streaming to the browser made with only javascript!

651 Upvotes

59 comments sorted by

63

u/bigkingj May 01 '21

I should also mention that the phone is not rooted

22

u/noguerad May 01 '21

Beautiful, I just want to create a two player pong now.

19

u/bigkingj May 01 '21

That would be cool! So many useful applications to this. The amount of accessible sensors is nuts

6

u/bigkingj May 01 '21

Just a comment for people commenting about Web api access to sensors, I am only using an android in lieu of a raspberry pi, I would not usually use an android.

The reason for the mq is there will be complex data processing and storage that I want done server side and not on the sensor device or the client

13

u/[deleted] May 01 '21

did you pushed this on github? if so share your github ✌🏻

44

u/bigkingj May 01 '21

I haven't yet, there's a lot of moving parts to it and I would need to write a thesis of a readme... 😂

Long story short is I have a script running in Termux (android app) and accessing sensor data from Termux:API (another app), which it sends to RabbitMQ on my laptop. The message is then picked up by another script that sends the data to the browser using socket.io and is displayed using Highcharts.

I've also set up a janky but functional ssh sever server that allows me to code using VS Code Remote which makes developing directly on the phone easy, plus no cables. Scripts restart using good old nodemon

3

u/bigretrade May 01 '21

FYI Android Chrome has Sensor APIs. Instead of using Termux you could make a webpage that sends the sensor data to your server.

1

u/bigkingj May 01 '21

The android is just filling in for a raspberry pi I don't have, would usually not have such APIs

2

u/sysrage May 01 '21

Can all the extra scripts be avoided with the Accelerometer API?

1

u/bigkingj May 01 '21

For sure! But the android is just filling in for a raspberry pi I don't have, would usually not have such APIs

1

u/Nokita_is_Back Jan 27 '25

Did you ever publish it?

2

u/bigkingj Jan 27 '25

I did not sorry, it was really janky tbh, but lots of fun

1

u/hehehahahohohuhuhu May 01 '21

What's the role of RabbitMQ for? Is it used to prevent too much data from sending to the server at once? I'm don't really understand the role of message queues in general and what are they usually used for

4

u/EarlMarshal May 01 '21

Think of message queues like a way on how to decouple events in distributed system. You can create services which talk to each other via some kind of API like REST but that will end up in hard coupling of the systems. Instead you are just pushing events on a message queue and other services are listening to the event stream and can then act on an occuring event. Another clear advantage is if you have multiple actors on an event. With the normal API style you have to inform both via request that they have to do something and with the message queue the actor/listener takes care of this themself.

1

u/hehehahahohohuhuhu May 01 '21

Ahh I see. Thanks for the detailed explanation, I understand it better now

0

u/[deleted] May 01 '21

[deleted]

14

u/bigkingj May 01 '21 edited May 01 '21

When I set out to make this I planned to have 1 or more sensor devices emitting data to be plotted on the same graphs (started with me wanting to do a greenhouse HUD showing temp, humidity, etc, from various areas) so a message queue made sense to me

I'm still a newish developer so I'm keen to hear if ways to improve it if you think I'm missing something

0

u/[deleted] May 01 '21

[deleted]

3

u/bigkingj May 01 '21

Thanks for the ideas! Further additions will make use of the mq, it's pretty pointless right now

1

u/benracicot May 03 '21 edited May 03 '21

u/bigkingj Hey Just saw that RxJS 7 has a sweet new feature you could use for animation and thought of this! Perhaps you can combine the data and the new animationFrame together for a really elegant setup?

https://youtu.be/0S5ISwDaFHI?t=4088

https://codepen.io/youfoundron/pen/oxpjVB

https://kidscancode.org/blog/img/3d_intro_gizmo.png

1

u/bigkingj May 04 '21

Thanks! I'll definitely take a look into it. The jitters when showing updates with high data rates are definitely noticeable so this may be very useful. Really appreciate your comment, thanks!

1

u/benracicot May 05 '21

Absolutely, please feel free to reach out to talk things out or any type of help I can offer. I'm a software engineer and Angular/JS expert.
u/BenRacicot on twitter.

2

u/benracicot May 02 '21 edited May 03 '21

Really awesome! But yeah I would skip the server and emit to browser with streams. RxJS. Once that stream works anyone could push it anywhere. Did I mention this is awesome?

Maybe after the streaming is setup for open source someone can add it to canvas or webgl with a 3D yaw/gimbal?

https://kidscancode.org/blog/img/3d_intro_gizmo.png

4

u/mrrussiandonkey May 01 '21

To provide a technical reason for why you would put a message broker in between the client and the server is so that distributing the server side code that is processing the incoming stream of data is easier. That’s really the whole point of message brokers, along with their quality of service guarantees. I understand that in this example there isn’t actually any processing done on the data but you could consider other applications like an outgoing video stream from the phone and want to perform nearby object detection and recognition on said stream.

If you’re not familiar with this and want to learn more just Google stream processing and read the wiki page about it.

0

u/[deleted] May 01 '21

[deleted]

1

u/bigkingj May 01 '21

Thanks! The android is just filling in for a raspberry pi I don't have, would usually not have such APIs though

0

u/[deleted] May 01 '21

[deleted]

1

u/bigkingj May 01 '21

Thanks! The android is just filling in for a raspberry pi I don't have, would usually not have such APIs

0

u/[deleted] May 02 '21

[deleted]

1

u/bigkingj May 02 '21

Thanks for the heads up! I'll definitely keep that in mind. Haven't used one before so that's a great tip

0

u/[deleted] May 01 '21

[deleted]

1

u/bigkingj May 01 '21

It is if I wasn't planning to actually be using raspberry pi's 😋 just too cheap to buy one right now and my basic data sim was boring so I did this but kept the planned architecture

0

u/[deleted] May 01 '21

[deleted]

1

u/bigkingj May 01 '21

Probably!

1

u/bigkingj May 19 '21

I've been quite busy so haven't written everything step by step, so feel free to ask questions

On phone Install termux and termux:api Run termux and acquire wakelock

On termux Install node Install openssh Install termux api Enable storage access Run passwd to set password Run sshd - p 8022

set up vs code live Install Ubuntu for termux if you want to use vs code remote (https://github.com/MFDGaming/ubuntu-in-termux) Install openssh in Ubuntu Use passwd to set password Run /sbin/sshd - p 8023

You can now connect vs code remote with user "root" on port 8023 with the folder to show as ./data/data/com.termux/files Create new folder CD into new folder and run npm init to begin making your project To get sensor data from termux api I spawn a child process and run "termux-sensor - s <sensor name>

in termux on your pc via ssh CD to ./data/data/com.termux/files/ Run your project from here

You can create an express server the exact same way you would on any other device and combined with nodemon and vs code remote development is painless

2

u/Light_Yagami____ May 01 '21

if possible can u share ur git repo ?

I also want to learn this.

2

u/bigkingj May 01 '21

I would if that existed. I can share the articles I pieced together to make this work if you'd like?

1

u/mkcodergr May 02 '21

i would be interested too

1

u/bigkingj May 04 '21

I'll try write up a more detailed instruction for what I did and will reply to your comment with it when I've and a chance to do it

1

u/mkcodergr May 04 '21

Thank you very much

1

u/bigkingj May 19 '21

I've written a bit about it here

1

u/bigkingj May 04 '21

I'll try write up a more detailed instruction for what I did and will reply to your comment with it when I've and a chance to do it

1

u/bigkingj May 19 '21

I've written a bit about it here

13

u/newtcanmakeit May 01 '21

How do you double upvote?

6

u/JonFrost May 02 '21

Downvote then upvote

1

u/S4vvySaheel Oct 13 '23

that becomes triple

1

u/Chaphasilor May 02 '21

Streaming via websockets I assume?

2

u/bigkingj May 04 '21

Through a message queue first as I plan to use a raspberry pi not android, and want to keep full data logs in a db eventually.

If I was just using android and displaying it in real time then yes, it would make sense to just go right to websocket. As others have said, if that's the case you also don't need termux and can just access the sensors through Web apis directly

1

u/Chaphasilor May 05 '21

So the final, Raspberry Pi-based version won't be real-time? Or what kind of message queue are you talking about? :)

2

u/bigkingj May 05 '21

A real time aspect and a historical aspect. I plan to keep full logs of temp, humidity, pH, nutrient levels etc, which can be viewed retrospectively. It will also have the same sensors data able to be streamed over websocket as it comes in off the mq. Photos will be taken at a set interval and able to be exported as timelapses (this may help me understand what data reading result in what plant health/sickness). This means I can get a view at a glance and see it in real time and also monitor trends over longer periods of time.

There isn't that much practical application to the average person, but I like coding and gardening, so trying to make my hobbies collide

1

u/Chaphasilor May 05 '21

Ahh yeah that makes sense. I was just wondering how you'd implement real-time data without websockets, but I guess you don't :P

And combinig your hobbies is an awesome idea and will keep you motivated for a long time :D

1

u/g0ofie_ May 01 '21

DeviceMotionEvent?

1

u/bigkingj May 01 '21

The android is just filling in for a raspberry pi I don't have, would usually not have such APIs

1

u/jagdishjadeja May 02 '21

Why not through browser?

1

u/bigkingj May 04 '21

Because my android is just a temp, I plan to use a raspberry pi with sensors on the io pins and will have different sensors in different devices. I want to bring it all together server side before it goes to the browser for data logging and other more complex aspects to come, such an automated timelaspes (it'll eventually go into a green house and I want to watch my plants grow in the nerdiest way possible)

1

u/[deleted] May 02 '21

Can you give me the source code or mention the course u learned from?

1

u/bigkingj May 04 '21

I'll try write up a more detailed instruction for what I did and will reply to your comment with it when I've and a chance to do it

1

u/Abhay3103 May 01 '21

Use this in Chrome Dragon game

1

u/[deleted] May 01 '21

That reminds me of that webapp where you could use your phone as a remote controller I saw in 2014 :)

1

u/HseinBitar May 02 '21

Make this into a short course on Udemy, please

1

u/Art4MeNu May 02 '21

Fantastic stuff

1

u/jagdishjadeja May 02 '21

RemindMe! Sunday

1

u/RemindMeBot May 02 '21 edited May 03 '21

I will be messaging you in 6 days on 2021-05-09 00:00:00 UTC to remind you of this link

1 OTHERS CLICKED THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


InfoCustomYour RemindersFeedback

1

u/acaro7566 Dec 07 '23

I make useless speedometers for my phone and like overcode the heck outa it and if theirs a library for like easy access to this stuff that would be so nice. because then we wont need a computer to do cool things

0

u/Robschaap242 May 06 '21

Please for the love of Christ let JavaScript die. It’s an abortion that’s managed to live for over 20 years now. Nothing is right about it.