node-red-contrib-periph-magnetometer 1.1.0 → 1.2.0

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
@@ -16,8 +16,10 @@ npm install node-red-contrib-periph-magnetometer
16
16
 
17
17
  | Node | Kind | Description |
18
18
  |------|------|-------------|
19
- | `as5600-device` | config | I²C bus for an AS5600 |
20
- | `periph-as5600` | input | Read angle from an AS5600 magnetic rotary position sensor |
19
+ | `as5600-device` | config | I²C bus and address for an AS5600 |
20
+ | `periph-as5600` | input | Reads the absolute angle, raw count, and magnet detection status from an AS5600 contactless rotary position sensor over I²C. |
21
+ | `hmc5883l-device` | config | I²C bus and address for a HMC5883L |
22
+ | `periph-hmc5883l` | input | Reads the magnetic field on three axes from an HMC5883L 3-axis magnetometer over I²C. |
21
23
 
22
24
  ## Links
23
25
 
@@ -0,0 +1,74 @@
1
+ [
2
+ {
3
+ "id": "tab-hmc5883l-demo",
4
+ "type": "tab",
5
+ "label": "HMC5883L 3-Axis Magnetometer",
6
+ "disabled": false,
7
+ "info": "Electronic compass demo: reads magnetic field from HMC5883L every 200 ms, computes heading from X/Y axes using atan2(y, x), and displays as a gauge (0–360°). A debug node shows raw X/Y/Z values in µT. Vertical mount detection: if |Z| > 30 µT, a warning appears indicating tilt compensation is needed. The HMC5883L has a fixed I²C address of 0x1E. Configure averaging=8, ODR=15 Hz, gain=±1.3 Ga for Earth field measurements."
8
+ },
9
+ {
10
+ "id": "cfg-hmc5883l",
11
+ "type": "hmc5883l-device",
12
+ "name": "HMC5883L",
13
+ "bus": "1",
14
+ "address": "1E"
15
+ },
16
+ {
17
+ "id": "inj-tick",
18
+ "type": "inject",
19
+ "name": "every 200 ms",
20
+ "repeat": "0.2",
21
+ "crontab": "",
22
+ "once": true,
23
+ "onceDelay": 0.2,
24
+ "topic": "",
25
+ "payload": "",
26
+ "payloadType": "date",
27
+ "z": "tab-hmc5883l-demo",
28
+ "x": 120,
29
+ "y": 160,
30
+ "wires": [["read-hmc5883l"]]
31
+ },
32
+ {
33
+ "id": "read-hmc5883l",
34
+ "type": "periph-hmc5883l",
35
+ "name": "HMC5883L",
36
+ "device": "cfg-hmc5883l",
37
+ "z": "tab-hmc5883l-demo",
38
+ "x": 300,
39
+ "y": 160,
40
+ "wires": [["calc-heading"]]
41
+ },
42
+ {
43
+ "id": "calc-heading",
44
+ "type": "function",
45
+ "name": "heading & tilt check",
46
+ "func": "const { x, y, z } = msg.payload;\nlet heading = null;\nif (x !== null && y !== null) {\n heading = Math.atan2(y, x) * 180 / Math.PI;\n if (heading < 0) heading += 360;\n}\nconst tiltWarn = z !== null && Math.abs(z) > 30e-6;\nmsg.payload = {\n heading: heading !== null ? heading.toFixed(1) : 'N/A',\n x_uT: x !== null ? (x * 1e6).toFixed(1) : 'overflow',\n y_uT: y !== null ? (y * 1e6).toFixed(1) : 'overflow',\n z_uT: z !== null ? (z * 1e6).toFixed(1) : 'overflow',\n tilt_warning: tiltWarn\n};\nreturn msg;",
47
+ "outputs": 1,
48
+ "noerr": 0,
49
+ "initialize": "",
50
+ "finalize": "",
51
+ "libs": [],
52
+ "z": "tab-hmc5883l-demo",
53
+ "x": 480,
54
+ "y": 160,
55
+ "wires": [["dbg-readings"]]
56
+ },
57
+ {
58
+ "id": "dbg-readings",
59
+ "type": "debug",
60
+ "name": "",
61
+ "active": true,
62
+ "tosidebar": true,
63
+ "console": false,
64
+ "tostatus": true,
65
+ "complete": "payload",
66
+ "targetType": "msg",
67
+ "statusVal": "payload.heading",
68
+ "statusType": "auto",
69
+ "z": "tab-hmc5883l-demo",
70
+ "x": 650,
71
+ "y": 160,
72
+ "wires": []
73
+ }
74
+ ]
@@ -37,7 +37,7 @@
37
37
  <script type="text/javascript">
