node-red-contrib-periph-temperature 1.5.1 → 1.5.3

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-temperature
2
2
 
3
- Node-RED nodes for temperature sensor chips — part of the [Periph](https://github.com/tuhde/Periph) library.
3
+ Node-RED nodes for temperature sensor chips: MCP9808, TMP102, TMP117 — part of the [Periph](https://github.com/tuhde/Periph) library.
4
+
5
+ **Supported chips (3):** MCP9808, TMP102, TMP117.
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-temperature/examples).
4
8
 
5
9
  ## Install
6
10
 
@@ -16,9 +20,12 @@ npm install node-red-contrib-periph-temperature
16
20
 
17
21
  | Node | Kind | Description |
18
22
  |------|------|-------------|
19
- | `periph-mcp9808` | input | Reads the ambient temperature from an MCP9808 ±0.5 °C digital temperature sensor over I²C, and optionally reports its TUPPER/TLOWER/TCRIT boundary alerts. |
20
- | `periph-tmp102` | input | Reads the temperature from a TMP102 12-bit digital temperature sensor over I²C. |
21
- | `periph-tmp117` | input | Reads the temperature from a TMP117 ±0.1 °C high-accuracy digital temperature sensor over I²C, and optionally reports its THIGH/TLOW limit alerts. |
23
+ | `mcp9808-device` | config | I²C bus and address for a MCP9808 |
24
+ | `mcp9808` | input | Reads the ambient temperature from an MCP9808 ±0.5 °C digital temperature sensor over I²C, and optionally reports its TUPPER/TLOWER/TCRIT boundary alerts. |
25
+ | `tmp102-device` | config | I²C bus and address for a TMP102 |
26
+ | `tmp102` | input | Reads the temperature from a TMP102 12-bit digital temperature sensor over I²C. |
27
+ | `tmp117-device` | config | I²C bus and address for a TMP117 |
28
+ | `tmp117` | input | Reads the temperature from a TMP117 ±0.1 °C high-accuracy digital temperature sensor over I²C, and optionally reports its THIGH/TLOW limit alerts. |
22
29
 
23
30
  ## Links
24
31
 
@@ -4,7 +4,7 @@
4
4
  "type": "tab",
5
5
  "label": "MCP9808 Freezer Monitor Demo",
6
6
  "disabled": false,
7
- "info": "An Inject node fires every 5 seconds, triggering the periph-mcp9808 node to read the temperature and Alert status, which a Function node formats into a log line. The node is configured with TCRIT at -5 °C — the \"too warm, door left open\" threshold for a freezer — with the Alert source set to TCRIT only and the Alert in latched interrupt mode. When the freezer warms past TCRIT the node emits a separate {source: \"critical\"} message; the Function node turns it into an alarm notification on the Debug sidebar and sends {clear_interrupt: true} back into the node to re-arm the latched Alert. To try it without a freezer, raise TCRIT just above room temperature and warm the sensor with a fingertip."
7
+ "info": "An Inject node fires every 5 seconds, triggering the mcp9808 node to read the temperature and Alert status, which a Function node formats into a log line. The node is configured with TCRIT at -5 °C — the \"too warm, door left open\" threshold for a freezer — with the Alert source set to TCRIT only and the Alert in latched interrupt mode. When the freezer warms past TCRIT the node emits a separate {source: \"critical\"} message; the Function node turns it into an alarm notification on the Debug sidebar and sends {clear_interrupt: true} back into the node to re-arm the latched Alert. To try it without a freezer, raise TCRIT just above room temperature and warm the sensor with a fingertip."
8
8
  },
9
9
  {
10
10
  "id": "inj-read",
@@ -27,19 +27,24 @@
27
27
  ]
28
28
  },
29
29
  {
30
- "id": "mcp9808-node",
31
- "type": "periph-mcp9808",
32
- "name": "Freezer MCP9808",
30
+ "id": "mcp9808-device-1",
31
+ "type": "mcp9808-device",
32
+ "name": "MCP9808",
33
33
  "bus": "1",
34
- "address": "0x18",
35
- "resolution": "0.25",
36
34
  "alertEnabled": true,
37
- "upperLimit": "-15",
38
- "lowerLimit": "-25",
39
- "criticalLimit": "-5",
40
- "hysteresis": "3",
41
- "alertMode": "critical_only",
35
+ "resolution": "0.25",
42
36
  "alertOutput": "interrupt",
37
+ "address": "0x18",
38
+ "alertMode": "critical_only",
39
+ "hysteresis": "3",
40
+ "criticalLimit": "-5",
41
+ "lowerLimit": "-25",
42
+ "upperLimit": "-15"
43
+ },
44
+ {
45
+ "id": "mcp9808-node",
46
+ "type": "mcp9808",
47
+ "name": "Freezer MCP9808",
43
48
  "z": "tab-mcp9808-demo",
44
49
  "x": 400,
45
50
  "y": 100,
@@ -47,7 +52,8 @@
47
52
  [
48
53
  "fn-alarm"
49
54
  ]
50
- ]
55
+ ],
56
+ "device": "mcp9808-device-1"
51
57
  },
