1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Add PiGPIOd node

alternative to Pi GPIO nodes with remote access
This commit is contained in:
Dave Conway-Jones 2017-04-14 21:37:44 +01:00
parent ec96d3622b
commit fc249b6792
No known key found for this signature in database
GPG Key ID: 81B04231572A9A2D
6 changed files with 789 additions and 0 deletions

177
hardware/pigpiod/LICENSE Normal file
View File

@ -0,0 +1,177 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS

View File

@ -0,0 +1,69 @@
node-red-node-pi-gpiod
======================
An alternative pair of <a href="http://nodered.org" target="_new">Node-RED</a> nodes to interact with Pi GPIO using
the <a href="http://abyz.co.uk/rpi/pigpio/pigpiod.html" target="_new">PiGPIOd</a> daemon that is now part of Raspbian.
The advantage is that it also talk to GPIO on a Pi that is remote as long as it is running the daemon, and also sharing pins works more cleanly as contention is handled by the multiple connections.
The disadvantage is that you must setup and run the PiGPIO daemon first.
## Requirements
PiGPIOd must be running on the pi. The easiest way to ensure this is to add the following line to your Pi `/etc/rc.local` file.
/usr/bin/pigpiod
**Note**: By default this will expose the daemon on TCP port 8888, which is obviously a **security vulnerability**. If you don't want or need remote access then you can start it in local mode only, (`-l` option), or restrict permissions to certain ip addresses, (`-n {ipaddr}` option). For example:
/usr/bin/pigpiod -l
/usr/bin/pigpiod -n 192.168.1.10
See the <a href="http://abyz.co.uk/rpi/pigpio/pigpiod.html" target="new">instructions</a> for more details.
## Install
Run the following command in your Node-RED user directory - typically `~/.node-red`
npm install node-red-node-pi-gpiod
## Usage
**Note:** the pin numbers refer the physical pin numbers on connector P1 as they are easier to locate.</p>
### Input node
Generates a `msg.payload` with either a 0 or 1 depending on the state of the input pin.
##### Outputs
- `msg.payload` - *number* - the level of the pin (0 or 1)
- `msg.topic` - *string* - pi/{the pin number}
##### Details
You may also enable the input pullup resistor &uarr; or the pulldown resistor &darr;.
### Output node
Can be used in Digital, PWM or Servo modes.
###### Input
- `msg.payload` - *number | string*
- Digital - 0, 1 - set pin low or high
- PWM - 0 to 100 - level from 0 to 100%
- Servo - 0 to 100, 50 is centred.
*Hint*: The `range` node can be used to scale inputs to the correct range.
##### Details
Digital mode expects a `msg.payload` with either a 0 or 1 (or true or false),
and will set the selected physical pin high or low depending on the value passed in.
The initial value of the pin at deploy time can also be set to 0 or 1.
When using PWM and Servo modes, the input value should be a number 0 - 100, and can be floating point.
In servo mode you can also preset the minimum and maximum pulse times as required by your servo in order to reach its full range. Minimum of 5mS, maximum of 25 mS - defaults to 10 and 20 mS respectively.

View File

@ -0,0 +1,57 @@
{
"pi-gpiod": {
"label": {
"gpiopin": "GPIO",
"selectpin": "select pin",
"host": "Host",
"resistor": "Resistor?",
"readinitial": "Read initial state of pin on deploy/restart?",
"type": "Type",
"initpin": "Initialise pin state?",
"debounce": "Debounce",
"limits": "Limits",
"min": "min",
"max": "max"
},
"place": {
"host": "local or remote ip",
"port": "port"
},
"resistor": {
"none": "none",
"pullup": "pullup",
"pulldown": "pulldown"
},
"digout": "Digital output",
"pwmout": "PWM output",
"servo": "Servo output",
"initpin0": "initial level of pin - low (0)",
"initpin1": "initial level of pin - high (1)",
"pinname": "Pin",
"tip": {
"pin": "<b>Pins in Use</b>: ",
"in": "<b>Tip</b>: Only Digital Input is supported - input must be 0 or 1.",
"dig": "<b>Tip</b>: For digital output - input must be 0 or 1.",
"pwm": "<b>Tip</b>: For PWM output - input must be between 0 to 100.",
"ser": "<b>Tip</b>: For Servo output - input must be between 0 to 100. 50 is centre.<br/>Min must be 5mS or more, Max must be 25mS or less."
},
"types": {
"digout": "digital output",
"input": "input",
"pullup": "input with pull up",
"pulldown": "input with pull down",
"pwmout": "PWM output",
"servo": "Servo output"
},
"status": {
"stopped": "stopped",
"closed": "closed",
"not-running": "not running"
},
"errors": {
"invalidpin": "Invalid GPIO pin",
"invalidinput": "Invalid input",
"error": "error: __error__"
}
}
}

