node-red-contrib-periph-light 1.5.2 → 1.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # node-red-contrib-periph-light
2
2
 
3
- Node-RED nodes for light sensor chips — part of the [Periph](https://github.com/tuhde/Periph) library.
3
+ Node-RED nodes for light sensor chips: APDS-9930, APDS9250, APDS9960, BH1750, MAX44009, SI1132 — part of the [Periph](https://github.com/tuhde/Periph) library.
4
+
5
+ **Supported chips (6):** APDS9930, APDS9250, APDS9960, BH1750, MAX44009, SI1132.
6
+
7
+ Each chip has a sensor/actuator node plus a shared `-device` config node that holds the bus settings. Importable example flows are in [`examples/`](https://github.com/tuhde/Periph/tree/main/nodejs/packages/node-red-contrib-periph-light/examples).
4
8
 
5
9
  ## Install
6
10
 
@@ -18,10 +22,16 @@ npm install node-red-contrib-periph-light
18
22
  |------|------|-------------|
19
23
  | `apds9930-device` | config | I²C bus and address for an APDS9930 |
20
24
  | `apds9930` | input | Reads ambient illuminance and proximity from an APDS-9930 sensor over I²C. |
25
+ | `apds9250-device` | config | Bus configuration for an APDS9250 |
26
+ | `apds9250` | input | Reads the red, green, blue and infrared channels of an APDS-9250 digital RGB, IR and ambient light sensor (I²C 0x52, 13–20 bit). |
21
27
  | `apds9960-device` | config | I²C bus and address for an APDS9960 |
22
28
  | `apds9960` | input | Reads ambient light and color (RGBC) from an APDS-9960 sensor over I²C. |
29
+ | `bh1750-device` | config | I²C bus and address for a BH1750 |
30
+ | `bh1750` | input | Reads the illuminance from a ROHM BH1750FVI ambient light sensor over I²C (address 0x23 or 0x5C) with one one-time measurement per message. |
23
31
  | `max44009-device` | config | I²C bus and address for a MAX44009 |
24
32
  | `max44009` | input | Reads the ambient illuminance of a MAX44009 ultra-low-power light sensor (I²C, 0.045 lx to 188 006.4 lx) and arms its threshold interrupt. |
33
+ | `si1132-device` | config | I²C bus and address for a SI1132 |
34
+ | `si1132` | input | Reads the UV index and the raw ambient-light counts from a Silicon Labs Si1132 over I²C (address 0x60) with one forced measurement per message. |
25
35
 
26
36
  ## Links
27
37
 
@@ -0,0 +1,83 @@
1
+ [
2
+ {
3
+ "id": "tab-apds9250-demo",
4
+ "type": "tab",
5
+ "label": "APDS-9250 Colour Demo",
6
+ "disabled": false,
7
+ "info": "Colour sampling with an APDS-9250 RGB/IR light sensor. An inject node fires every 2 s into apds9250; a function node normalises the red, green and blue counts to an #rrggbb colour and computes the IR fraction; a debug node shows the result."
8
+ },
9
+ {
10
+ "id": "inj-read",
11
+ "type": "inject",
12
+ "name": "Read every 2 s",
13
+ "topic": "",
14
+ "payload": "",
15
+ "payloadType": "date",
16
+ "repeat": "2",
17
+ "crontab": "",
18
+ "once": true,
19
+ "onceDelay": 1,
20
+ "z": "tab-apds9250-demo",
21
+ "x": 150,
22
+ "y": 100,
23
+ "wires": [
24
+ [
25
+ "apds9250-node"
26
+ ]
27
+ ]
28
+ },
29
+ {
30
+ "id": "apds9250-device-1",
31
+ "type": "apds9250-device",
32
+ "name": "APDS-9250",
33
+ "bus": "1",
34
+ "resolution": "18",
35
+ "rate": "100",
36
+ "gain": "3"
37
+ },
38
+ {
39
+ "id": "apds9250-node",
40
+ "type": "apds9250",
41
+ "name": "APDS-9250",
42
+ "z": "tab-apds9250-demo",
43
+ "x": 400,
44
+ "y": 140,
45
+ "wires": [
46
+ [
47
+ "fn-colour"
48
+ ]
49
+ ],
50
+ "device": "apds9250-device-1"
51
+ },
52
+ {
53
+ "id": "fn-colour",
54
+ "type": "function",
55
+ "name": "counts to #rrggbb",
56
+ "func": "// Normalise R/G/B to the strongest channel and compute the IR fraction.\nconst p = msg.payload;\nconst top = Math.max(p.red, p.green, p.blue, 1);\nconst h = (v) => Math.floor(v * 255 / top).toString(16).padStart(2, '0');\np.colour = '#' + h(p.red) + h(p.green) + h(p.blue);\np.irFraction = p.ir / Math.max(1, p.ir + p.green);\nreturn msg;",
57
+ "outputs": 1,
58
+ "noerr": 0,
59
+ "z": "tab-apds9250-demo",
60
+ "x": 640,
61
+ "y": 140,
62
+ "wires": [
63
+ [
64
+ "dbg-apds9250"
65
+ ]
66
+ ]
67
+ },
68
+ {
69
+ "id": "dbg-apds9250",
70
+ "type": "debug",
71
+ "name": "colour / IR",
72
+ "active": true,
73
+ "tosidebar": true,
74
+ "console": false,
75
+ "tostatus": false,
76
+ "complete": "payload",
77
+ "targetType": "msg",
78
+ "z": "tab-apds9250-demo",
79
+ "x": 860,
80
+ "y": 140,
81
+ "wires": []
82
+ }
83
+ ]
@@ -0,0 +1,92 @@
1
+ [
2
+ {
3
+ "id": "bh1750-tab",
4
+ "type": "tab",
5
+ "label": "BH1750 light meter",
6
+ "disabled": false,
7
+ "info": "Measures the illuminance with a BH1750 every 2 s and formats it as \"350 lx\". Each message triggers one one-time measurement; change the mode (H 1 lx, H2 0.5 lx, L 4 lx) or the measurement time register in the BH1750 device node to trade resolution, range and measurement time."
8
+ },
9
+ {
10
+ "id": "bh1750-inject",
11
+ "type": "inject",
12
+ "z": "bh1750-tab",
13
+ "name": "2 s timer",
14
+ "props": [
15
+ {
16
+ "p": "payload"
17
+ }
18
+ ],
19
+ "repeat": "2",
20
+ "crontab": "",
21
+ "once": false,
22
+ "onceDelay": 0.1,
23
+ "topic": "",
24
+ "payloadType": "date",
25
+ "x": 160,
26
+ "y": 160,
27
+ "wires": [
28
+ [
29
+ "bh1750-node"
30
+ ]
31
+ ]
32
+ },
33
+ {
34
+ "id": "bh1750-device-1",
35
+ "type": "bh1750-device",
36
+ "name": "BH1750",
37
+ "bus": "1",
38
+ "address": "0x23",
39
+ "mode": "0",
40
+ "mtreg": "69"
41
+ },
42
+ {
43
+ "id": "bh1750-node",
44
+ "type": "bh1750",
45
+ "z": "bh1750-tab",
46
+ "name": "BH1750",
47
+ "x": 380,
48
+ "y": 160,
49
+ "wires": [
50
+ [
51
+ "bh1750-format"
52
+ ]
53
+ ],
54
+ "device": "bh1750-device-1"
55
+ },
56
+ {
57
+ "id": "bh1750-format",
58
+ "type": "function",
59
+ "z": "bh1750-tab",
60
+ "name": "Format",
61
+ "func": "msg.payload = Math.round(msg.payload.lux) + ' lx';\nreturn msg;",
62
+ "outputs": 1,
63
+ "noerr": 0,
64
+ "initialize": "",
65
+ "finalize": "",
66
+ "libs": [],
67
+ "x": 600,
68
+ "y": 160,
69
+ "wires": [
70
+ [
71
+ "bh1750-debug"
72
+ ]
73
+ ]
74
+ },
75
+ {
76
+ "id": "bh1750-debug",
77
+ "type": "debug",
78
+ "z": "bh1750-tab",
79
+ "name": "Reading",
80
+ "active": true,
81
+ "tosidebar": true,
82
+ "console": false,
83
+ "tostatus": false,
84
+ "complete": "payload",
85
+ "targetType": "msg",
86
+ "statusVal": "",
87
+ "statusType": "auto",
88
+ "x": 800,
89
+ "y": 160,
90
+ "wires": []
91
+ }
92
+ ]
@@ -0,0 +1,92 @@
1
+ [
2
+ {
3
+ "id": "si1132-tab",
4
+ "type": "tab",
5
+ "label": "Si1132 UV monitor",
6
+ "disabled": false,
7
+ "info": "Reads the UV index and the visible and infrared counts from an Si1132 every 2 s and classifies the UV index as low (below 3), moderate (3 to 5.9) or high (6 and above). Set the high-range flags in the Si1132 device node for direct sunlight."
8
+ },
9
+ {
10
+ "id": "si1132-inject",
11
+ "type": "inject",
12
+ "z": "si1132-tab",
13
+ "name": "2 s timer",
14
+ "props": [
15
+ {
16
+ "p": "payload"
17
+ }
18
+ ],
19
+ "repeat": "2",
20
+ "crontab": "",
21
+ "once": false,
22
+ "onceDelay": 0.1,
23
+ "topic": "",
24
+ "payloadType": "date",
25
+ "x": 160,
26
+ "y": 160,
27
+ "wires": [
28
+ [
29
+ "si1132-node"
30
+ ]
31
+ ]
32
+ },
33
+ {
34
+ "id": "si1132-device-1",
35
+ "type": "si1132-device",
36
+ "name": "Si1132",
37
+ "bus": "1",
38
+ "address": "0x60",
39
+ "highRangeVisible": false,
40
+ "highRangeInfrared": false
41
+ },
42
+ {
43
+ "id": "si1132-node",
44
+ "type": "si1132",
45
+ "z": "si1132-tab",
46
+ "name": "Si1132",
47
+ "x": 380,
48
+ "y": 160,
49
+ "wires": [
50
+ [
51
+ "si1132-format"
52
+ ]
53
+ ],
54
+ "device": "si1132-device-1"
55
+ },
56
+ {
57
+ "id": "si1132-format",
58
+ "type": "function",
59
+ "z": "si1132-tab",
60
+ "name": "Classify",
61
+ "func": "const uv = msg.payload.uv_index;\nconst level = uv < 3 ? 'low' : (uv < 6 ? 'moderate' : 'high');\nmsg.payload = 'UV ' + uv.toFixed(2) + ' (' + level + '), visible ' + msg.payload.visible + ', infrared ' + msg.payload.infrared;\nreturn msg;",
62
+ "outputs": 1,
63
+ "noerr": 0,
64
+ "initialize": "",
65
+ "finalize": "",
66
+ "libs": [],
67
+ "x": 600,
68
+ "y": 160,
69
+ "wires": [
70
+ [
71
+ "si1132-debug"
72
+ ]
73
+ ]
74
+ },
75
+ {
76
+ "id": "si1132-debug",
77
+ "type": "debug",
78
+ "z": "si1132-tab",
79
+ "name": "Reading",
80
+ "active": true,
81
+ "tosidebar": true,
82
+ "console": false,
83
+ "tostatus": false,
84
+ "complete": "payload",
85
+ "targetType": "msg",
86
+ "statusVal": "",
87
+ "statusType": "auto",
88
+ "x": 800,
89
+ "y": 160,
90
+ "wires": []
91
+ }
92
+ ]
@@ -0,0 +1,98 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('apds9250-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ resolution: { value: '18' },
9
+ rate: { value: '100' },
10
+ gain: { value: '3' }
11
+ },
12
+ label: function() {
13
+ return this.name || ('APDS-9250 @ 0x52 (bus ' + this.bus + ')');
14
+ }
15
+ });
16
+ </script>
17
+
18
+ <script type="text/html" data-template-name="apds9250-device">
19
+ <div class="form-row">
20
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
21
+ <input type="text" id="node-config-input-name" placeholder="APDS-9250">
22
+ </div>
23
+ <div class="form-row">
24
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
25
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N, address 0x52)
26
+ </div>
27
+ <div class="form-row">
28
+ <label for="node-config-input-resolution">Resolution</label>
29
+ <select id="node-config-input-resolution" style="width:160px">
30
+ <option value="20">20 bit (400 ms)</option><option value="19">19 bit (200 ms)</option>
31
+ <option value="18">18 bit (100 ms)</option><option value="17">17 bit (50 ms)</option>
32
+ <option value="16">16 bit (25 ms)</option><option value="13">13 bit (3.125 ms)</option>
33
+ </select>
34
+ </div>
35
+ <div class="form-row">
36
+ <label for="node-config-input-rate">Rate</label>
37
+ <select id="node-config-input-rate" style="width:160px">
38
+ <option value="25">25 ms</option><option value="50">50 ms</option>
39
+ <option value="100">100 ms</option><option value="200">200 ms</option>
40
+ <option value="500">500 ms</option><option value="1000">1000 ms</option>
41
+ <option value="2000">2000 ms</option>
42
+ </select>
43
+ </div>
44
+ <div class="form-row">
45
+ <label for="node-config-input-gain">Gain</label>
46
+ <select id="node-config-input-gain" style="width:160px">
47
+ <option value="1">×1</option><option value="3">×3</option><option value="6">×6</option>
48
+ <option value="9">×9</option><option value="18">×18</option>
49
+ </select>
50
+ </div>
51
+ </script>
52
+
53
+ <!-- Read node -->
54
+ <script type="text/javascript">
55
+ RED.nodes.registerType('apds9250', {
56
+ category: 'Peripheral',
57
+ color: '#C084FC',
58
+ defaults: {
59
+ name: { value: '' },
60
+ device: { value: '', type: 'apds9250-device', required: true }
61
+ },
62
+ inputs: 1,
63
+ outputs: 1,
64
+ icon: 'font-awesome/fa-sun-o',
65
+ label: function() { return this.name || 'APDS-9250'; },
66
+ paletteLabel: 'APDS-9250'
67
+ });
68
+ </script>
69
+
70
+ <script type="text/html" data-template-name="apds9250">
71
+ <div class="form-row">
72
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
73
+ <input type="text" id="node-input-name" placeholder="APDS-9250">
74
+ </div>
75
+ <div class="form-row">
76
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
77
+ <input type="text" id="node-input-device">
78
+ </div>
79
+ </script>
80
+
81
+ <script type="text/html" data-help-name="apds9250">
82
+ <p>Reads the red, green, blue and infrared channels of an APDS-9250 digital RGB, IR and ambient light sensor (I²C 0x52, 13–20 bit).</p>
83
+
84
+ <h3>Inputs</h3>
85
+ <dl class="message-properties">
86
+ <dt>payload <span class="property-type">any</span></dt>
87
+ <dd>Any message triggers a read of the latest conversion.</dd>
88
+ </dl>
89
+
90
+ <h3>Outputs</h3>
91
+ <dl class="message-properties">
92
+ <dt>payload.red, payload.green, payload.blue, payload.ir <span class="property-type">number</span></dt>
93
+ <dd>Unsigned channel counts (up to 20 bit). The green channel doubles as the ambient-light channel.</dd>
94
+ </dl>
95
+
96
+ <h3>Configuration</h3>
97
+ <p>Set the Linux I²C bus number (the address is fixed at 0x52). <b>Resolution</b> sets the ADC resolution and with it the integration time (13 bit 3.125 ms to 20 bit 400 ms), <b>Rate</b> the measurement period (25 ms to 2 s) and <b>Gain</b> the analog gain (×1 to ×18). The chip is not 5 V tolerant (VDD 1.7–3.6 V). Counts are not lux; normalise with <code>counts / (gain × integration time)</code> to compare settings.</p>
98
+ </script>
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { APDS9250Full } = require('periph/src/chips/light/apds9250');
6
+
7
+ function APDS9250DeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ node.connection = new I2CConnection(parseInt(config.bus, 10), 0x52);
12
+ node.driver = new APDS9250Full(node.connection);
13
+ node.ready = (async () => {
14
+ await node.driver.init();
15
+ await node.driver.setResolution(parseInt(config.resolution, 10) || 18);
16
+ await node.driver.setMeasurementRate(parseInt(config.rate, 10) || 100);
17
+ await node.driver.setGain(parseInt(config.gain, 10) || 3);
18
+ })();
19
+ node.ready.then(() => node.status({ fill: 'green', shape: 'dot', text: 'I2C' }))
20
+ .catch((e) => node.error('APDS9250 init failed: ' + e.message));
21
+ } catch (e) {
22
+ node.error('APDS9250 init failed: ' + e.message);
23
+ }
24
+
25
+ node.on('close', async function(removed, done) {
26
+ try {
27
+ if (node.driver) await node.driver.offInterrupt();
28
+ if (node.connection) await node.connection.close();
29
+ } catch (e) {
30
+ // bus may already be gone; nothing more to do on close
31
+ } finally {
32
+ done();
33
+ }
34
+ });
35
+ }
36
+ RED.nodes.registerType('apds9250-device', APDS9250DeviceNode);
37
+
38
+ function APDS9250ReadNode(config) {
39
+ RED.nodes.createNode(this, config);
40
+ const node = this;
41
+ const deviceNode = RED.nodes.getNode(config.device) || {};
42
+
43
+ node.on('input', async function(msg, send, done) {
44
+ if (!deviceNode.driver) { done(new Error('APDS9250 not initialised')); return; }
45
+ try {
46
+ await deviceNode.ready;
47
+ const counts = await deviceNode.driver.read();
48
+ msg.payload = counts;
49
+ send(msg);
50
+ done();
51
+ } catch (e) {
52
+ done(e);
53
+ }
54
+ });
55
+ }
56
+ RED.nodes.registerType('apds9250', APDS9250ReadNode);
57
+ };
@@ -0,0 +1,91 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('bh1750-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '0x23' },
9
+ mode: { value: '0' },
10
+ mtreg: { value: '69', validate: RED.validators.number() }
11
+ },
12
+ label: function() {
13
+ return this.name || ('BH1750 @ ' + this.address + ' (bus ' + this.bus + ')');
14
+ }
15
+ });
16
+ </script>
17
+
18
+ <script type="text/html" data-template-name="bh1750-device">
19
+ <div class="form-row">
20
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
21
+ <input type="text" id="node-config-input-name" placeholder="BH1750">
22
+ </div>
23
+ <div class="form-row">
24
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
25
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
26
+ </div>
27
+ <div class="form-row">
28
+ <label for="node-config-input-address"><i class="fa fa-microchip"></i> I²C address</label>
29
+ <input type="text" id="node-config-input-address" style="width:80px" placeholder="0x23"> &nbsp;(0x23 or 0x5C, ADDR pin)
30
+ </div>
31
+ <div class="form-row">
32
+ <label for="node-config-input-mode"><i class="fa fa-cog"></i> Mode</label>
33
+ <select id="node-config-input-mode" style="width:200px">
34
+ <option value="0" selected>H-resolution (1 lx)</option>
35
+ <option value="1">H-resolution 2 (0.5 lx)</option>
36
+ <option value="2">L-resolution (4 lx, 16 ms)</option>
37
+ </select>
38
+ </div>
39
+ <div class="form-row">
40
+ <label for="node-config-input-mtreg"><i class="fa fa-cog"></i> Measurement time register</label>
41
+ <input type="number" id="node-config-input-mtreg" min="31" max="254" style="width:80px"> &nbsp;(31-254, default 69)
42
+ </div>
43
+ </script>
44
+
45
+ <!-- Read node -->
46
+ <script type="text/javascript">
47
+ RED.nodes.registerType('bh1750', {
48
+ category: 'Peripheral',
49
+ color: '#C084FC',
50
+ defaults: {
51
+ name: { value: '' },
52
+ device: { value: '', type: 'bh1750-device', required: true }
53
+ },
54
+ inputs: 1,
55
+ outputs: 1,
56
+ icon: 'bridge.svg',
57
+ label: function() { return this.name || 'BH1750'; },
58
+ paletteLabel: 'BH1750'
59
+ });
60
+ </script>
61
+
62
+ <script type="text/html" data-template-name="bh1750">
63
+ <div class="form-row">
64
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
65
+ <input type="text" id="node-input-name" placeholder="BH1750">
66
+ </div>
67
+ <div class="form-row">
68
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
69
+ <input type="text" id="node-input-device">
70
+ </div>
71
+ </script>
72
+
73
+ <script type="text/html" data-help-name="bh1750">
74
+ <p>Reads the illuminance from a ROHM BH1750FVI ambient light sensor over I²C (address 0x23 or 0x5C)
75
+ with one one-time measurement per message.</p>
76
+ <h3>Inputs</h3>
77
+ <dl class="message-properties">
78
+ <dt>payload <span class="property-type">any</span></dt>
79
+ <dd>Any incoming message triggers one measurement; the node waits for the full measurement time (180 ms in the
80
+ H modes, 24 ms in L mode, scaled with the measurement time register).</dd>
81
+ </dl>
82
+ <h3>Outputs</h3>
83
+ <dl class="message-properties">
84
+ <dt>payload.lux <span class="property-type">number</span></dt>
85
+ <dd>Illuminance in lux.</dd>
86
+ </dl>
87
+ <h3>Configuration</h3>
88
+ <p>I²C bus number, I²C address (0x23 or 0x5C), the measurement mode (H-resolution 1 lx, H-resolution 2 0.5 lx or
89
+ L-resolution 4 lx) and the measurement time register (31 to 254, default 69; a larger value raises the sensitivity
90
+ and lengthens the measurement), set in the <b>bh1750-device</b> config node.</p>
91
+ </script>
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { BH1750Full } = require('periph/src/chips/light/bh1750');
6
+
7
+ function BH1750DeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const address = parseInt(config.address, 16) || 0x23;
12
+ const connection = new I2CConnection(parseInt(config.bus), address);
13
+ node.connection = connection;
14
+ node.driver = new BH1750Full(connection);
15
+ const mode = parseInt(config.mode);
16
+ node.mode = isNaN(mode) ? BH1750Full.MODE_H : mode;
17
+ node.ready = (async () => {
18
+ await node.driver.init();
19
+ const mtreg = parseInt(config.mtreg);
20
+ await node.driver.setMeasurementTime(isNaN(mtreg) ? 69 : mtreg);
21
+ })();
22
+ node.ready.catch((e) => {
23
+ node.error('BH1750 init failed: ' + e.message);
24
+ });
25
+ } catch (e) {
26
+ node.error('BH1750 init failed: ' + e.message);
27
+ }
28
+
29
+ node.on('close', function() {
30
+ if (node.connection) node.connection.close();
31
+ });
32
+ }
33
+ RED.nodes.registerType('bh1750-device', BH1750DeviceNode);
34
+
35
+ function BH1750ReadNode(config) {
36
+ RED.nodes.createNode(this, config);
37
+ const node = this;
38
+ const deviceNode = RED.nodes.getNode(config.device) || {};
39
+
40
+ node.on('input', async function(msg, send, done) {
41
+ if (!deviceNode.driver) {
42
+ node.error('BH1750 not initialised', msg);
43
+ done();
44
+ return;
45
+ }
46
+ try {
47
+ await deviceNode.ready;
48
+ const lux = await deviceNode.driver.measureOnce(deviceNode.mode);
49
+ msg.payload = { lux };
50
+ send(msg);
51
+ done();
52
+ } catch (e) {
53
+ done(e);
54
+ }
55
+ });
56
+ }
57
+ RED.nodes.registerType('bh1750', BH1750ReadNode);
58
+ };
@@ -0,0 +1,89 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('si1132-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '0x60' },
9
+ highRangeVisible: { value: false },
10
+ highRangeInfrared: { value: false }
11
+ },
12
+ label: function() {
13
+ return this.name || ('Si1132 @ ' + this.address + ' (bus ' + this.bus + ')');
14
+ }
15
+ });
16
+ </script>
17
+
18
+ <script type="text/html" data-template-name="si1132-device">
19
+ <div class="form-row">
20
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
21
+ <input type="text" id="node-config-input-name" placeholder="Si1132">
22
+ </div>
23
+ <div class="form-row">
24
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
25
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
26
+ </div>
27
+ <div class="form-row">
28
+ <label for="node-config-input-address"><i class="fa fa-microchip"></i> I²C address</label>
29
+ <input type="text" id="node-config-input-address" style="width:80px" placeholder="0x60"> &nbsp;(fixed 0x60)
30
+ </div>
31
+ <div class="form-row">
32
+ <label for="node-config-input-highRangeVisible"><i class="fa fa-sun-o"></i> High range visible</label>
33
+ <input type="checkbox" id="node-config-input-highRangeVisible" style="width:auto"> &nbsp;sensitivity ÷ 14.5 (direct sunlight)
34
+ </div>
35
+ <div class="form-row">
36
+ <label for="node-config-input-highRangeInfrared"><i class="fa fa-sun-o"></i> High range infrared</label>
37
+ <input type="checkbox" id="node-config-input-highRangeInfrared" style="width:auto"> &nbsp;sensitivity ÷ 14.5 (direct sunlight)
38
+ </div>
39
+ </script>
40
+
41
+ <!-- Read node -->
42
+ <script type="text/javascript">
43
+ RED.nodes.registerType('si1132', {
44
+ category: 'Peripheral',
45
+ color: '#C084FC',
46
+ defaults: {
47
+ name: { value: '' },
48
+ device: { value: '', type: 'si1132-device', required: true }
49
+ },
50
+ inputs: 1,
51
+ outputs: 1,
52
+ icon: 'bridge.svg',
53
+ label: function() { return this.name || 'Si1132'; },
54
+ paletteLabel: 'Si1132'
55
+ });
56
+ </script>
57
+
58
+ <script type="text/html" data-template-name="si1132">
59
+ <div class="form-row">
60
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
61
+ <input type="text" id="node-input-name" placeholder="Si1132">
62
+ </div>
63
+ <div class="form-row">
64
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
65
+ <input type="text" id="node-input-device">
66
+ </div>
67
+ </script>
68
+
69
+ <script type="text/html" data-help-name="si1132">
70
+ <p>Reads the UV index and the raw ambient-light counts from a Silicon Labs Si1132 over I²C (address 0x60)
71
+ with one forced measurement per message.</p>
72
+ <h3>Inputs</h3>
73
+ <dl class="message-properties">
74
+ <dt>payload <span class="property-type">any</span></dt>
75
+ <dd>Any incoming message triggers one measurement.</dd>
76
+ </dl>
77
+ <h3>Outputs</h3>
78
+ <dl class="message-properties">
79
+ <dt>payload.uv_index <span class="property-type">number</span></dt>
80
+ <dd>UV index (the chip reports 100 times the index).</dd>
81
+ <dt>payload.visible <span class="property-type">number</span></dt>
82
+ <dd>Raw 16-bit visible ambient-light count.</dd>
83
+ <dt>payload.infrared <span class="property-type">number</span></dt>
84
+ <dd>Raw 16-bit infrared ambient-light count.</dd>
85
+ </dl>
86
+ <h3>Configuration</h3>
87
+ <p>I²C bus number, I²C address (0x60) and the high-range flags for the visible and infrared channels
88
+ (sensitivity ÷ 14.5, for direct sunlight), set in the <b>si1132-device</b> config node.</p>
89
+ </script>
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { Si1132Full } = require('periph/src/chips/light/si1132');
6
+
7
+ function Si1132DeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const address = parseInt(config.address, 16) || 0x60;
12
+ const connection = new I2CConnection(parseInt(config.bus), address);
13
+ node.connection = connection;
14
+ node.driver = new Si1132Full(connection);
15
+ node.ready = (async () => {
16
+ await node.driver.init();
17
+ const visible = config.highRangeVisible === true || config.highRangeVisible === 'true';
18
+ const infrared = config.highRangeInfrared === true || config.highRangeInfrared === 'true';
19
+ if (visible || infrared) await node.driver.setHighRange(visible, infrared);
20
+ })();
21
+ node.ready.catch((e) => {
22
+ node.error('Si1132 init failed: ' + e.message);
23
+ });
24
+ } catch (e) {
25
+ node.error('Si1132 init failed: ' + e.message);
26
+ }
27
+
28
+ node.on('close', function() {
29
+ if (node.connection) node.connection.close();
30
+ });
31
+ }
32
+ RED.nodes.registerType('si1132-device', Si1132DeviceNode);
33
+
34
+ function Si1132ReadNode(config) {
35
+ RED.nodes.createNode(this, config);
36
+ const node = this;
37
+ const deviceNode = RED.nodes.getNode(config.device) || {};
38
+
39
+ node.on('input', async function(msg, send, done) {
40
+ if (!deviceNode.driver) {
41
+ node.error('Si1132 not initialised', msg);
42
+ done();
43
+ return;
44
+ }
45
+ try {
46
+ await deviceNode.ready;
47
+ const { uv_index, visible, infrared } = await deviceNode.driver.read();
48
+ msg.payload = { uv_index, visible, infrared };
49
+ send(msg);
50
+ done();
51
+ } catch (e) {
52
+ done(e);
53
+ }
54
+ });
55
+ }
56
+ RED.nodes.registerType('si1132', Si1132ReadNode);
57
+ };
package/package.json CHANGED
@@ -1,24 +1,47 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-light",
3
- "version": "1.5.2",
4
- "description": "Node-RED nodes for light sensor chips",
3
+ "version": "1.5.4",
4
+ "description": "Node-RED nodes for light sensor chips: APDS-9930, APDS9250, APDS9960, BH1750, MAX44009, SI1132",
5
5
  "keywords": [
6
6
  "node-red",
7
7
  "light",
8
8
  "i2c",
9
9
  "spi",
10
10
  "sensor",
11
- "peripheral"
11
+ "peripheral",
12
+ "node-red-contrib",
13
+ "apds9250",
14
+ "apds-9930",
15
+ "apds9960",
16
+ "bh1750",
17
+ "max44009",
18
+ "si1132"
12
19
  ],
13
20
  "node-red": {
21
+ "version": ">=3.0.0",
14
22
  "nodes": {
15
23
  "apds-9930": "nodes/apds-9930/apds-9930.js",
24
+ "apds9250": "nodes/apds9250/apds9250.js",
16
25
  "apds9960": "nodes/apds9960/apds9960.js",
17
- "max44009": "nodes/max44009/max44009.js"
26
+ "bh1750": "nodes/bh1750/bh1750.js",
27
+ "max44009": "nodes/max44009/max44009.js",
28
+ "si1132": "nodes/si1132/si1132.js"
18
29
  }
19
30
  },
20
31
  "dependencies": {
21
32
  "periph": "*"
22
33
  },
23
- "license": "MIT"
34
+ "license": "MIT",
35
+ "engines": {
36
+ "node": ">=18"
37
+ },
38
+ "bugs": {
39
+ "url": "https://github.com/tuhde/Periph/issues"
40
+ },
41
+ "homepage": "https://github.com/tuhde/Periph/tree/main/nodejs/packages/node-red-contrib-periph-light#readme",
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/tuhde/Periph.git",
45
+ "directory": "nodejs/packages/node-red-contrib-periph-light"
46
+ }
24
47
  }