52
58
  {
53
59
  "id": "fn-alarm",
@@ -4,7 +4,7 @@
4
4
  "type": "tab",
5
5
  "label": "TMP102 Temperature Demo",
6
6
  "disabled": false,
7
- "info": "An Inject node fires every second, triggering the periph-tmp102 node to read the temperature (12-bit, 0.0625 °C resolution, 4 Hz conversion rate). A Function node formats the reading as \"%.2f °C\" and a Debug node shows it in the sidebar. Change the conversion rate or the I²C address in the node's configuration panel (0x48-0x4B depending on the ADD0 strap)."
7
+ "info": "An Inject node fires every second, triggering the tmp102 node to read the temperature (12-bit, 0.0625 °C resolution, 4 Hz conversion rate). A Function node formats the reading as \"%.2f °C\" and a Debug node shows it in the sidebar. Change the conversion rate or the I²C address in the node's configuration panel (0x48-0x4B depending on the ADD0 strap)."
8
8
  },
9
9
  {
10
10
  "id": "inj-read",
@@ -20,19 +20,33 @@
20
20
  "z": "tab-tmp102-demo",
21
21
  "x": 160,
22
22
  "y": 100,
23
- "wires": [["tmp102-node"]]
23
+ "wires": [
24
+ [
25
+ "tmp102-node"
26
+ ]
27
+ ]
24
28
  },
25
29
  {
26
- "id": "tmp102-node",
27
- "type": "periph-tmp102",
30
+ "id": "tmp102-device-1",
31
+ "type": "tmp102-device",
28
32
  "name": "TMP102",
29
33
  "bus": "1",
30
34
  "address": "0x48",
31
- "conversionRate": "4",
35
+ "conversionRate": "4"
36
+ },
37
+ {
38
+ "id": "tmp102-node",
39
+ "type": "tmp102",
40
+ "name": "TMP102",
32
41
  "z": "tab-tmp102-demo",
33
42
  "x": 380,
34
43
  "y": 100,
35
- "wires": [["fn-format"]]
44
+ "wires": [
45
+ [
46
+ "fn-format"
47
+ ]
48
+ ],
49
+ "device": "tmp102-device-1"
36
50
  },
37
51
  {
38
52
  "id": "fn-format",
@@ -44,7 +58,11 @@
44
58
  "z": "tab-tmp102-demo",
45
59
  "x": 600,
46
60
  "y": 100,
47
- "wires": [["dbg-temp"]]
61
+ "wires": [
62
+ [
63
+ "dbg-temp"
64
+ ]
65
+ ]
48
66
  },
49
67
  {
50
68
  "id": "dbg-temp",
@@ -4,7 +4,7 @@
4
4
  "type": "tab",
5
5
  "label": "TMP117 Server Room Cooling Demo",
6
6
  "disabled": false,
7
- "info": "An Inject node fires every 5 seconds, triggering the periph-tmp117 node to read the temperature and its alert flags, which a Function node formats into a log line. The node is configured in Therm mode with THIGH at 30 °C — a server-room \"too hot\" threshold — and TLOW at 27 °C. When the room heats past THIGH the node emits a separate {source: \"high\"} message and the Function node switches the cooling fan on through the second output. In Therm mode the HIGH_Alert flag latches until the room cools below TLOW, so the periodic reading's high_alert field keeps the fan running and switches it off again automatically once the room is back below 27 °C. Replace the \"Cooling fan\" Debug node with a GPIO or relay node to drive a real fan. To try it without a server room, set THIGH just above room temperature and warm the sensor with a fingertip."
7
+ "info": "An Inject node fires every 5 seconds, triggering the tmp117 node to read the temperature and its alert flags, which a Function node formats into a log line. The node is configured in Therm mode with THIGH at 30 °C — a server-room \"too hot\" threshold — and TLOW at 27 °C. When the room heats past THIGH the node emits a separate {source: \"high\"} message and the Function node switches the cooling fan on through the second output. In Therm mode the HIGH_Alert flag latches until the room cools below TLOW, so the periodic reading's high_alert field keeps the fan running and switches it off again automatically once the room is back below 27 °C. Replace the \"Cooling fan\" Debug node with a GPIO or relay node to drive a real fan. To try it without a server room, set THIGH just above room temperature and warm the sensor with a fingertip."
8
8
  },
9
9
  {
10
10
  "id": "inj-read",
@@ -27,18 +27,25 @@
27
27
  ]
28
28
  },
29
29
  {
30
- "id": "tmp117-node",
31
- "type": "periph-tmp117",
32
- "name": "Server room TMP117",
30
+ "id": "tmp117-device-1",
31
+ "type": "tmp117-device",
32
+ "name": "TMP117",
33
33
  "bus": "1",
34
- "address": "0x48",
35
- "averaging": "8",
36
- "cycleSeconds": "1",
37
34
  "alertEnabled": true,
38
35
  "highLimit": "30",
39
- "lowLimit": "27",
36
+ "address": "0x48",
37
+ "cycleSeconds": "1",
38
+ "averaging": "8",
40
39
  "alertMode": "therm",
41
40
  "polarity": "active_low",
41
+ "lowLimit": "27"
42
+ },
43
+ {
44
+ "id": "tmp117-node",
45
+ "type": "tmp117",
46
+ "name": "Server room TMP117",
47
+ "averaging": "8",
48
+ "cycleSeconds": "1",
42
49
  "z": "tab-tmp117-demo",
43
50
  "x": 400,
44
51
  "y": 100,
@@ -46,7 +53,8 @@
46
53
  [
47
54
  "fn-fan"
48
55
  ]
49
- ]
56
+ ],
57
+ "device": "tmp117-device-1"
50
58
  },
