Skip to content

Instantly share code, notes, and snippets.

@juzam

juzam/README.md Secret

Last active August 29, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juzam/3356b174b24c99ca9e5d to your computer and use it in GitHub Desktop.
Save juzam/3356b174b24c99ca9e5d to your computer and use it in GitHub Desktop.
Pinoccio Command Flow

This flow sends commands to the remote pinoccio via HQ API calls. Node-RED version >= 0.7.1 is required, due to the use of the new templating feature of http request nodes.

This flow uses an API token which is configured in the "Pinoccio Config" inject Node.

It's useful if you want to send scoutscripts commands (via HQ) using anything that is not a "direct" REST call.

Configuration is done within Pinoccio Config node. There you have to define your HQ API Token in the Payload:

{ "token": "yourtokenhere" }

Use Topic Property to define the root of the MQTT topics used i.e. pinoccio/v1/

Command Payload Example:

{"troop": "1", "scout": "2", "topic": "result", "command": "pin.write(\"d8\", HIGH)"}

Please note that at the moment any double quote (") character in command string must be escaped.

[{"id":"1f8dc6c2.cba389","type":"mqtt-broker","broker":"localhost","port":"1883","clientid":"nodered-1"},{"id":"1788c453.e8773c","type":"mqtt out","name":"","topic":"","broker":"1f8dc6c2.cba389","x":1059.1071395874023,"y":159.6071491241455,"z":"e4aa5c74.55cc4","wires":[]},{"id":"b48f5d84.4b70a","type":"inject","name":"\"print power.percent\" Command Example","topic":"","payload":"{\"troop\": \"1\", \"scout\": \"1\", \"topic\": \"result\", \"command\": \"print power.percent\"}","payloadType":"string","repeat":"","crontab":"","once":false,"x":271.49998474121094,"y":239.6785774230957,"z":"e4aa5c74.55cc4","wires":[["7b0ffb83.84f004"]]},{"id":"19be09e3.e641f6","type":"http request","name":"REST POST","method":"POST","url":"https://api.pinocc.io/v1/{{troop}}/{{scout}}/command?token={{token}}","x":668.8571395874023,"y":159.8571491241455,"z":"e4aa5c74.55cc4","wires":[["a556f2dc.5aa91"]]},{"id":"a556f2dc.5aa91","type":"function","name":"Prepare Output","func":"data = JSON.parse(msg.payload);\n\nreply = { payload: data.data.reply.trim() , topic: msg.topic, retain: 1, troop: msg.troop, scout: msg.scout};\ntstamp = { payload: data.data._t, topic: msg.topic + \"/tstamp\", retain: 1, troop: msg.troop, scout: msg.scout};\n\n\nreturn [ reply, tstamp ];","outputs":"2","x":851.8571395874023,"y":159.8571491241455,"z":"e4aa5c74.55cc4","wires":[["1788c453.e8773c"],["1788c453.e8773c"]]},{"id":"86c361cf.793ca","type":"inject","name":"Pinoccio Config","topic":"pinoccio/v1/","payload":"{ \"token\": \"yourtokenhere\" }","payloadType":"string","repeat":"","crontab":"","once":true,"x":191.14285278320312,"y":358.8571548461914,"z":"e4aa5c74.55cc4","wires":[["52fc16e6.ad03e8"]]},{"id":"52fc16e6.ad03e8","type":"function","name":"Set Global Variables","func":"// set global variables\ndata = JSON.parse(msg.payload);\ncontext.global.pinoccio_token = data.token;\ncontext.global.pinoccio_basetopic = msg.topic;\n\nreturn msg;","outputs":1,"x":458.6428527832031,"y":358.857177734375,"z":"e4aa5c74.55cc4","wires":[[]]},{"id":"7b0ffb83.84f004","type":"function","name":"Prepare Api Call","func":"data = JSON.parse(msg.payload);\n\nmsg.token = context.global.pinoccio_token;\nmsg.topic = context.global.pinoccio_basetopic+\"/\"+data.troop+\"/\"+data.scout+\"/\"+data.topic;\nmsg.troop = data.troop;\nmsg.scout = data.scout;\n\nreturn msg;","outputs":1,"x":465.60713958740234,"y":159.8571491241455,"z":"e4aa5c74.55cc4","wires":[["19be09e3.e641f6"]]},{"id":"e0d2e5a5.1f2d18","type":"comment","name":"Api Token and Base Topic Config","info":"Token is obtained by calling:\n\ncurl -X POST -v --data '{\"email\":\"youremail\",\"password\":\"boats\"}' https://api.pinocc.io/v1/login\n\nBase topic is configured by setting \"Pinoccio Config\" topic property and is then stored\nand use as a \"root\" topic for output","x":281.1428527832031,"y":312.8571472167969,"z":"e4aa5c74.55cc4","wires":[]},{"id":"8fe18e8d.701e7","type":"mqtt in","name":"","topic":"pinoccio/command","broker":"1f8dc6c2.cba389","x":177.85713958740234,"y":159.8571491241455,"z":"e4aa5c74.55cc4","wires":[["7b0ffb83.84f004"]]},{"id":"29f04960.d60fb6","type":"comment","name":"Input Stage","info":"","x":151.85713958740234,"y":82.85714912414551,"z":"e4aa5c74.55cc4","wires":[]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment