I want to create my on node module for custom GPIO pin

I want to select pin value from the HTML radio button as a msg.payloadand and pass it in to rpio.open(pin, rpio.OUTPUT, rpio.LOW);

in my HTML file

<script type="text/javascript">
    RED.nodes.registerType('muxbox-pins-out',{
        category: 'MUXBOX',
        color: '#C0C0C0',
        defaults: {
           name: {value: ""},
           pin: { value:"", required:true, validate:RED.validators.number() },
        },
        inputs:1, //1 
        outputs:0,//0
        icon: "font-awesome/fa-cogs",
        label: function() {
            return this.name||"muxbox-pins-out";
        },
        outputLabels: function() { return "GPIO"+this.pin; },
        oneditprepare: function() {
            $('#pinform input').on('change', function() {
                this.pin = $("#pinform input[type='radio']:checked").val();
                $("#node-input-pin").val(this.pin);
            });
        }
    });
</script>

<script type="text/html" data-template-name="muxbox-pins-out">
    <div class="form-row" style="min-width: 540px">
        <label><i class="fa fa-circle"></i>PIN OUT<span data-i18n="rpi-gpio.pin"></span></label>
        <input type="text" id="node-input-pin" style="display:none;">
        <div class="rpi-gpio-pinTable">
            <div class="pinTableBody" id="pinform">
                <div class="pinTableRow">
                    <div class="pinTableCellL pinColorDual"><label for="pinTable-pin-1">DO1 - GPIO6 - 1  <input id="pinTable-pin-1" type="radio" name="pins" value="1"></label></div>
                    <div class="pinTableCellR pinColorPower"><label><input disabled type="radio" name="pins" value=""> 2 - 24V Power</label></div>
                </div>
                <div class="pinTableRow">
                    <div class="pinTableCellL pinColorDual"><label for="pinTable-pin-2">DO2 - GPIO12 - 3 <input id="pinTable-pin-2" type="radio" name="pins" value="2"></label></div>
                    <div class="pinTableCellR pinColorPower"><label><input disabled type="radio" name="pins" value=""> 4 - 24V Power</label></div>
                </div>
                <div class="pinTableRow">
                    <div class="pinTableCellL pinColorDual"><label for="pinTable-pin-5">DO3 - GPIO13 - 5 <input id="pinTable-pin-5" type="radio" name="pins" value="5"></label></div>
                    <div class="pinTableCellR pinColorPower"><label><input disabled type="radio" name="pins" value=""> 6 - 24V Power</label></div>
                </div>
                 <div class="pinTableRow">
                    <div class="pinTableCellL pinColorDual"><label for="pinTable-pin-5">DO4 - GPIO19 - 7 <input id="pinTable-pin-5" type="radio" name="pins" value="5"></label></div>
                    <div class="pinTableCellR pinColorPower"><label><input disabled type="radio" name="pins" value=""> 6 - 24V Power</label></div>
                </div>
                   <div class="pinTableRow">
                    <div class="pinTableCellL pinColorDual"><label for="pinTable-pin-5">DO5 - GPIO5 - 9 <input id="pinTable-pin-5" type="radio" name="pins" value="5"></label></div>
                    <div class="pinTableCellR pinColorPower"><label><input disabled type="radio" name="pins" value=""> 10 - 24V Power</label></div>
                </div>
                   <div class="pinTableRow">
                    <div class="pinTableCellL pinColorDual"><label for="pinTable-pin-5">DO6 - GPIO16 - 11 <input id="pinTable-pin-5" type="radio" name="pins" value="5"></label></div>
                    <div class="pinTableCellR pinColorPower"><label><input disabled type="radio" name="pins" value=""> 12 - 24V Power</label></div>
                </div>
            </div>
        </div>
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
        <input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
    </div>
</script>

in my js file

module.exports = function(RED) {
    "use strict";
    const rpio = require('rpio');

    function muxBoxPins(config) {
        RED.nodes.createNode(this,config);
        var node = this;

        node.on('input', function(msg) {
            const pin = Number(msg.payload)
            rpio.open(pin, rpio.OUTPUT, rpio.LOW);
            
            rpio.write(pin, rpio.HIGH);
            node.send(msg);
        });

    }
    RED.nodes.registerType("muxbox-pins",muxBoxPins);
}

Have you read the readme ? https://www.npmjs.com/package/rpio
I don't see you doing a rpio.init() anywhere

how can i get radio button value as a payload in js file from html

in will working without rpio.init() options when i pass rpio.open(19, rpio.OUTPUT, rpio.LOW) it will work but the problem is that I cant pass value from HTML radio button value

I'm not quite understanding - you want to operate the pin from within the config editor ? or from a message ? - The basic idea of Node-RED is that the messages come form other nodes so arrive on the on "input" callback. That message can come from any other node - like a Dashboard UI button for example. Changing things like pins from within the editor configuration is not really a pattern we want to support.

1 Like

in my case massage come from input field in option html tag .

What node is sending the message ? Where is this option tag ?

i am still shuck in this
i change my code

// var Gpio = require('onoff').Gpio;
var rpio = require('rpio');

module.exports = function(RED) {
  "use strict";
  function opiOutNode(config) {
      RED.nodes.createNode(this,config);
      var node = this;
      node.name = config.name;
      node.log("name: "+node.name);
      node.pins = config.pins;
      node.log("Pin: "+node.pins);
      var outpin;
      
    // // init:
    // if(node.pin !== ''){
    //   outpin = new Gpio(node.pin, 'out');
    //   outpin.unwatch();
    // }
    

    this.on('input', function(msg){

      outpin.write(Number(msg.payload), rpio.HIGH , function(err) {
        if (err) node.log("gpio.output.error");
      });
      // rpio.write(12,);
      node.status({fill:"green",shape:"dot",text:msg.payload.toString()});
       if (RED.settings.verbose) { node.log("out: "+msg.payload); }

       console.log("show payload" ,msg.payload)
    });

    this.on('close', function(){
      outpin.close();
      node.log("close ");
    })
  }
  RED.nodes.registerType("muxbox-pins-out",opiOutNode);
}

As @dceejay said - this isn't how NodeRED is intended to be used, so your probably not going to get much support here

If you want to control pins from a webpage then setting up a dashboard is the accepted way to do it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.