51
59
  {
52
60
  "id": "fn-fan",
@@ -1,13 +1,12 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-mcp9808', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('mcp9808-device', {
4
+ category: 'config',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  bus: { value: '1', required: true, validate: RED.validators.number() },
8
8
  address: { value: '0x18' },
9
9
  resolution: { value: '0.0625' },
10
-
11
10
  alertEnabled: { value: false },
12
11
  upperLimit: { value: '30', validate: RED.validators.number() },
13
12
  lowerLimit: { value: '10', validate: RED.validators.number() },
@@ -16,26 +15,24 @@
16
15
  alertMode: { value: 'all' },
17
16
  alertOutput: { value: 'comparator' }
18
17
  },
19
- inputs: 1,
20
- outputs: 1,
21
- icon: 'font-awesome/fa-thermometer-half',
22
- label: function() { return this.name || 'MCP9808'; },
23
- paletteLabel: 'MCP9808'
18
+ label: function() {
19
+ return this.name || ('MCP9808 @ ' + this.address + ' (bus ' + this.bus + ')');
20
+ }
24
21
  });
25
22
  </script>
26
23
 
27
- <script type="text/html" data-template-name="periph-mcp9808">
24
+ <script type="text/html" data-template-name="mcp9808-device">
28
25
  <div class="form-row">
29
- <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
30
- <input type="text" id="node-input-name" placeholder="MCP9808">
26
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
27
+ <input type="text" id="node-config-input-name" placeholder="MCP9808">
31
28
  </div>
32
29
  <div class="form-row">
33
- <label for="node-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
34
- <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
30
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
31
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
35
32
  </div>
36
33
  <div class="form-row">
37
- <label for="node-input-address"><i class="fa fa-map-marker"></i> I²C address</label>
38
- <select id="node-input-address" style="width:120px">
34
+ <label for="node-config-input-address"><i class="fa fa-map-marker"></i> I²C address</label>
35
+ <select id="node-config-input-address" style="width:120px">
39
36
  <option value="0x18">0x18</option>
40
37
  <option value="0x19">0x19</option>
41
38
  <option value="0x1A">0x1A</option>
@@ -47,35 +44,33 @@
47
44
  </select>
48
45
  </div>
49
46
  <div class="form-row">
50
- <label for="node-input-resolution"><i class="fa fa-sliders"></i> Resolution</label>
51
- <select id="node-input-resolution" style="width:200px">
47
+ <label for="node-config-input-resolution"><i class="fa fa-sliders"></i> Resolution</label>
48
+ <select id="node-config-input-resolution" style="width:200px">
52
49
  <option value="0.5">0.5 °C (30 ms)</option>
53
50
  <option value="0.25">0.25 °C (65 ms)</option>
54
51
  <option value="0.125">0.125 °C (130 ms)</option>
55
52
  <option value="0.0625">0.0625 °C (250 ms)</option>
56
53
  </select>
57
54
  </div>
58
-
59
- <hr>
60
55
  <div class="form-row">
61
- <label for="node-input-alertEnabled"><i class="fa fa-bell"></i> Alert</label>
62
- <input type="checkbox" id="node-input-alertEnabled" style="width:auto"> enabled (emits a <code>source</code> message when a boundary trips)
56
+ <label for="node-config-input-alertEnabled"><i class="fa fa-bell"></i> Alert</label>
57
+ <input type="checkbox" id="node-config-input-alertEnabled" style="width:auto"> enabled (emits a <code>source</code> message when a boundary trips)
63
58
  </div>
64
59
  <div class="form-row">
65
- <label for="node-input-upperLimit">&nbsp; TUPPER °C</label>
66
- <input type="text" id="node-input-upperLimit" style="width:80px">
60
+ <label for="node-config-input-upperLimit">&nbsp; TUPPER °C</label>
61
+ <input type="text" id="node-config-input-upperLimit" style="width:80px">
67
62
  </div>
68
63
  <div class="form-row">
69
- <label for="node-input-lowerLimit">&nbsp; TLOWER °C</label>
70
- <input type="text" id="node-input-lowerLimit" style="width:80px">
64
+ <label for="node-config-input-lowerLimit">&nbsp; TLOWER °C</label>
65
+ <input type="text" id="node-config-input-lowerLimit" style="width:80px">
71
66
  </div>
72
67
  <div class="form-row">
73
- <label for="node-input-criticalLimit">&nbsp; TCRIT °C</label>
74
- <input type="text" id="node-input-criticalLimit" style="width:80px">
68
+ <label for="node-config-input-criticalLimit">&nbsp; TCRIT °C</label>
69
+ <input type="text" id="node-config-input-criticalLimit" style="width:80px">
75
70
  </div>
76
71
  <div class="form-row">
