Connecting PJON bus to Node red - Girgitt/PJON-python GitHub Wiki

PJON-python can be used to receive and send messages to PJON-enabled devices from any program capable of talking to Redis pub/sub channel.

This is a kind of hack but it works fine and gives some flexibility regarding the application layer. This example explains how to connect PJON serial devices to Node-Red.

Stack:

  1. Redis server as a pub/sub communication broker
  2. proxy program utilizing 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
  3. Node-red with node-contrib-redis node installed

Receiving from PJON bus:

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

Sending to PJON bus:

  • Node-red redis 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 code (the one from stack / item 2 above).
  • 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) ---- proxy[PJON-python serial(id=100) <-> PJON-python redis(id=100)] --<pub/sub>-- Node-red Redis client

Arduino(id=201) ---- proxy[PJON-python serial(id=200) <-> PJON-python redis(id=200)] --<pub/sub>-- Node-red Redis client

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

receive example (import it to Red-node through clipboard import function and check output in debug tab):

[{"id":"39377abc.60a896","type":"redis-in","z":"f10ed26b.b507e","server":"4cac76d7.110a38","command":"subscribe","name":"","topic":"pjon-python-redis","timeout":0,"x":217,"y":645,"wires":[["1b962c43.07d604"]]},{"id":"1b962c43.07d604","type":"debug","z":"f10ed26b.b507e","name":"","active":false,"console":"false","complete":"false","x":740,"y":637,"wires":[]},{"id":"4cac76d7.110a38","type":"redis-config","z":"","host":"localhost","port":"6379","dbase":"0","pass":""}]

JSON message example:

{ "sender_bus_id": [ 0, 0, 0, 0 ], "receiver_id": 100, "payload_length": 11, "sender_id": 101, "receiver_bus_id": [ 0, 0, 0, 0 ], "originator_uuid": "cfe01439-6ded-40bf-b246-694ba56a176e", "payload": "motion_in:0" }

⚠️ **GitHub.com Fallback** ⚠️