View File

@ -0,0 +1,29 @@
{
"name": "node-red-node-pi-gpiod",
"version": "0.0.1",
"description": "A node-red node for PiGPIOd",
"dependencies" : {
"js-pigpio": "^1.0.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/node-red/node-red-nodes/tree/master/hardware/pigpiod"
},
"keywords": [
"node-red", "Pi", "GPIO", "PiGPIOd"
],
"author": {
"name": "Dave Conway-Jones",
"email": "ceejay@vnet.ibm.com",
"url": "http://nodered.org"
},
"license": "Apache-2.0",
"node-red" : {
"nodes": {
"pi-gpiod": "pi-gpiod.js"
}
}
}

View File

@ -0,0 +1,293 @@
<script type="text/x-red" data-template-name="pi-gpiod in">
<div class="form-row">
<label for="node-input-pin"><i class="fa fa-circle"></i> <span data-i18n="pi-gpiod.label.gpiopin"></span></label>
<select type="text" id="node-input-pin" style="width:250px;">
<option value='' disabled selected style='display:none;'><span data-i18n="pi-gpiod.label.selectpin"></span></option>
<option value="2">Pin 3 - GPIO2 - SDA1*</option>
<option value="3">Pin 5 - GPIO3 - SCL1*</option>
<option value="4">Pin 7 - GPIO4</option>
<option value="14">Pin 8 - GPIO14 - TxD*</option>
<option value="15">Pin 10 - GPIO15 - RxD*</option>
<option value="17">Pin 11 - GPIO17</option>
<option value="18">Pin 12 - GPIO18</option>
<option value="27">Pin 13 - GPIO27</option>
<option value="22">Pin 15 - GPIO22</option>
<option value="23">Pin 16 - GPIO23</option>
<option value="24">Pin 18 - GPIO24</option>
<option value="10">Pin 19 - GPIO10 - MOSI*</option>
<option value="9">Pin 21 - GPIO9 - MISO*</option>
<option value="25">Pin 22 - GPIO25</option>
<option value="11">Pin 23 - GPIO11 - SCLK*</option>
<option value="8">Pin 24 - GPIO8 - CE0*</option>
<option value="7">Pin 26 - GPIO7 - CE1*</option>
<option value="5">Pin 29 - GPIO05</option>
<option value="6">Pin 31 - GPIO06</option>
<option value="12">Pin 32 - GPIO12</option>
<option value="13">Pin 33 - GPIO13</option>
<option value="19">Pin 35 - GPIO19</option>
<option value="16">Pin 36 - GPIO16</option>
<option value="26">Pin 37 - GPIO26</option>
<option value="20">Pin 38 - GPIO20</option>
<option value="21">Pin 40 - GPIO21</option>
</select>
&nbsp;<span id="pitype"></span>
</div>
<div class="form-row">
<label for="node-input-intype"><i class="fa fa-level-up"></i> <span data-i18n="pi-gpiod.label.resistor"></span></label>
<select type="text" id="node-input-intype" style="width:100px;">
<option value="PUD_OFF" data-i18n="pi-gpiod.resistor.none"></option>
<option value="PUD_UP" data-i18n="pi-gpiod.resistor.pullup"></option>
<option value="PUD_DOWN" data-i18n="pi-gpiod.resistor.pulldown"></option>
</select>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span data-i18n="pi-gpiod.label.debounce"></span>
<input type="text" id="node-input-debounce" style="width:47px; text-align:right"/>&nbsp;mS
</div>
<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-read" style="display:inline-block; width:auto; vertical-align:top;">
<label for="node-input-read" style="width:70%;"><span data-i18n="pi-gpiod.label.readinitial"></span></label>
</div>
<div class="form-row">
<label for="node-input-host"><i class="fa fa-globe"></i> <span data-i18n="pi-gpiod.label.host"></label>
<input type="text" id="node-input-host" data-i18n="[placeholder]pi-gpiod.place.host" style="width:250px;">
<input type="number" id="node-input-port" data-i18n="[placeholder]pi-gpiod.place.port" style="width:65px;">
</div>
<br/>
<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>
<div class="form-tips" id="pin-tip">Pins marked with <b>*</b> are dual use. Make sure they are not enabled for
their other use before using as GPIO.</div>
<div class="form-tips"><span data-i18n="[html]pi-gpiod.tip.in"></span></div>
</script>
<script type="text/x-red" data-help-name="pi-gpiod in">
<p>Raspberry Pi input node. Generates a <code>msg.payload</code> with either a
0 or 1 depending on the state of the input pin.
Requires the <a href="http://abyz.co.uk/rpi/pigpio/pigpiod.html" target="_new">pi-gpiod</a>
daemon to be running on the host computer in order to work.</p>
<p><b>Outputs</b>
<ul>
<li><code>msg.payload</code> - <i>number</i> - the level of the pin (0 or 1).</li>
<li><code>msg.topic</code> - <i>string</i> - pi/{the pin number}</li>
</ul>
<p><b>Details</b></p>
<p>You may also enable the input pullup resistor &uarr; or the pulldown resistor &darr;.</p>
<p><b>Note:</b> the pin numbers refer the physical pin numbers on connector P1 as they are easier to locate.</p>
</script>
<script type="text/javascript">
var bcm2pin = {
"2":"3", "3":"5", "4":"7", "14":"8", "15":"10", "17":"11", "18":"12", "27":"13", "22":"15",
"23":"16", "24":"18", "10":"19", "9":"21", "25":"22", "11":"23", "8":"24", "7":"26",
"5":"29", "6":"31", "12":"32", "13":"33", "19":"35", "16":"36", "26":"37", "20":"38", "21":"40"
};
RED.nodes.registerType('pi-gpiod in',{
category: 'Raspberry Pi',
color:"#c6dbef",
defaults: {
name: { value:"" },
host: { value:"localhost" },
port: { value:8888 },
pin: { value:"", required:true, validate:RED.validators.number() },
intype: { value:"PUD_OFF" },
debounce: { value:"25" },
read: { value:false }
},
inputs:0,
outputs:1,
icon: "rpi.png",
label: function() {
var suf = "";
if (this.intype === "PUD_UP") { suf = "↑ "}
if (this.intype === "PUD_DOWN") { suf = "↓ "}
return this.name || "GPIO: "+suf+bcm2pin[this.pin] ;
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
palettelabel: "pi gpiod",
oneditprepare: function() {
var pinnow = this.pin;
var pintip = this._("pi-gpiod.tip.pin");
var pinname = this._("pi-gpiod.pinname");
}
});
</script>
<script type="text/x-red" data-template-name="pi-gpiod out">
<div class="form-row">
<label for="node-input-pin"><i class="fa fa-circle"></i> <span data-i18n="pi-gpiod.label.gpiopin"></span></label>
<select type="text" id="node-input-pin" style="width: 250px;">
<option value='' disabled selected style='display:none;'><span data-i18n="pi-gpiod.label.selectpin"></span></option>
<option value="2">Pin 3 - GPIO2 - SDA1*</option>
<option value="3">Pin 5 - GPIO3 - SCL1*</option>
<option value="4">Pin 7 - GPIO4</option>
<option value="14">Pin 8 - GPIO14 - TxD*</option>
<option value="15">Pin 10 - GPIO15 - RxD*</option>
<option value="17">Pin 11 - GPIO17</option>
<option value="18">Pin 12 - GPIO18</option>
<option value="27">Pin 13 - GPIO27</option>
<option value="22">Pin 15 - GPIO22</option>
<option value="23">Pin 16 - GPIO23</option>
<option value="24">Pin 18 - GPIO24</option>
<option value="10">Pin 19 - GPIO10 - MOSI*</option>
<option value="9">Pin 21 - GPIO9 - MISO*</option>
<option value="25">Pin 22 - GPIO25</option>
<option value="11">Pin 23 - GPIO11 - SCLK*</option>
<option value="8">Pin 24 - GPIO8 - CE0*</option>
<option value="7">Pin 26 - GPIO7 - CE1*</option>
<option value="5">Pin 29 - GPIO05</option>
<option value="6">Pin 31 - GPIO06</option>
<option value="12">Pin 32 - GPIO12</option>
<option value="13">Pin 33 - GPIO13</option>
<option value="19">Pin 35 - GPIO19</option>
<option value="16">Pin 36 - GPIO16</option>
<option value="26">Pin 37 - GPIO26</option>
<option value="20">Pin 38 - GPIO20</option>
<option value="21">Pin 40 - GPIO21</option>
</select>
&nbsp;<span id="pitype"></span>
</div>
<div class="form-row" id="node-set-pwm">
<label>&nbsp;&nbsp;&nbsp;&nbsp;<span data-i18n="pi-gpiod.label.type"></span></label>
<select id="node-input-out" style="width: 250px;">
<option value="out" data-i18n="pi-gpiod.digout"></option>
<option value="pwm" data-i18n="pi-gpiod.pwmout"></option>
<option value="ser" data-i18n="pi-gpiod.servo"></option>
</select>
</div>
<div class="form-row" id="node-set-minimax">
<label>&nbsp;&nbsp;&nbsp;&nbsp;<span data-i18n="pi-gpiod.label.limits"></label>
<span data-i18n="pi-gpiod.label.min"></span> <input type="text" id="node-input-sermin" style="width:70px;"> mS
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<span data-i18n="pi-gpiod.label.max"></span> <input type="text" id="node-input-sermax" style="width:70px;"> mS
</div>
<div class="form-row" id="node-set-tick">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-set" style="display:inline-block; width:auto; vertical-align:top;">
<label for="node-input-set" style="width:70%;"><span data-i18n="pi-gpiod.label.initpin"></span></label>
</div>
<div class="form-row" id="node-set-state">
<label for="node-input-level">&nbsp;</label>
<select id="node-input-level" style="width:250px;">
<option value="0" data-i18n="pi-gpiod.initpin0"></option>
<option value="1" data-i18n="pi-gpiod.initpin1"></option>
</select>
</div>
<div class="form-row">
<label for="node-input-host"><i class="fa fa-globe"></i> <span data-i18n="pi-gpiod.label.host"></label>
<input type="text" id="node-input-host" placeholder="localhost" style="width:250px;">
<input type="number" id="node-input-port" placeholder="port" style="width:65px;">
</div>
<br/>
<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>
<div class="form-tips" id="pin-tip">Pins marked with <b>*</b> are dual use. Make sure they are not enabled for
their other use before using as GPIO.</div>
<div class="form-tips" id="dig-tip"><span data-i18n="[html]pi-gpiod.tip.dig"></span></div>
<div class="form-tips" id="pwm-tip"><span data-i18n="[html]pi-gpiod.tip.pwm"></span></div>
<div class="form-tips" id="ser-tip"><span data-i18n="[html]pi-gpiod.tip.ser"></span></div>
</script>
<script type="text/x-red" data-help-name="pi-gpiod out">
<p>Raspberry Pi output node. Can be used in Digital, PWM or Servo modes. Requires the
<a href="http://abyz.co.uk/rpi/pigpio/pigpiod.html" target="_new">pi-gpiod</a> daemon to be running in order to work.</p>
<p><b>Inputs</b>
<ul>
<li><code>msg.payload</code> - <i>number | string</i> - 0,1 (Digital), 0-100 (PWM, Servo)</li>
</ul>
<p><b>Details</b></p>
<p>Digital mode expects a <code>msg.payload</code> with either a 0 or 1 (or true or false),
and will set the selected physical pin high or low depending on the value passed in.</p>
<p>The initial value of the pin at deploy time can also be set to 0 or 1.</p>
<p>When using PWM and Servo modes, the input value should be a number 0 - 100, and can be floating point.</p>
<p><b>Note</b>: the pin numbers refer the physical pin numbers on connector P1 as they are easier to locate.</p>
</script>
<script type="text/javascript">
var bcm2pin = {
"2":"3", "3":"5", "4":"7", "14":"8", "15":"10", "17":"11", "18":"12", "27":"13", "22":"15",
"23":"16", "24":"18", "10":"19", "9":"21", "25":"22", "11":"23", "8":"24", "7":"26",
"5":"29", "6":"31", "12":"32", "13":"33", "19":"35", "16":"36", "26":"37", "20":"38", "21":"40"
};
RED.nodes.registerType('pi-gpiod out',{
category: 'Raspberry Pi',
color:"#c6dbef",
defaults: {
name: { value:"" },
host: { value:"localhost" },
port: { value:8888 },
pin: { value:"", required:true, validate:RED.validators.number() },
set: { value:"" },
level: { value:"0" },
out: { value:"out" },
sermin: { value:"10" },
sermax: { value:"20" }
},
inputs:1,
outputs:0,
icon: "rpi.png",
align: "right",
label: function() {
if (this.out === "pwm") { return this.name || "PWM: "+bcm2pin[this.pin]; }
if (this.out === "ser") { return this.name || "Servo: "+bcm2pin[this.pin]; }
else { return this.name||"GPIO: "+bcm2pin[this.pin] ; }
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
palettelabel: "pi gpiod",
oneditprepare: function() {
var pinnow = bcm2pin[this.pin];
console.log("NOW",pinnow);
var pintip = this._("pi-gpiod.tip.pin");
var pinname = this._("pi-gpiod.pinname");
if (!$("#node-input-out").val()) { $("#node-input-out").val("out"); }
var hidestate = function () {
if ($("#node-input-out").val() === "pwm") {
$('#node-set-tick').hide();
$('#node-set-state').hide();
$('#node-set-minimax').hide();
$('#node-input-set').prop('checked', false);
$("#dig-tip").hide();
$("#pwm-tip").show();
$("#ser-tip").hide();
}
if ($("#node-input-out").val() === "ser") {
$('#node-set-tick').hide();
$('#node-set-state').hide();
$('#node-set-minimax').show();
$('#node-input-set').prop('checked', false);
$("#dig-tip").hide();
$("#pwm-tip").hide();
$("#ser-tip").show();
}
else {
$('#node-set-tick').show();
$('#node-set-minimax').hide();
$("#dig-tip").show();
$("#pwm-tip").hide();
$("#ser-tip").hide();
}
};
$("#node-input-out").change(function () { hidestate(); });
hidestate();
var setstate = function () {
if ($('#node-input-set').is(":checked")) {
$("#node-set-state").show();
} else {
$("#node-set-state").hide();
}
};
$("#node-input-set").change(function () { setstate(); });
setstate();
}
});
</script>

View File

@ -0,0 +1,164 @@
module.exports = function(RED) {
"use strict";
var Pigpio = require('js-pigpio');
var bcm2pin = {
"2":"3", "3":"5", "4":"7", "14":"8", "15":"10", "17":"11", "18":"12", "27":"13", "22":"15",
"23":"16", "24":"18", "10":"19", "9":"21", "25":"22", "11":"23", "8":"24", "7":"26",
"5":"29", "6":"31", "12":"32", "13":"33", "19":"35", "16":"36", "26":"37", "20":"38", "21":"40"
};
var pinTypes = {
"PUD_OFF":RED._("pi-gpiod:types.input"),
"PUD_UP":RED._("pi-gpiod:types.pullup"),
"PUD_DOWN":RED._("pi-gpiod:types.pulldown"),
"out":RED._("pi-gpiod:types.digout"),
"pwm":RED._("pi-gpiod:types.pwmout"),
"ser":RED._("pi-gpiod:types.servo")
};
function GPioInNode(n) {
RED.nodes.createNode(this,n);
this.host = n.host || "127.0.0.1";
this.port = n.port || 8888;
this.pin = n.pin;
this.pio = bcm2pin[n.pin];
this.intype = n.intype;
this.read = n.read || false;
this.debounce = Number(n.debounce || 25);
var node = this;
var PiGPIO;
if (node.pin !== undefined) {
PiGPIO = new Pigpio();
var inerror = false;
var doit = function() {
PiGPIO.pi(node.host, node.port, function(err) {
if (err) {
node.status({fill:"red",shape:"ring",text:err.code+" "+node.host+":"+node.port});
if (!inerror) { node.error(err); inerror = true; }
node.retry = setTimeout(function() { doit(); }, 5000);
}
else {
inerror = false;
PiGPIO.set_mode(node.pin,PiGPIO.INPUT);
PiGPIO.set_pull_up_down(node.pin,PiGPIO[node.intype]);
PiGPIO.set_glitch_filter(node.pin,node.debounce);
node.status({fill:"green",shape:"dot",text:"node-red:common.status.ok"});
node.cb = PiGPIO.callback(node.pin, PiGPIO.EITHER_EDGE, function(gpio, level, tick) {
node.send({ topic:"pi/"+node.pio, payload:Number(level) });
node.status({fill:"green",shape:"dot",text:level});
});
if (node.read) {
setTimeout(function() {
PiGPIO.read(node.pin, function(err, level) {
node.send({ topic:"pi/"+node.pio, payload:Number(level) });
node.status({fill:"green",shape:"dot",text:level});
});
}, 20);
}
}
});
}
doit();
}
else {
node.warn(RED._("pi-gpiod:errors.invalidpin")+": "+node.pio);
}
node.on("close", function(done) {
if (node.retry) { clearTimeout(node.retry); }
node.status({fill:"grey",shape:"ring",text:"pi-gpiod.status.closed"});
node.cb.cancel();
PiGPIO.close();
done();
});
}
RED.nodes.registerType("pi-gpiod in",GPioInNode);
function GPioOutNode(n) {
RED.nodes.createNode(this,n);
this.host = n.host || "127.0.0.1";
this.port = n.port || 8888;
this.pin = n.pin;
this.pio = bcm2pin[n.pin];
this.set = n.set || false;
this.level = parseInt(n.level || 0);
this.out = n.out || "out";
this.sermin = Number(n.sermin);
this.sermax = Number(n.sermax);
if (this.sermin > this.sermax) {
var tmp = this.sermin;
this.sermin = this.sermax;
this.sermax = tmp;
}
if (this.sermin < 5) { this.sermin = 5; }
if (this.sermax > 25) { this.sermax = 25; }
var node = this;
var PiGPIO;
function inputlistener(msg) {
if (msg.payload === "true") { msg.payload = true; }
if (msg.payload === "false") { msg.payload = false; }
var out = Number(msg.payload);
var limit = 1;
if (node.out !== "out") { limit = 100; }
if ((out >= 0) && (out <= limit)) {
if (RED.settings.verbose) { node.log("out: "+msg.payload); }
if (node.out === "out") {
PiGPIO.write(node.pin, msg.payload);
}
if (node.out === "pwm") {
PiGPIO.set_PWM_dutycycle(node.pin, parseInt(msg.payload * 2.55));
}
if (node.out === "ser") {
var r = (node.sermax - node.sermin) * 100;
PiGPIO.setServoPulsewidth(node.pin, parseInt(1500 - (r/2) + (msg.payload * r / 100)));
}
node.status({fill:"green",shape:"dot",text:msg.payload.toString()});
}
else { node.warn(RED._("pi-gpiod:errors.invalidinput")+": "+out); }
}
if (node.pin !== undefined) {
PiGPIO = new Pigpio();
var inerror = false;
var doit = function() {
PiGPIO.pi(node.host, node.port, function(err) {
if (err) {
node.status({fill:"red",shape:"ring",text:err.code+" "+node.host+":"+node.port});
if (!inerror) { node.error(err,err); inerror = true; }
node.retry = setTimeout(function() { doit(); }, 5000);
}
else {
PiGPIO.set_mode(node.pin,PiGPIO.OUTPUT);
if (node.set) {
setTimeout(function() { PiGPIO.write(node.pin,node.level); }, 25 );
node.status({fill:"green",shape:"dot",text:node.level});
} else {
node.status({fill:"green",shape:"dot",text:"node-red:common.status.ok"});
}
// if (node.out === "pwm") {
// PiGPIO.set_PWM_frequency(1000);
// PiGPIO.set_PWM_range(1000);
// }
}
});
}
doit();
node.on("input", inputlistener);
}
else {
node.warn(RED._("pi-gpiod:errors.invalidpin")+": "+node.pio);
}
node.on("close", function(done) {
if (node.retry) { clearTimeout(node.retry); }
node.status({fill:"grey",shape:"ring",text:"pi-gpiod.status.closed"});
PiGPIO.close();
done();
});
}
RED.nodes.registerType("pi-gpiod out",GPioOutNode);
}