77
- <label for="node-input-hysteresis">&nbsp; Hysteresis</label>
78
- <select id="node-input-hysteresis" style="width:120px">
72
+ <label for="node-config-input-hysteresis">&nbsp; Hysteresis</label>
73
+ <select id="node-config-input-hysteresis" style="width:120px">
79
74
  <option value="0">0 °C</option>
80
75
  <option value="1.5">1.5 °C</option>
81
76
  <option value="3">3.0 °C</option>
@@ -83,22 +78,50 @@
83
78
  </select>
84
79
  </div>
85
80
  <div class="form-row">
86
- <label for="node-input-alertMode">&nbsp; Source</label>
87
- <select id="node-input-alertMode" style="width:200px">
81
+ <label for="node-config-input-alertMode">&nbsp; Source</label>
82
+ <select id="node-config-input-alertMode" style="width:200px">
88
83
  <option value="all">TUPPER / TLOWER / TCRIT</option>
89
84
  <option value="critical_only">TCRIT only</option>
90
85
  </select>
91
86
  </div>
92
87
  <div class="form-row">
93
- <label for="node-input-alertOutput">&nbsp; Mode</label>
94
- <select id="node-input-alertOutput" style="width:200px">
88
+ <label for="node-config-input-alertOutput">&nbsp; Mode</label>
89
+ <select id="node-config-input-alertOutput" style="width:200px">
95
90
  <option value="comparator">Comparator</option>
96
91
  <option value="interrupt">Interrupt (latched)</option>
97
92
  </select>
98
93
  </div>
99
94
  </script>
100
95
 
101
- <script type="text/html" data-help-name="periph-mcp9808">
96
+ <!-- Read node -->
97
+ <script type="text/javascript">
98
+ RED.nodes.registerType('mcp9808', {
99
+ category: 'Peripheral',
100
+ color: '#C084FC',
101
+ defaults: {
102
+ name: { value: '' },
103
+ device: { value: '', type: 'mcp9808-device', required: true }
104
+ },
105
+ inputs: 1,
106
+ outputs: 1,
107
+ icon: 'font-awesome/fa-thermometer-half',
108
+ label: function() { return this.name || 'MCP9808'; },
109
+ paletteLabel: 'MCP9808'
110
+ });
111
+ </script>
112
+
113
+ <script type="text/html" data-template-name="mcp9808">
114
+ <div class="form-row">
115
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
116
+ <input type="text" id="node-input-name" placeholder="MCP9808">
117
+ </div>
118
+ <div class="form-row">
119
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
120
+ <input type="text" id="node-input-device">
121
+ </div>
122
+ </script>
123
+
124
+ <script type="text/html" data-help-name="mcp9808">
102
125
  <p>Reads the ambient temperature from an MCP9808 ±0.5 °C digital temperature sensor over I²C, and optionally reports its TUPPER/TLOWER/TCRIT boundary alerts.</p>
103
126
 
104
127
  <h3>Inputs</h3>
@@ -10,10 +10,9 @@ module.exports = function(RED) {
10
10
  [MCP9808Full.SOURCE_CRITICAL, 'critical'],
11
11
  ];
12
12
 
