BBB Digital Read with Node-RED

Home > Guides

This guide provides instructions for configuring a BBB to read a GPIO through Node-RED.

Steps

1. Flash the BBB EMMC with the latest image.

2. Construct a simple circuit.

3. Start the BBB.

4. Connect to the BBB.

5. Configure Node-RED to make the Bonescript module available.

4. Start Node-RED on the BBB (http://192.168.7.2:1880).

6. Import the flow into Node-RED (text at the bottom of the page).

If you’d like to publish MQTT messages to a broker, configure the mqtt out node appropriately.

Node-RED should appear as follows:

7. Operate the circuit.

When the switch is opened and closed messages will be published to the Node-RED debug window, and the state of the switch will be displayed in the palette under the Determine State node.

8. Enjoy.

Node-RED Flow

[
   {
      "id":"33ee3bd0.4d1de4",
      "type":"tab",
      "label":"Flow 1",
      "disabled":false,
      "info":""
   },
   {
      "id":"bd649397.deb7f",
      "type":"gpio in",
      "z":"33ee3bd0.4d1de4",
      "name":"P9_14 GPIO_50",
      "state":"INPUT",
      "pin":"P9_14",
      "board":"b22bcb60.11a438",
      "x":160,
      "y":240,
      "wires":[
         [
            "22adbf69.6c292"
         ]
      ]
   },
   {
      "id":"68d09258.ded4bc",
      "type":"debug",
      "z":"33ee3bd0.4d1de4",
      "name":"",
      "active":true,
      "tosidebar":true,
      "console":false,
      "tostatus":false,
      "complete":"false",
      "x":490,
      "y":340,
      "wires":[
      ]
   },
   {
      "id":"22adbf69.6c292",
      "type":"function",
      "z":"33ee3bd0.4d1de4",
      "name":"Determine State",
      "func":"// Determine last value\nvar lastValue = context.get('lastValue')||0;\n\n// If new value is same as last value, do nothing.\nif(msg.payload == lastValue) {\n    return null;\n}\nelse {\n    context.set('lastValue', msg.payload)\n    \n    if(msg.payload === 1 ){\n        node.status({fill:\"green\",shape:\"dot\",text:\"Switch closed\"});\n    }\n    else {\n        node.status({fill:\"red\",shape:\"dot\",text:\"Switch open\"});\n    }\n    return msg;\n}",
      "outputs":1,
      "noerr":0,
      "initialize":"",
      "finalize":"",
      "x":400,
      "y":240,
      "wires":[
         [
            "9ec6af7f.bb055"
         ]
      ]
   },
   {
      "id":"9ec6af7f.bb055",
      "type":"function",
      "z":"33ee3bd0.4d1de4",
      "name":"Generate Message",
      "func":"var now = new Date();\nvar assetId = global.get(\"assetId\")\nvar dataItemId = \"EquipmentChangeState\"\nvar topic = \"Asset/\" + assetId + \"/\" + dataItemId\n\nif (msg.payload === 1){\n    node.status({fill:\"green\", shape:\"dot\", text:\"Last true: \" + now.toISOString()});\n    payload = {}\n    payload[\"assetId\"] = assetId;\n    payload[\"dataItemId\"] = dataItemId;\n    payload[\"dateTime\"] = now.toISOString();\n    payload[\"value\"] = \"READY-PROCESSING-EXECUTING\"\n    return {\"topic\": topic, \"payload\": payload}\n} else {\n    node.status({fill:\"green\", shape:\"dot\", text:\"Last true: \" + now.toISOString()});\n    payload = {}\n    payload[\"assetId\"] = assetId;\n    payload[\"dataItemId\"] = dataItemId;\n    payload[\"dateTime\"] = now.toISOString();\n    payload[\"value\"] = \"OFF\"\n    return {\"topic\": topic, \"payload\": payload}\n}",
      "outputs":1,
      "noerr":0,
      "initialize":"",
      "finalize":"",
      "x":240,
      "y":380,
      "wires":[
         [
            "68d09258.ded4bc",
            "f6e1fab6.7f1cf8"
         ]
      ]
   },
   {
      "id":"2cdae9bf.714a56",
      "type":"inject",
      "z":"33ee3bd0.4d1de4",
      "name":"Trigger on Start",
      "props":[
         {
            "p":"payload"
         },
         {
            "p":"topic",
            "vt":"str"
         }
      ],
      "repeat":"",
      "crontab":"",
      "once":false,
      "onceDelay":0.1,
      "topic":"",
      "payload":"true",
      "payloadType":"bool",
      "x":170,
      "y":100,
      "wires":[
         [
            "a41bd290.62927"
         ]
      ]
   },
   {
      "id":"a41bd290.62927",
      "type":"function",
      "z":"33ee3bd0.4d1de4",
      "name":"Set Global ",
      "func":"global.set(\"assetId\", \"ANT1000\")\n\nreturn msg;",
      "outputs":1,
      "noerr":0,
      "initialize":"",
      "finalize":"",
      "x":440,
      "y":100,
      "wires":[
         [
            "840531c8.a1b5a"
         ]
      ]
   },
   {
      "id":"540d4cfa.09ebe4",
      "type":"comment",
      "z":"33ee3bd0.4d1de4",
      "name":"Sets global variables on start.",
      "info":"",
      "x":220,
      "y":140,
      "wires":[
      ]
   },
   {
      "id":"83496baf.e86db8",
      "type":"comment",
      "z":"33ee3bd0.4d1de4",
      "name":"Publish messages based upon GPIO changes.",
      "info":"",
      "x":280,
      "y":440,
      "wires":[
         
      ]
   },
   {
      "id":"f6e1fab6.7f1cf8",
      "type":"mqtt out",
      "z":"33ee3bd0.4d1de4",
      "name":"mb.iotfm.org",
      "topic":"",
      "qos":"",
      "retain":"",
      "broker":"7adab055.da5c2",
      "x":490,
      "y":380,
      "wires":[
      ]
   },
   {
      "id":"840531c8.a1b5a",
      "type":"debug",
      "z":"33ee3bd0.4d1de4",
      "name":"",
      "active":true,
      "tosidebar":true,
      "console":false,
      "tostatus":false,
      "complete":"false",
      "statusVal":"",
      "statusType":"auto",
      "x":690,
      "y":100,
      "wires":[
      ]
   },
   {
      "id":"b22bcb60.11a438",
      "type":"ioplugin",
      "z":"",
      "name":"Beaglebone",
      "username":"",
      "password":"",
      "boardType":"beaglebone-io",
      "serialportName":"",
      "connectionType":"local",
      "mqttServer":"",
      "pubTopic":"",
      "subTopic":"",
      "tcpHost":"",
      "tcpPort":"",
      "sparkId":"",
      "sparkToken":"",
      "beanId":"",
      "impId":"",
      "uuid":"",
      "token":"",
      "sendUuid":"",
      "samplingInterval":"500"
   },
   {
      "id":"7adab055.da5c2",
      "type":"mqtt-broker",
      "z":"",
      "name":"mb.iotfm.org",
      "broker":"mb.iotfm.org",
      "port":"1883",
      "clientid":"",
      "usetls":false,
      "compatmode":false,
      "keepalive":"60",
      "cleansession":true,
      "birthTopic":"",
      "birthQos":"0",
      "birthPayload":"",
      "closeTopic":"",
      "closeQos":"0",
      "closePayload":"",
      "willTopic":"",
      "willQos":"0",
      "willPayload":""
   }
]