38
38
  RED.nodes.registerType('periph-as5600', {
39
39
  category: 'Peripheral',
40
- color: '#a6bbcf',
40
+ color: '#C084FC',
41
41
  defaults: {
42
42
  name: { value: '' },
43
43
  device: { value: '', type: 'as5600-device', required: true }
@@ -1,21 +1,21 @@
1
1
  'use strict';
2
2
 
3
3
  module.exports = function(RED) {
4
- const { I2CTransport } = require('periph/src/transport/i2c');
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
5
  const { AS5600Full } = require('periph/src/chips/magnetometer/as5600');
6
6
 
7
7
  function AS5600DeviceNode(config) {
8
8
  RED.nodes.createNode(this, config);
9
9
  const node = this;
10
10
  try {
11
- const transport = new I2CTransport(parseInt(config.bus), parseInt(config.address, 16));
12
- node.driver = new AS5600Full(transport);
13
- node.transport = transport;
11
+ const connection = new I2CConnection(parseInt(config.bus), parseInt(config.address, 16));
12
+ node.driver = new AS5600Full(connection);
13
+ node.connection = connection;
14
14
  } catch (e) {
15
15
  node.error('AS5600 init failed: ' + e.message);
16
16
  }
17
17
  node.on('close', function() {
18
- if (node.transport) node.transport.close();
18
+ if (node.connection) node.connection.close();
19
19
  });
20
20
  }
21
21
  RED.nodes.registerType('as5600-device', AS5600DeviceNode);
@@ -25,7 +25,7 @@ module.exports = function(RED) {
25
25
  const node = this;
26
26
  node.device = RED.nodes.getNode(config.device);
27
27
 
28
- node.on('input', function(msg, send, done) {
28
+ node.on('input', async function(msg, send, done) {
29
29
  if (!node.device || !node.device.driver) {
30
30
  node.error('No AS5600 device configured', msg);
31
31
  done();
@@ -34,9 +34,9 @@ module.exports = function(RED) {
34
34
  try {
35
35
  const d = node.device.driver;
36
36
  msg.payload = {
37
- angle: d.angle(),
38
- raw: d.angleRaw(),
39
- magnet_detected: d.isMagnetDetected()
37
+ angle: await d.angle(),
38
+ raw: await d.angleRaw(),
39
+ magnet_detected: await d.isMagnetDetected()
40
40
  };
41
41
  send(msg);
42
42
  done();
@@ -0,0 +1,77 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('hmc5883l-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '1E', required: true }
9
+ },
10
+ label: function() {
11
+ return this.name || ('HMC5883L @ 0x' + this.address + ' (bus ' + this.bus + ')');
12
+ }
13
+ });
14
+ </script>
15
+
16
+ <script type="text/html" data-template-name="hmc5883l-device">
17
+ <div class="form-row">
18
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
19
+ <input type="text" id="node-config-input-name" placeholder="HMC5883L">
20
+ </div>
21
+ <div class="form-row">
22
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
23
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
24
+ </div>
25
+ <div class="form-row">
26
+ <label for="node-config-input-address">Address (hex)</label>
27
+ <input type="text" id="node-config-input-address" placeholder="1E" style="width:80px"> &nbsp;fixed: 1E (0x1E)
28
+ </div>
29
+ </script>
30
+
31
+ <!-- Read node: trigger → measurements -->
32
+ <script type="text/javascript">
33
+ RED.nodes.registerType('periph-hmc5883l', {
34
+ category: 'Peripheral',
35
+ color: '#C084FC',
36
+ defaults: {
37
+ name: { value: '' },
38
+ device: { value: '', type: 'hmc5883l-device', required: true }
39
+ },
40
+ inputs: 1,
41
+ outputs: 1,
42
+ icon: 'font-awesome/fa-magnet',
43
+ label: function() { return this.name || 'HMC5883L'; },
44
+ paletteLabel: 'HMC5883L'
45
+ });
46
+ </script>
47
+
48
+ <script type="text/html" data-template-name="periph-hmc5883l">
49
+ <div class="form-row">
50
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
51
+ <input type="text" id="node-input-device">
52
+ </div>
53
+ <div class="form-row">
54
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
55
+ <input type="text" id="node-input-name" placeholder="HMC5883L">
56
+ </div>
57
+ </script>
58
+
59
+ <script type="text/html" data-help-name="periph-hmc5883l">
60
+ <p>Reads the magnetic field on three axes from an HMC5883L 3-axis magnetometer over I²C.</p>
61
+ <p>Any input message triggers a measurement. The device is configured via an
62
+ <b>hmc5883l-device</b> config node.</p>
63
+
64
+ <h3>Output</h3>
65
+ <dl class="message-properties">
66
+ <dt>payload.x <span class="property-type">number|null</span></dt>
67
+ <dd>X-axis magnetic field strength in Tesla. Null if ADC overflow (raw = -4096).</dd>
68
+ <dt>payload.y <span class="property-type">number|null</span></dt>
69
+ <dd>Y-axis magnetic field strength in Tesla. Null if ADC overflow (raw = -4096).</dd>
70
+ <dt>payload.z <span class="property-type">number|null</span></dt>
71
+ <dd>Z-axis magnetic field strength in Tesla. Null if ADC overflow (raw = -4096).</dd>
72
+ </dl>
73
+
74
+ <h3>Configuration</h3>
75
+ <p>Set the I²C bus number. The HMC5883L has a fixed 7-bit address of <code>0x1E</code>
76
+ (no address pin).</p>
77
+ </script>
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { HMC5883LFull } = require('periph/src/chips/magnetometer/hmc5883l');
6
+
7
+ function HMC5883LDeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const connection = new I2CConnection(parseInt(config.bus), parseInt(config.address, 16));
12
+ node.driver = new HMC5883LFull(connection);
13
+ node.connection = connection;
14
+ } catch (e) {
15
+ node.error('HMC5883L init failed: ' + e.message);
16
+ }
17
+ node.on('close', function() {
18
+ if (node.connection) node.connection.close();
19
+ });
20
+ }
21
+ RED.nodes.registerType('hmc5883l-device', HMC5883LDeviceNode);
22
+
23
+ function HMC5883LReadNode(config) {
24
+ RED.nodes.createNode(this, config);
25
+ const node = this;
26
+ node.device = RED.nodes.getNode(config.device);
27
+
28
+ node.on('input', async function(msg, send, done) {
29
+ if (!node.device || !node.device.driver) {
30
+ node.error('No HMC5883L device configured', msg);
31
+ done();
32
+ return;
33
+ }
34
+ try {
35
+ const d = node.device.driver;
36
+ const { x, y, z } = await d.magneticField();
37
+ msg.payload = { x, y, z };
38
+ send(msg);
39
+ done();
40
+ } catch (e) {
41
+ done(e);
42
+ }
43
+ });
44
+ }
45
+ RED.nodes.registerType('periph-hmc5883l', HMC5883LReadNode);
46
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-magnetometer",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Node-RED nodes for magnetometer chips",
5
5
  "keywords": [
6
6
  "node-red",