13
- function MCP9808Node(config) {
13
+ function MCP9808DeviceNode(config) {
14
14
  RED.nodes.createNode(this, config);
15
15
  const node = this;
16
-
17
16
  try {
18
17
  const address = parseInt(config.address, 16) || MCP9808Full.I2C_ADDRESS;
19
18
  const connection = new I2CConnection(parseInt(config.bus, 10), address);
@@ -33,7 +32,7 @@ module.exports = function(RED) {
33
32
  await node.driver.enableAlert();
34
33
  await node.driver.onInterrupt((status) => {
35
34
  for (const [bit, name] of SOURCE_NAMES) {
36
- if (status & bit) node.send({ payload: { source: name } });
35
+ if (status & bit) node.emit('periph-data', { payload: { source: name } });
37
36
  }
38
37
  });
39
38
  }
@@ -42,18 +41,41 @@ module.exports = function(RED) {
42
41
  node.error('MCP9808 init failed: ' + e.message);
43
42
  }
44
43
 
44
+ node.on('close', async function(removed, done) {
45
+ try {
46
+ if (node.driver) await node.driver.offInterrupt();
47
+ if (node.connection) await node.connection.close();
48
+ } catch (e) {
49
+ // bus may already be gone; nothing more to do on close
50
+ } finally {
51
+ done();
52
+ }
53
+ });
54
+ }
55
+ RED.nodes.registerType('mcp9808-device', MCP9808DeviceNode);
56
+
57
+ function MCP9808ReadNode(config) {
58
+ RED.nodes.createNode(this, config);
59
+ const node = this;
60
+ const deviceNode = RED.nodes.getNode(config.device) || {};
61
+ if (deviceNode.on) {
62
+ const onData = (m) => node.send(m);
63
+ deviceNode.on('periph-data', onData);
64
+ node.on('close', () => deviceNode.removeListener('periph-data', onData));
65
+ }
66
+
45
67
  node.on('input', async function(msg, send, done) {
46
- if (!node.driver) { done(); return; }
68
+ if (!deviceNode.driver) { done(); return; }
47
69
  try {
48
70
  const p = msg.payload;
49
71
  if (p && typeof p === 'object') {
50
- if (p.shutdown === true) { await node.driver.shutdown(); done(); return; }
51
- if (p.shutdown === false) { await node.driver.wake(); done(); return; }
52
- if (p.clear_interrupt === true) { await node.driver.clearInterrupt(); done(); return; }
72
+ if (p.shutdown === true) { await deviceNode.driver.shutdown(); done(); return; }
73
+ if (p.shutdown === false) { await deviceNode.driver.wake(); done(); return; }
74
+ if (p.clear_interrupt === true) { await deviceNode.driver.clearInterrupt(); done(); return; }
53
75
  }
54
76
  msg.payload = {
55
- temperature_c: await node.driver.readTemperature(),
56
- alert_asserted: await node.driver.isAlertAsserted(),
77
+ temperature_c: await deviceNode.driver.readTemperature(),
78
+ alert_asserted: await deviceNode.driver.isAlertAsserted(),
57
79
  };
58
80
  send(msg);
59
81
  done();
@@ -61,17 +83,6 @@ module.exports = function(RED) {
61
83
  done(e);
62
84
  }
63
85
  });
64
-
65
- node.on('close', async function(removed, done) {
66
- try {
67
- if (node.driver) await node.driver.offInterrupt();
68
- if (node.connection) await node.connection.close();
69
- } catch (e) {
70
- // bus may already be gone; nothing more to do on close
71
- } finally {
72
- done();
73
- }
74
- });
75
86
  }
76
- RED.nodes.registerType('periph-mcp9808', MCP9808Node);
87
+ RED.nodes.registerType('mcp9808', MCP9808ReadNode);
77
88
  };
@@ -1,33 +1,31 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-tmp102', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('tmp102-device', {
4
+ category: 'config',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  bus: { value: '1', required: true, validate: RED.validators.number() },
8
8
  address: { value: '0x48' },
9
9
  conversionRate: { value: '4' }
10
10
  },
11
- inputs: 1,
12
- outputs: 1,
13
- icon: 'font-awesome/fa-thermometer-half',
14
- label: function() { return this.name || 'TMP102'; },
15
- paletteLabel: 'TMP102'
11
+ label: function() {
12
+ return this.name || ('TMP102 @ ' + this.address + ' (bus ' + this.bus + ')');
13
+ }
16
14
  });
17
15
  </script>
18
16
 
19
- <script type="text/html" data-template-name="periph-tmp102">
17
+ <script type="text/html" data-template-name="tmp102-device">
20
18
  <div class="form-row">
21
- <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
22
- <input type="text" id="node-input-name" placeholder="TMP102">
19
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
20
+ <input type="text" id="node-config-input-name" placeholder="TMP102">
23
21
  </div>
24
22
  <div class="form-row">
25
- <label for="node-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
26
- <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
23
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
24
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
27
25
  </div>
28
26
  <div class="form-row">
29
- <label for="node-input-address"><i class="fa fa-map-marker"></i> I²C address</label>
30
- <select id="node-input-address" style="width:200px">
27
+ <label for="node-config-input-address"><i class="fa fa-map-marker"></i> I²C address</label>
28
+ <select id="node-config-input-address" style="width:200px">
31
29
  <option value="0x48">0x48 (ADD0 = GND)</option>
32
30
  <option value="0x49">0x49 (ADD0 = V+)</option>
33
31
  <option value="0x4A">0x4A (ADD0 = SDA)</option>
@@ -35,8 +33,8 @@
35
33
  </select>
36
34
  </div>
37
35
  <div class="form-row">
38
- <label for="node-input-conversionRate"><i class="fa fa-clock-o"></i> Conversion rate</label>
39
- <select id="node-input-conversionRate" style="width:200px">
36
+ <label for="node-config-input-conversionRate"><i class="fa fa-clock-o"></i> Conversion rate</label>
37
+ <select id="node-config-input-conversionRate" style="width:200px">
40
38
  <option value="0.25">0.25 Hz</option>
41
39
  <option value="1">1 Hz</option>
42
40
  <option value="4">4 Hz</option>
@@ -45,7 +43,35 @@
45
43
  </div>
46
44
  </script>
47
45
 
48
- <script type="text/html" data-help-name="periph-tmp102">
46
+ <!-- Read node -->
47
+ <script type="text/javascript">
48
+ RED.nodes.registerType('tmp102', {
49
+ category: 'Peripheral',
50
+ color: '#C084FC',
51
+ defaults: {
52
+ name: { value: '' },
53
+ device: { value: '', type: 'tmp102-device', required: true }
54
+ },
55
+ inputs: 1,
56
+ outputs: 1,
57
+ icon: 'font-awesome/fa-thermometer-half',
58
+ label: function() { return this.name || 'TMP102'; },
59
+ paletteLabel: 'TMP102'
60
+ });
61
+ </script>
62
+
63
+ <script type="text/html" data-template-name="tmp102">
64
+ <div class="form-row">
65
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
66
+ <input type="text" id="node-input-name" placeholder="TMP102">
67
+ </div>
68
+ <div class="form-row">
69
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
70
+ <input type="text" id="node-input-device">
71
+ </div>
72
+ </script>
73
+
74
+ <script type="text/html" data-help-name="tmp102">
49
75
  <p>Reads the temperature from a TMP102 12-bit digital temperature sensor over I²C.</p>
