PJON icon indicating copy to clipboard operation
PJON copied to clipboard

Node.js bindings?

Open vshymanskyy opened this issue 7 years ago • 13 comments

I'm working a lot with different IoT systems, and for me it looks like PJON would really benefit from Node.js bindings. Are there any projects that try to achieve this, or is it planned?

vshymanskyy avatar Nov 13 '17 20:11 vshymanskyy

Ciao @vshymanskyy, thank you for your support. I agree with you, absolutely. For now there are no projects using node.js, as far as I know.

I would say that it is planned, but, being horrified by the npm dependency system, and being a vanilla extremist, I have used it rarely, was waiting for node.js more adavanced users feedback, so help obviously would be highly appreciated if you have any will and experience with that :)

gioblu avatar Nov 14 '17 03:11 gioblu

@vshymanskyy I'm doing "sort of" node.js development - it's actually IBM's node-red based but the concept applies to pure node.js as well.

This is a kind of hack but it works fine and gives some flexibility regarding the application layer. I'm using:

  1. Redis server as a pub/sub communication broker
  2. proxy program utulizing PJON-python to interface with Redis and PJON-enabled micro over serial (e.g. Arduino connected over USB). code example is here: https://gist.github.com/Girgitt/e490ff6d0e2fbcb4a9cf01d9bfb34373

async receive from PJON bus:

  • Proxy gets PJON message and passes it to Redis over a pub channel 'pjon-python-redis'
  • node.js redis client picks up the message which is a dictionary {'sender_id','receiver_id','payload_length','payload'..}

async send to PJON bus

  • Node.js client sends JSON message to pub channel 'pjon-python-redis' wiith sender_id set to the same ID as used for the pjon_redis_mock client in the proxy conde.
  • When to PJON-python redis client gets a message sent with its own sender id it forwards it to PJON-python serial client which passes it to microcontroller over USB/serial.

You can run multiple proxy programs connected to the same Redis server. Depending on the sender_id from the message sent to Redis a proper proxy client will pass the message to PJON-python serial client. Lets suppose you have two micros you want to communicate with:

Arduino(id=101) ---<USB>--- proxy[PJON-python serial(id=100) <-> PJON-python redis(id=100)] --<Redis>-- Node,js

Arduino(id=201) ---<USB>--- proxy[PJON-python serial(id=200) <-> PJON-python redis(id=200)] --<Redis>-- Node,js

to send payload to node 101 the JSON dict passed to redis should have sender_id set to 100 and receiver id set to 101

to send payload to node 201 the JSON dict passed to redis should have sender_id set to 200 and receiver id set to 201

I'll make a better description how to use PJON-python for such integration when time allows.

EDIT: I added more details and JSON example here: https://github.com/Girgitt/PJON-python/wiki/Connecting-PJON-bus-to-Node-red

Girgitt avatar Nov 14 '17 09:11 Girgitt

Ciao @Girgitt, thank you for your feedback, I still have to try your setup!

It seems that bindings would simplify the integration process, potentially enabling PJON to run over serial or TCP/UDP directly in node!

gioblu avatar Nov 14 '17 15:11 gioblu

@gioblu sure they would simplify the stack but add additional component be maintained (which is a bit of an issue given the quick pace of PJON development). Using Redis for communication opens many programming languages and environments and seems a good solution for now as only PJON-piper needs to be kept compatible with PJON (and preferably should be extended with TCP and UDP strategies).

Girgitt avatar Nov 14 '17 16:11 Girgitt

Ciao @Girgitt absolutely, I will run some tests of the redis server, thank you for your support! I agree with you that the setup you have designed could cover a lot of use cases considering the flexibility of the redis server.

I agree also with @vshymanskyy considering handy to have a direct interface from node to the c++ implementation. I will study bindings.

gioblu avatar Nov 14 '17 17:11 gioblu

@gioblu this will reduce package maintenance efforts a lot: https://medium.com/the-node-js-collection/n-api-next-generation-node-js-apis-for-native-modules-169af5235b06

vshymanskyy avatar Nov 14 '17 17:11 vshymanskyy

@vshymanskyy that looks very promising. If such bindings could be automatically compiled from PJON sources - I'm all for it.

Girgitt avatar Nov 14 '17 17:11 Girgitt

@Girgitt @gioblu i don't think it can auto-generate something, but it should be rather simple to wrap your library with N-API. the hard part may be to provide proper Node objects (that act the Node way). But usually node community helps with that...

vshymanskyy avatar Nov 14 '17 20:11 vshymanskyy

Ciao @vshymanskyy @Girgitt probably if there is no change in method names or parameters a version update should go through transparently? This seems really cool, thank you @vshymanskyy

gioblu avatar Nov 14 '17 22:11 gioblu

@gioblu exactly. That's what they promise, at least ;) I'm sorry I can't help you with coding currently - too much stuff is going on ;(

vshymanskyy avatar Nov 14 '17 23:11 vshymanskyy

Ciao @vshymanskyy dont be preoccupied :), compliments for your work and thank you for your feedback.

gioblu avatar Nov 15 '17 13:11 gioblu

Just found this thread and I've been searching for a clean solution for Arduino <-> Node.js (running on a Raspberry Pi) bi-directional communication. Has anyone taken the challenge yet? The idea is so good I might try to hack together some prototype if I only just find the time. I'm familiar with the NPM ecosystem, have been planning on trying N-API and PJON (originally for ESP8266 <-> AVR communication) for some time now. I've only got half a year of C/C++ experience through hobby projects though so I'm hoping no one is holding their breath waiting for this to happen. :roll_eyes:

I'll sure to post here in case I manage to pull something off.

ristomatti avatar Jan 16 '18 16:01 ristomatti

Ciao @ristomatti I still have no physical time to work on this issue, any support would be highly appreciated :) Thank you for your support to the project.

gioblu avatar Jan 16 '18 21:01 gioblu