50
76
 
51
77
  <h3>Inputs</h3>
@@ -4,10 +4,9 @@ module.exports = function(RED) {
4
4
  const { I2CConnection } = require('periph/src/connection/i2c');
5
5
  const { TMP102Full } = require('periph/src/chips/temperature/tmp102');
6
6
 
7
- function TMP102Node(config) {
7
+ function TMP102DeviceNode(config) {
8
8
  RED.nodes.createNode(this, config);
9
9
  const node = this;
10
-
11
10
  try {
12
11
  const address = parseInt(config.address, 16) || TMP102Full.I2C_ADDRESS;
13
12
  const connection = new I2CConnection(parseInt(config.bus, 10), address);
@@ -21,17 +20,6 @@ module.exports = function(RED) {
21
20
  node.error('TMP102 init failed: ' + e.message);
22
21
  }
23
22
 
24
- node.on('input', async function(msg, send, done) {
25
- if (!node.driver) { done(); return; }
26
- try {
27
- msg.payload = { temperature: await node.driver.readTemperature() };
28
- send(msg);
29
- done();
30
- } catch (e) {
31
- done(e);
32
- }
33
- });
34
-
35
23
  node.on('close', async function(removed, done) {
36
24
  try {
37
25
  if (node.connection) await node.connection.close();
@@ -42,5 +30,23 @@ module.exports = function(RED) {
42
30
  }
43
31
  });
44
32
  }
45
- RED.nodes.registerType('periph-tmp102', TMP102Node);
33
+ RED.nodes.registerType('tmp102-device', TMP102DeviceNode);
34
+
35
+ function TMP102ReadNode(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) { done(); return; }
42
+ try {
43
+ msg.payload = { temperature: await deviceNode.driver.readTemperature() };
44
+ send(msg);
45
+ done();
46
+ } catch (e) {
47
+ done(e);
48
+ }
49
+ });
50
+ }
51
+ RED.nodes.registerType('tmp102', TMP102ReadNode);
46
52
  };
@@ -1,40 +1,37 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-tmp117', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('tmp117-device', {
4
+ category: 'config',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  bus: { value: '1', required: true, validate: RED.validators.number() },
8
8
  address: { value: '0x48' },
9
9
  averaging: { value: '8' },
10
10
  cycleSeconds: { value: '1' },
11
-
12
11
  alertEnabled: { value: false },
13
12
  highLimit: { value: '30', validate: RED.validators.number() },
14
13
  lowLimit: { value: '10', validate: RED.validators.number() },
15
14
  alertMode: { value: 'alert' },
16
15
  polarity: { value: 'active_low' }
17
16
  },
18
- inputs: 1,
19
- outputs: 1,
20
- icon: 'font-awesome/fa-thermometer-half',
21
- label: function() { return this.name || 'TMP117'; },
22
- paletteLabel: 'TMP117'
17
+ label: function() {
18
+ return this.name || ('TMP117 @ ' + this.address + ' (bus ' + this.bus + ')');
19
+ }
23
20
  });
24
21
  </script>
25
22
 
26
- <script type="text/html" data-template-name="periph-tmp117">
23
+ <script type="text/html" data-template-name="tmp117-device">
27
24
  <div class="form-row">
28
- <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
29
- <input type="text" id="node-input-name" placeholder="TMP117">
25
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
26
+ <input type="text" id="node-config-input-name" placeholder="TMP117">
30
27
  </div>
31
28
  <div class="form-row">
32
- <label for="node-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
33
- <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
29
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
30
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
34
31
  </div>
35
32
  <div class="form-row">
36
- <label for="node-input-address"><i class="fa fa-map-marker"></i> I²C address</label>
37
- <select id="node-input-address" style="width:200px">
33
+ <label for="node-config-input-address"><i class="fa fa-map-marker"></i> I²C address</label>
34
+ <select id="node-config-input-address" style="width:200px">
38
35
  <option value="0x48">0x48 (ADD0 = GND)</option>
39
36
  <option value="0x49">0x49 (ADD0 = V+)</option>
40
37
  <option value="0x4A">0x4A (ADD0 = SDA)</option>
@@ -42,8 +39,8 @@
42
39
  </select>
43
40
  </div>
44
41
  <div class="form-row">
45
- <label for="node-input-averaging"><i class="fa fa-sliders"></i> Averaging</label>
46
- <select id="node-input-averaging" style="width:200px">
42
+ <label for="node-config-input-averaging"><i class="fa fa-sliders"></i> Averaging</label>
43
+ <select id="node-config-input-averaging" style="width:200px">
47
44
  <option value="0">none</option>
48
45
  <option value="8">8 conversions</option>
49
46
  <option value="32">32 conversions</option>
@@ -51,8 +48,8 @@
51
48
  </select>
52
49
  </div>
53
50
  <div class="form-row">
54
- <label for="node-input-cycleSeconds"><i class="fa fa-clock-o"></i> Cycle time</label>
55
- <select id="node-input-cycleSeconds" style="width:200px">
51
+ <label for="node-config-input-cycleSeconds"><i class="fa fa-clock-o"></i> Cycle time</label>
52
+ <select id="node-config-input-cycleSeconds" style="width:200px">
56
53
  <option value="0.0155">15.5 ms</option>
57
54
  <option value="0.125">125 ms</option>
58
55
  <option value="0.25">250 ms</option>
@@ -63,37 +60,87 @@
63
60
  <option value="16">16 s</option>
64
61
  </select>
65
62
  </div>
66
-
67
- <hr>
68
63
  <div class="form-row">
69
- <label for="node-input-alertEnabled"><i class="fa fa-bell"></i> Alert</label>
70
- <input type="checkbox" id="node-input-alertEnabled" style="width:auto"> enabled (emits a <code>source</code> message when a limit trips)
64
+ <label for="node-config-input-alertEnabled"><i class="fa fa-bell"></i> Alert</label>
65
+ <input type="checkbox" id="node-config-input-alertEnabled" style="width:auto"> enabled (emits a <code>source</code> message when a limit trips)
71
66
  </div>
72
67
  <div class="form-row">
73
- <label for="node-input-highLimit">&nbsp; THIGH °C</label>
74
- <input type="text" id="node-input-highLimit" style="width:80px">
68
+ <label for="node-config-input-highLimit">&nbsp; THIGH °C</label>
69
+ <input type="text" id="node-config-input-highLimit" style="width:80px">
75
70
  </div>
76
71
  <div class="form-row">
77
- <label for="node-input-lowLimit">&nbsp; TLOW °C</label>
78
- <input type="text" id="node-input-lowLimit" style="width:80px">
72
+ <label for="node-config-input-lowLimit">&nbsp; TLOW °C</label>
73
+ <input type="text" id="node-config-input-lowLimit" style="width:80px">
79
74
  </div>
80
75
  <div class="form-row">
81
- <label for="node-input-alertMode">&nbsp; Mode</label>
82
- <select id="node-input-alertMode" style="width:200px">
76
+ <label for="node-config-input-alertMode">&nbsp; Mode</label>
77
+ <select id="node-config-input-alertMode" style="width:200px">
83
78
  <option value="alert">Alert (window)</option>
84
79
  <option value="therm">Therm (latching, TLOW resets)</option>
85
80
  </select>
86
81
  </div>
87
82
  <div class="form-row">
88
- <label for="node-input-polarity">&nbsp; Polarity</label>
89
- <select id="node-input-polarity" style="width:200px">
83
+ <label for="node-config-input-polarity">&nbsp; Polarity</label>
84
+ <select id="node-config-input-polarity" style="width:200px">
90
85
  <option value="active_low">Active low</option>
91
86
  <option value="active_high">Active high</option>
92
87
  </select>
93
88
  </div>
94
89
  </script>
95
90
 
96
- <script type="text/html" data-help-name="periph-tmp117">
91
+ <!-- Read node -->
92
+ <script type="text/javascript">
93
+ RED.nodes.registerType('tmp117', {
94
+ category: 'Peripheral',
95
+ color: '#C084FC',
96
+ defaults: {
97
+ name: { value: '' },
98
+ device: { value: '', type: 'tmp117-device', required: true },
99
+ averaging: { value: '8' },
100
+ cycleSeconds: { value: '1' }
101
+ },
102
+ inputs: 1,
103
+ outputs: 1,
104
+ icon: 'font-awesome/fa-thermometer-half',
105
+ label: function() { return this.name || 'TMP117'; },
106
+ paletteLabel: 'TMP117'
107
+ });
108
+ </script>
109
+
110
+ <script type="text/html" data-template-name="tmp117">
111
+ <div class="form-row">
112
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
113
+ <input type="text" id="node-input-name" placeholder="TMP117">
114
+ </div>
115
+ <div class="form-row">
116
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
117
+ <input type="text" id="node-input-device">
118
+ </div>
119
+ <div class="form-row">
120
+ <label for="node-input-averaging"><i class="fa fa-sliders"></i> Averaging</label>
121
+ <select id="node-input-averaging" style="width:200px">
122
+ <option value="0">none</option>
123
+ <option value="8">8 conversions</option>
124
+ <option value="32">32 conversions</option>
125
+ <option value="64">64 conversions</option>
126
+ </select>
127
+ </div>
128
+ <div class="form-row">
129
+ <label for="node-input-cycleSeconds"><i class="fa fa-clock-o"></i> Cycle time</label>
130
+ <select id="node-input-cycleSeconds" style="width:200px">
131
+ <option value="0.0155">15.5 ms</option>
132
+ <option value="0.125">125 ms</option>
133
+ <option value="0.25">250 ms</option>
134
+ <option value="0.5">500 ms</option>
135
+ <option value="1">1 s</option>
136
+ <option value="4">4 s</option>
137
+ <option value="8">8 s</option>
138
+ <option value="16">16 s</option>
139
+ </select>
140
+ </div>
141
+ </script>
142
+
143
+ <script type="text/html" data-help-name="tmp117">
97
144
  <p>Reads the temperature from a TMP117 ±0.1 °C high-accuracy digital temperature sensor over I²C, and optionally reports its THIGH/TLOW limit alerts.</p>
98
145
 
99
146
  <h3>Inputs</h3>
@@ -9,7 +9,7 @@ module.exports = function(RED) {
9
9
  [TMP117Full.SOURCE_LOW, 'low'],
10
10
  ];
11
11
 
12
- function TMP117Node(config) {
12
+ function TMP117DeviceNode(config) {
13
13
  RED.nodes.createNode(this, config);
14
14
  const node = this;
15
15
  const averaging = parseInt(config.averaging, 10);
@@ -31,7 +31,7 @@ module.exports = function(RED) {
31
31
  config.polarity || 'active_low', 'alert');
32
32
  await node.driver.onInterrupt((status) => {
33
33
  for (const [bit, name] of SOURCE_NAMES) {
34
- if (status & bit) node.send({ payload: { source: name } });
34
+ if (status & bit) node.emit('periph-data', { payload: { source: name } });
35
35
  }
36
36
  });
37
37
  }
@@ -40,17 +40,43 @@ module.exports = function(RED) {
40
40
  node.error('TMP117 init failed: ' + e.message);
41
41
  }
42
42
 
43
+ node.on('close', async function(removed, done) {
44
+ try {
45
+ if (node.driver) await node.driver.offInterrupt();
46
+ if (node.connection) await node.connection.close();
47
+ } catch (e) {
48
+ // bus may already be gone; nothing more to do on close
49
+ } finally {
50
+ done();
51
+ }
52
+ });
53
+ }
54
+ RED.nodes.registerType('tmp117-device', TMP117DeviceNode);
55
+
56
+ function TMP117ReadNode(config) {
57
+ RED.nodes.createNode(this, config);
58
+ const node = this;
59
+ const deviceNode = RED.nodes.getNode(config.device) || {};
60
+ if (deviceNode.on) {
61
+ const onData = (m) => node.send(m);
62
+ deviceNode.on('periph-data', onData);
63
+ node.on('close', () => deviceNode.removeListener('periph-data', onData));
64
+ }
65
+
66
+ const averaging = parseInt(config.averaging, 10);
67
+ const cycleSeconds = parseFloat(config.cycleSeconds) || 1.0;
68
+
43
69
  node.on('input', async function(msg, send, done) {
44
- if (!node.driver) { done(); return; }
70
+ if (!deviceNode.driver) { done(); return; }
45
71
  try {
46
72
  const p = msg.payload;
47
73
  if (p && typeof p === 'object' && typeof p.mode === 'string') {
48
- await node.driver.configure(p.mode, isNaN(averaging) ? 8 : averaging, cycleSeconds);
74
+ await deviceNode.driver.configure(p.mode, isNaN(averaging) ? 8 : averaging, cycleSeconds);
49
75
  done();
50
76
  return;
51
77
  }
52
- const temperature = await node.driver.readTemperature();
53
- const status = await node.driver.pollInterrupt();
78
+ const temperature = await deviceNode.driver.readTemperature();
79
+ const status = await deviceNode.driver.pollInterrupt();
54
80
  msg.payload = {
55
81
  temperature_c: temperature,
56
82
  high_alert: (status & TMP117Full.SOURCE_HIGH) !== 0,
@@ -62,17 +88,6 @@ module.exports = function(RED) {
62
88
  done(e);
63
89
  }
64
90
  });
65
-
66
- node.on('close', async function(removed, done) {
67
- try {
68
- if (node.driver) await node.driver.offInterrupt();
69
- if (node.connection) await node.connection.close();
70
- } catch (e) {
71
- // bus may already be gone; nothing more to do on close
72
- } finally {
73
- done();
74
- }
75
- });
76
91
  }
77
- RED.nodes.registerType('periph-tmp117', TMP117Node);
92
+ RED.nodes.registerType('tmp117', TMP117ReadNode);
78
93
  };
package/package.json CHANGED
@@ -1,16 +1,21 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-temperature",
3
- "version": "1.5.1",
4
- "description": "Node-RED nodes for temperature sensor chips",
3
+ "version": "1.5.3",
4
+ "description": "Node-RED nodes for temperature sensor chips: MCP9808, TMP102, TMP117",
5
5
  "keywords": [
6
6
  "node-red",
7
7
  "temperature",
8
8
  "i2c",
9
9
  "spi",
10
10
  "sensor",
11
- "peripheral"
11
+ "peripheral",
12
+ "node-red-contrib",
13
+ "mcp9808",
14
+ "tmp102",
15
+ "tmp117"
12
16
  ],
13
17
  "node-red": {
18
+ "version": ">=3.0.0",
14
19
  "nodes": {
15
20
  "mcp9808": "nodes/mcp9808/mcp9808.js",
16
21
  "tmp102": "nodes/tmp102/tmp102.js",
package/index.js DELETED
@@ -1,11 +0,0 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
-
4
- module.exports = function(RED) {
5
- const nodesDir = path.join(__dirname, 'nodes');
6
- if (!fs.existsSync(nodesDir)) return;
7
- fs.readdirSync(nodesDir).forEach(function(chip) {
8
- const nodeFile = path.join(nodesDir, chip, chip + '.js');
9
- if (fs.existsSync(nodeFile)) require(nodeFile)(RED);
10
- });
11
- };