node-red-contrib-periph-adc-dac 1.1.0 → 1.2.1

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.
@@ -0,0 +1,93 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('ad7705-device', {
3
+ category: 'config',
4
+ defaults: {
5
+ name: { value: '' },
6
+ bus: { value: '0', required: true, validate: RED.validators.number() },
7
+ device: { value: '0', required: true, validate: RED.validators.number() },
8
+ vref: { value: '2.5', required: true },
9
+ mclkHz: { value: '2457600', required: true }
10
+ },
11
+ label: function() {
12
+ return this.name || ('AD7705 @ SPI ' + this.bus + '.' + this.device + ' (Vref=' + this.vref + 'V)');
13
+ }
14
+ });
15
+ </script>
16
+
17
+ <script type="text/html" data-template-name="ad7705-device">
18
+ <div class="form-row">
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="AD7705">
21
+ </div>
22
+ <div class="form-row">
23
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> SPI bus</label>
24
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px">
25
+ </div>
26
+ <div class="form-row">
27
+ <label for="node-config-input-device"><i class="fa fa-microchip"></i> SPI device (CS)</label>
28
+ <input type="number" id="node-config-input-device" min="0" style="width:80px">
29
+ </div>
30
+ <div class="form-row">
31
+ <label for="node-config-input-vref">V_REF (V)</label>
32
+ <input type="number" id="node-config-input-vref" step="0.01" value="2.5" style="width:80px">
33
+ </div>
34
+ <div class="form-row">
35
+ <label for="node-config-input-mclkHz">Master clock (Hz)</label>
36
+ <select id="node-config-input-mclkHz" style="width:160px">
37
+ <option value="1000000">1 MHz</option>
38
+ <option value="2000000">2 MHz</option>
39
+ <option value="2457600">2.4576 MHz</option>
40
+ <option value="4915200">4.9152 MHz</option>
41
+ </select>
42
+ </div>
43
+ </script>
44
+
45
+ <script type="text/javascript">
46
+ RED.nodes.registerType('periph-ad7705', {
47
+ category: 'Peripheral',
48
+ color: '#C084FC',
49
+ defaults: {
50
+ name: { value: '' },
51
+ device: { value: '', type: 'ad7705-device', required: true }
52
+ },
53
+ inputs: 1,
54
+ outputs: 1,
55
+ icon: 'font-awesome/fa-wave-square',
56
+ label: function() { return this.name || 'AD7705'; },
57
+ paletteLabel: 'AD7705'
58
+ });
59
+ </script>
60
+
61
+ <script type="text/html" data-template-name="periph-ad7705">
62
+ <div class="form-row">
63
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
64
+ <input type="text" id="node-input-device">
65
+ </div>
66
+ <div class="form-row">
67
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
68
+ <input type="text" id="node-input-name" placeholder="AD7705">
69
+ </div>
70
+ </script>
71
+
72
+ <script type="text/html" data-help-name="periph-ad7705">
73
+ <p>Reads voltage from a 2-channel, 16-bit sigma-delta AD7705 ADC over SPI. The driver uses the chip's two-phase register-access protocol and polls DRDY over SPI, so only SCLK/DIN/DOUT/CS are needed (no dedicated DRDY GPIO).</p>
74
+
75
+ <h3>Inputs</h3>
76
+ <dl class="message-properties">
77
+ <dt>payload <span class="property-type">any</span></dt>
78
+ <dd>Any incoming message triggers a dual-channel read. If the device config has Channel 2 disabled, the output omits <code>channel2</code>. The literal string <code>"calibrate"</code> triggers a self-calibration of both configured channels.</dd>
79
+ </dl>
80
+
81
+ <h3>Outputs</h3>
82
+ <dl class="message-properties">
83
+ <dt>payload.channel1 <span class="property-type">number</span></dt>
84
+ <dd>Channel 1 input voltage in V.</dd>
85
+ <dt>payload.channel2 <span class="property-type">number</span></dt>
86
+ <dd>Channel 2 input voltage in V (omitted if Channel 2 is disabled).</dd>
87
+ <dt>payload.calibrated <span class="property-type">boolean</span></dt>
88
+ <dd>True after a <code>"calibrate"</code> command.</dd>
89
+ </dl>
90
+
91
+ <h3>Configuration</h3>
92
+ <p>Set the SPI bus and CS line (Linux <code>/dev/spidevBUS.DEVICE</code>), the reference voltage V_REF in V (default 2.5), and the master clock frequency wired to MCLK IN (1/2/2.4576/4.9152 MHz). SPI Mode 3 (CPOL=1 CPHA=1) is used; the maximum clock is 5 MHz.</p>
93
+ </script>
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const SPIConnection = require('periph/src/connection/spi').SPIConnection;
5
+ const AD7705Full = require('periph/src/chips/adc_dac/ad7705').AD7705Full;
6
+
7
+ function AD7705DeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const connection = new SPIConnection(
12
+ parseInt(config.bus),
13
+ parseInt(config.device),
14
+ { mode: 3, maxSpeedHz: 5_000_000 }
15
+ );
16
+ const vref = parseFloat(config.vref) || 2.5;
17
+ const mclkHz = parseInt(config.mclkHz) || 2457600;
18
+ const chip = new AD7705Full(connection, vref, mclkHz);
19
+
20
+ const channel1 = config.channel1 || { enabled: true, gain: '1', bipolar: true, buffered: false, rate: '50' };
21
+ const channel2 = config.channel2 || { enabled: false, gain: '1', bipolar: true, buffered: false, rate: '50' };
22
+
23
+ if (channel2.enabled) {
24
+ chip.configure(2, parseInt(channel2.gain), channel2.bipolar, channel2.buffered, parseInt(channel2.rate))
25
+ .then(() => chip.selfCalibrate(2))
26
+ .catch((err) => node.error('AD7705 channel 2 init failed: ' + err.message));
27
+ }
28
+
29
+ chip.selfCalibrate(1).then(() => {
30
+ node.chip = chip;
31
+ node.connection = connection;
32
+ node.channel2Enabled = channel2.enabled;
33
+ }).catch((err) => {
34
+ node.error('AD7705 channel 1 init failed: ' + err.message);
35
+ });
36
+ } catch (e) {
37
+ node.error('AD7705 init failed: ' + e.message);
38
+ }
39
+ node.on('close', function() {
40
+ if (node.connection) node.connection.close();
41
+ });
42
+ }
43
+ RED.nodes.registerType('ad7705-device', AD7705DeviceNode);
44
+
45
+ function AD7705Node(config) {
46
+ RED.nodes.createNode(this, config);
47
+ const node = this;
48
+ node.device = RED.nodes.getNode(config.device);
49
+
50
+ node.on('input', async function(msg, send, done) {
51
+ if (!node.device || !node.device.chip) {
52
+ done(new Error('No AD7705 device configured'));
53
+ return;
54
+ }
55
+ try {
56
+ const chip = node.device.chip;
57
+ if (msg.payload === 'calibrate') {
58
+ await chip.selfCalibrate(1);
59
+ if (node.device.channel2Enabled) {
60
+ await chip.selfCalibrate(2);
61
+ }
62
+ msg.payload = { calibrated: true };
63
+ send(msg); done();
64
+ return;
65
+ }
66
+ const v1 = await chip.readVoltageChannel(1);
67
+ const result = { channel1: v1 };
68
+ if (node.device.channel2Enabled) {
69
+ const v2 = await chip.readVoltageChannel(2);
70
+ result.channel2 = v2;
71
+ }
72
+ msg.payload = result;
73
+ send(msg); done();
74
+ } catch (e) { done(e); }
75
+ });
76
+ }
77
+ RED.nodes.registerType('periph-ad7705', AD7705Node);
78
+ };
@@ -0,0 +1,95 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('ad7706-device', {
3
+ category: 'config',
4
+ defaults: {
5
+ name: { value: '' },
6
+ bus: { value: '0', required: true, validate: RED.validators.number() },
7
+ device: { value: '0', required: true, validate: RED.validators.number() },
8
+ vref: { value: '2.5', required: true },
9
+ mclkHz: { value: '2457600', required: true }
10
+ },
11
+ label: function() {
12
+ return this.name || ('AD7706 @ SPI ' + this.bus + '.' + this.device + ' (Vref=' + this.vref + 'V)');
13
+ }
14
+ });
15
+ </script>
16
+
17
+ <script type="text/html" data-template-name="ad7706-device">
18
+ <div class="form-row">
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="AD7706">
21
+ </div>
22
+ <div class="form-row">
23
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> SPI bus</label>
24
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px">
25
+ </div>
26
+ <div class="form-row">
27
+ <label for="node-config-input-device"><i class="fa fa-microchip"></i> SPI device (CS)</label>
28
+ <input type="number" id="node-config-input-device" min="0" style="width:80px">
29
+ </div>
30
+ <div class="form-row">
31
+ <label for="node-config-input-vref">V_REF (V)</label>
32
+ <input type="number" id="node-config-input-vref" step="0.01" value="2.5" style="width:80px">
33
+ </div>
34
+ <div class="form-row">
35
+ <label for="node-config-input-mclkHz">Master clock (Hz)</label>
36
+ <select id="node-config-input-mclkHz" style="width:160px">
37
+ <option value="1000000">1 MHz</option>
38
+ <option value="2000000">2 MHz</option>
39
+ <option value="2457600">2.4576 MHz</option>
40
+ <option value="4915200">4.9152 MHz</option>
41
+ </select>
42
+ </div>
43
+ </script>
44
+
45
+ <script type="text/javascript">
46
+ RED.nodes.registerType('periph-ad7706', {
47
+ category: 'Peripheral',
48
+ color: '#C084FC',
49
+ defaults: {
50
+ name: { value: '' },
51
+ device: { value: '', type: 'ad7706-device', required: true }
52
+ },
53
+ inputs: 1,
54
+ outputs: 1,
55
+ icon: 'font-awesome/fa-wave-square',
56
+ label: function() { return this.name || 'AD7706'; },
57
+ paletteLabel: 'AD7706'
58
+ });
59
+ </script>
60
+
61
+ <script type="text/html" data-template-name="periph-ad7706">
62
+ <div class="form-row">
63
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
64
+ <input type="text" id="node-input-device">
65
+ </div>
66
+ <div class="form-row">
67
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
68
+ <input type="text" id="node-input-name" placeholder="AD7706">
69
+ </div>
70
+ </script>
71
+
72
+ <script type="text/html" data-help-name="periph-ad7706">
73
+ <p>Reads voltage from a 3-channel, 16-bit sigma-delta AD7706 ADC over SPI. The driver uses the chip's two-phase register-access protocol and polls DRDY over SPI, so only SCLK/DIN/DOUT/CS are needed (no dedicated DRDY GPIO). All three channels share a single COMMON reference pin.</p>
74
+
75
+ <h3>Inputs</h3>
76
+ <dl class="message-properties">
77
+ <dt>payload <span class="property-type">any</span></dt>
78
+ <dd>Any incoming message triggers a multi-channel read. Channels 2 and 3 are omitted from the output if their device config has them disabled. The literal string <code>"calibrate"</code> triggers a self-calibration of all configured channels.</dd>
79
+ </dl>
80
+
81
+ <h3>Outputs</h3>
82
+ <dl class="message-properties">
83
+ <dt>payload.channel1 <span class="property-type">number</span></dt>
84
+ <dd>Channel 1 (AIN1 vs COMMON) input voltage in V.</dd>
85
+ <dt>payload.channel2 <span class="property-type">number</span></dt>
86
+ <dd>Channel 2 (AIN2 vs COMMON) input voltage in V (omitted if Channel 2 is disabled).</dd>
87
+ <dt>payload.channel3 <span class="property-type">number</span></dt>
88
+ <dd>Channel 3 (AIN3 vs COMMON) input voltage in V (omitted if Channel 3 is disabled).</dd>
89
+ <dt>payload.calibrated <span class="property-type">boolean</span></dt>
90
+ <dd>True after a <code>"calibrate"</code> command.</dd>
91
+ </dl>
92
+
93
+ <h3>Configuration</h3>
94
+ <p>Set the SPI bus and CS line (Linux <code>/dev/spidevBUS.DEVICE</code>), the reference voltage V_REF in V (default 2.5), and the master clock frequency wired to MCLK IN (1/2/2.4576/4.9152 MHz). SPI Mode 3 (CPOL=1 CPHA=1) is used; the maximum clock is 5 MHz.</p>
95
+ </script>
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const SPIConnection = require('periph/src/connection/spi').SPIConnection;
5
+ const AD7706Full = require('periph/src/chips/adc_dac/ad7706').AD7706Full;
6
+
7
+ function AD7706DeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const connection = new SPIConnection(
12
+ parseInt(config.bus),
13
+ parseInt(config.device),
14
+ { mode: 3, maxSpeedHz: 5_000_000 }
15
+ );
16
+ const vref = parseFloat(config.vref) || 2.5;
17
+ const mclkHz = parseInt(config.mclkHz) || 2457600;
18
+ const chip = new AD7706Full(connection, vref, mclkHz);
19
+
20
+ const channel1 = config.channel1 || { enabled: true, gain: '1', bipolar: true, buffered: false, rate: '50' };
21
+ const channel2 = config.channel2 || { enabled: false, gain: '1', bipolar: true, buffered: false, rate: '50' };
22
+ const channel3 = config.channel3 || { enabled: false, gain: '1', bipolar: true, buffered: false, rate: '50' };
23
+
24
+ const initChannel = (chNum, ch) => {
25
+ if (ch.enabled) {
26
+ chip.configure(chNum, parseInt(ch.gain), ch.bipolar, ch.buffered, parseInt(ch.rate))
27
+ .then(() => chip.selfCalibrate(chNum))
28
+ .catch((err) => node.error(`AD7706 channel ${chNum} init failed: ` + err.message));
29
+ }
30
+ };
31
+
32
+ initChannel(2, channel2);
33
+ initChannel(3, channel3);
34
+
35
+ chip.selfCalibrate(1).then(() => {
36
+ node.chip = chip;
37
+ node.connection = connection;
38
+ node.channel2Enabled = channel2.enabled;
39
+ node.channel3Enabled = channel3.enabled;
40
+ }).catch((err) => {
41
+ node.error('AD7706 channel 1 init failed: ' + err.message);
42
+ });
43
+ } catch (e) {
44
+ node.error('AD7706 init failed: ' + e.message);
45
+ }
46
+ node.on('close', function() {
47
+ if (node.connection) node.connection.close();
48
+ });
49
+ }
50
+ RED.nodes.registerType('ad7706-device', AD7706DeviceNode);
51
+
52
+ function AD7706Node(config) {
53
+ RED.nodes.createNode(this, config);
54
+ const node = this;
55
+ node.device = RED.nodes.getNode(config.device);
56
+
57
+ node.on('input', async function(msg, send, done) {
58
+ if (!node.device || !node.device.chip) {
59
+ done(new Error('No AD7706 device configured'));
60
+ return;
61
+ }
62
+ try {
63
+ const chip = node.device.chip;
64
+ if (msg.payload === 'calibrate') {
65
+ await chip.selfCalibrate(1);
66
+ if (node.device.channel2Enabled) {
67
+ await chip.selfCalibrate(2);
68
+ }
69
+ if (node.device.channel3Enabled) {
70
+ await chip.selfCalibrate(3);
71
+ }
72
+ msg.payload = { calibrated: true };
73
+ send(msg); done();
74
+ return;
75
+ }
76
+ const v1 = await chip.readVoltageChannel(1);
77
+ const result = { channel1: v1 };
78
+ if (node.device.channel2Enabled) {
79
+ const v2 = await chip.readVoltageChannel(2);
80
+ result.channel2 = v2;
81
+ }
82
+ if (node.device.channel3Enabled) {
83
+ const v3 = await chip.readVoltageChannel(3);
84
+ result.channel3 = v3;
85
+ }
86
+ msg.payload = result;
87
+ send(msg); done();
88
+ } catch (e) { done(e); }
89
+ });
90
+ }
91
+ RED.nodes.registerType('periph-ad7706', AD7706Node);
92
+ };
@@ -0,0 +1,92 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-hx710a', {
3
+ category: 'Peripheral',
4
+ color: '#C084FC',
5
+ defaults: {
6
+ name: { value: '' },
7
+ gpioChip: { value: '0', required: true, validate: RED.validators.number() },
8
+ doutPin: { value: '5', required: true, validate: RED.validators.number() },
9
+ pdSckPin: { value: '6', required: true, validate: RED.validators.number() },
10
+ rate: { value: '10' },
11
+ scaleFactor: { value: '1.0' },
12
+ tareOffset: { value: '0' }
13
+ },
14
+ inputs: 1,
15
+ outputs: 1,
16
+ icon: 'font-awesome/fa-weight-hanging',
17
+ label: function() { return this.name || 'HX710A'; },
18
+ paletteLabel: 'HX710A'
19
+ });
20
+ </script>
21
+
22
+ <script type="text/html" data-template-name="periph-hx710a">
23
+ <div class="form-row">
24
+ <label for="node-input-gpioChip"><i class="fa fa-microchip"></i> GPIO chip</label>
25
+ <input type="number" id="node-input-gpioChip" min="0" style="width:80px"> &nbsp;gpiochip index (e.g. 0 for /dev/gpiochip0)
26
+ </div>
27
+ <div class="form-row">
28
+ <label for="node-input-doutPin"><i class="fa fa-microchip"></i> DOUT line</label>
29
+ <input type="number" id="node-input-doutPin" min="0" style="width:80px"> &nbsp;gpiod line offset (BCM on Raspberry Pi)
30
+ </div>
31
+ <div class="form-row">
32
+ <label for="node-input-pdSckPin"><i class="fa fa-microchip"></i> PD_SCK line</label>
33
+ <input type="number" id="node-input-pdSckPin" min="0" style="width:80px"> &nbsp;gpiod line offset (BCM on Raspberry Pi)
34
+ </div>
35
+ <div class="form-row">
36
+ <label for="node-input-rate"><i class="fa fa-tachometer-alt"></i> Rate</label>
37
+ <select id="node-input-rate" style="width:200px">
38
+ <option value="10">10 SPS (default)</option>
39
+ <option value="40">40 SPS</option>
40
+ </select>
41
+ </div>
42
+ <div class="form-row">
43
+ <label for="node-input-scaleFactor"><i class="fa fa-balance-scale"></i> Scale factor</label>
44
+ <input type="text" id="node-input-scaleFactor" style="width:120px"> &nbsp;counts per unit weight
45
+ </div>
46
+ <div class="form-row">
47
+ <label for="node-input-tareOffset"><i class="fa fa-crosshairs"></i> Tare offset</label>
48
+ <input type="text" id="node-input-tareOffset" style="width:120px"> &nbsp;or send <code>msg.payload = "tare"</code>
49
+ </div>
50
+ <div class="form-row">
51
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
52
+ <input type="text" id="node-input-name" placeholder="HX710A">
53
+ </div>
54
+ </script>
55
+
56
+ <script type="text/html" data-help-name="periph-hx710a">
57
+ <p>Reads weight from an HX710A 24-bit ADC connected to a load cell via two GPIO pins.
58
+ Also exposes the chip's on-chip temperature sensor as a raw ADC code.</p>
59
+
60
+ <h3>Inputs</h3>
61
+ <dl class="message-properties">
62
+ <dt>payload <span class="property-type">any</span></dt>
63
+ <dd>Any message triggers a weight reading. If <code>payload</code> is the string
64
+ <code>"tare"</code>, the node captures the current average (10 readings) as the
65
+ zero offset and produces no output message. If <code>payload</code> is the string
66
+ <code>"temperature"</code>, the node triggers a temperature-channel reading instead.</dd>
67
+ </dl>
68
+
69
+ <h3>Outputs</h3>
70
+ <dl class="message-properties">
71
+ <dt>payload.raw <span class="property-type">number</span></dt>
72
+ <dd>Signed 24-bit differential-input ADC value (-8 388 608 to +8 388 607).</dd>
73
+ <dt>payload.weight <span class="property-type">number</span></dt>
74
+ <dd>Calibrated weight: <code>(raw - tareOffset) / scaleFactor</code>.
75
+ Returns <code>0.0</code> when scale factor is 1.0 and offset is 0.</dd>
76
+ <dt>payload.raw_temperature <span class="property-type">number</span></dt>
77
+ <dd>Only present when triggered by <code>payload = "temperature"</code>. Raw,
78
+ uncalibrated on-chip temperature sensor code (~20.4 LSB/°C, not a °C value).</dd>
79
+ </dl>
80
+
81
+ <h3>Details</h3>
82
+ <p><b>GPIO chip</b> is the gpiochip index (check with <code>gpiodetect</code>); <b>DOUT line</b> and
83
+ <b>PD_SCK line</b> are gpiod line offsets on that chip (check with <code>gpioinfo</code> — matches
84
+ the BCM GPIO number directly on Raspberry Pi's <code>pinctrl-bcm2711</code>).</p>
85
+ <p><b>Rate</b> selects the differential-input output rate: 10 or 40 samples per second.
86
+ The HX710A has a single, fixed-gain (128) differential input — unlike the HX711, there
87
+ is no channel or gain selection.</p>
88
+ <p><b>Scale factor</b> is the number of ADC counts per unit weight. Calibrate by placing a
89
+ known weight on the scale: <code>factor = (readAverage() - tareOffset) / knownWeight</code>.</p>
90
+ <p><b>Tare offset</b> sets the initial zero point. You can also zero the scale at any time
91
+ by injecting a message with <code>payload = "tare"</code>.</p>
92
+ </script>
@@ -0,0 +1,56 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { Default } = require('opengpio');
5
+ const { HX711Connection } = require('periph/src/connection/hx711');
6
+ const { HX710AFull } = require('periph/src/chips/adc_dac/hx710a');
7
+
8
+ function HX710ANode(config) {
9
+ RED.nodes.createNode(this, config);
10
+ const node = this;
11
+
12
+ try {
13
+ const chip = parseInt(config.gpioChip || 0);
14
+ const dout = Default.input({ chip, line: parseInt(config.doutPin) });
15
+ const pd_sck = Default.output({ chip, line: parseInt(config.pdSckPin) });
16
+ node.connection = new HX711Connection(dout, pd_sck);
17
+ node.driver = new HX710AFull(node.connection);
18
+
19
+ const rate = parseInt(config.rate || 10);
20
+ if (rate !== 10) node.driver.setRate(rate);
21
+ node.driver._offset = parseInt(config.tareOffset || 0);
22
+ node.driver.setScale(parseFloat(config.scaleFactor || 1.0));
23
+ } catch (e) {
24
+ node.error('HX710A init failed: ' + e.message);
25
+ }
26
+
27
+ node.on('input', function(msg, send, done) {
28
+ if (!node.driver) { done(); return; }
29
+ try {
30
+ if (msg.payload === 'tare') {
31
+ node.driver.tare(10);
32
+ done();
33
+ } else if (msg.payload === 'temperature') {
34
+ const raw_temperature = node.driver.readTemperatureRaw();
35
+ msg.payload = { raw_temperature };
36
+ send(msg);
37
+ done();
38
+ } else {
39
+ const raw = node.driver.readRaw();
40
+ const weight = (raw - node.driver.getOffset()) / node.driver.getScale();
41
+ msg.payload = { raw, weight };
42
+ send(msg);
43
+ done();
44
+ }
45
+ } catch (e) {
46
+ done(e);
47
+ }
48
+ });
49
+
50
+ node.on('close', function() {
51
+ if (node.connection) node.connection.close();
52
+ });
53
+ }
54
+
55
+ RED.nodes.registerType('periph-hx710a', HX710ANode);
56
+ };
@@ -0,0 +1,95 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-hx710b', {
3
+ category: 'Peripheral',
4
+ color: '#C084FC',
5
+ defaults: {
6
+ name: { value: '' },
7
+ gpioChip: { value: '0', required: true, validate: RED.validators.number() },
8
+ doutPin: { value: '5', required: true, validate: RED.validators.number() },
9
+ pdSckPin: { value: '6', required: true, validate: RED.validators.number() },
10
+ rate: { value: '10' },
11
+ scaleFactor: { value: '1.0' },
12
+ tareOffset: { value: '0' }
13
+ },
14
+ inputs: 1,
15
+ outputs: 1,
16
+ icon: 'font-awesome/fa-weight-hanging',
17
+ label: function() { return this.name || 'HX710B'; },
18
+ paletteLabel: 'HX710B'
19
+ });
20
+ </script>
21
+
22
+ <script type="text/html" data-template-name="periph-hx710b">
23
+ <div class="form-row">
24
+ <label for="node-input-gpioChip"><i class="fa fa-microchip"></i> GPIO chip</label>
25
+ <input type="number" id="node-input-gpioChip" min="0" style="width:80px"> &nbsp;gpiochip index (e.g. 0 for /dev/gpiochip0)
26
+ </div>
27
+ <div class="form-row">
28
+ <label for="node-input-doutPin"><i class="fa fa-microchip"></i> DOUT line</label>
29
+ <input type="number" id="node-input-doutPin" min="0" style="width:80px"> &nbsp;gpiod line offset (BCM on Raspberry Pi)
30
+ </div>
31
+ <div class="form-row">
32
+ <label for="node-input-pdSckPin"><i class="fa fa-microchip"></i> PD_SCK line</label>
33
+ <input type="number" id="node-input-pdSckPin" min="0" style="width:80px"> &nbsp;gpiod line offset (BCM on Raspberry Pi)
34
+ </div>
35
+ <div class="form-row">
36
+ <label for="node-input-rate"><i class="fa fa-tachometer-alt"></i> Rate</label>
37
+ <select id="node-input-rate" style="width:200px">
38
+ <option value="10">10 SPS (default)</option>
39
+ <option value="40">40 SPS</option>
40
+ </select>
41
+ </div>
42
+ <div class="form-row">
43
+ <label for="node-input-scaleFactor"><i class="fa fa-balance-scale"></i> Scale factor</label>
44
+ <input type="text" id="node-input-scaleFactor" style="width:120px"> &nbsp;counts per unit weight
45
+ </div>
46
+ <div class="form-row">
47
+ <label for="node-input-tareOffset"><i class="fa fa-crosshairs"></i> Tare offset</label>
48
+ <input type="text" id="node-input-tareOffset" style="width:120px"> &nbsp;or send <code>msg.payload = "tare"</code>
49
+ </div>
50
+ <div class="form-row">
51
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
52
+ <input type="text" id="node-input-name" placeholder="HX710B">
53
+ </div>
54
+ </script>
55
+
56
+ <script type="text/html" data-help-name="periph-hx710b">
57
+ <p>Reads weight from an HX710B 24-bit ADC connected to a load cell via two GPIO pins.
58
+ Also exposes the chip's DVDD-AVDD supply-voltage-difference channel as a raw ADC code
59
+ for battery-monitoring applications.</p>
60
+
61
+ <h3>Inputs</h3>
62
+ <dl class="message-properties">
63
+ <dt>payload <span class="property-type">any</span></dt>
64
+ <dd>Any message triggers a weight reading. If <code>payload</code> is the string
65
+ <code>"tare"</code>, the node captures the current average (10 readings) as the
66
+ zero offset and produces no output message. If <code>payload</code> is the string
67
+ <code>"supply_diff"</code>, the node triggers a supply-difference-channel reading instead.</dd>
68
+ </dl>
69
+
70
+ <h3>Outputs</h3>
71
+ <dl class="message-properties">
72
+ <dt>payload.raw <span class="property-type">number</span></dt>
73
+ <dd>Signed 24-bit differential-input ADC value (-8 388 608 to +8 388 607).</dd>
74
+ <dt>payload.weight <span class="property-type">number</span></dt>
75
+ <dd>Calibrated weight: <code>(raw - tareOffset) / scaleFactor</code>.
76
+ Returns <code>0.0</code> when scale factor is 1.0 and offset is 0.</dd>
77
+ <dt>payload.raw_supply_diff <span class="property-type">number</span></dt>
78
+ <dd>Only present when triggered by <code>payload = "supply_diff"</code>. Raw,
79
+ uncalibrated DVDD-AVDD supply-difference code — the datasheet gives no absolute
80
+ LSB-to-volts scale factor for this reading; use it for relative drift tracking
81
+ against a known-good baseline, not as an absolute voltage.</dd>
82
+ </dl>
83
+
84
+ <h3>Details</h3>
85
+ <p><b>GPIO chip</b> is the gpiochip index (check with <code>gpiodetect</code>); <b>DOUT line</b> and
86
+ <b>PD_SCK line</b> are gpiod line offsets on that chip (check with <code>gpioinfo</code> — matches
87
+ the BCM GPIO number directly on Raspberry Pi's <code>pinctrl-bcm2711</code>).</p>
88
+ <p><b>Rate</b> selects the differential-input output rate: 10 or 40 samples per second.
89
+ The HX710B has a single, fixed-gain (128) differential input — unlike the HX711, there
90
+ is no channel or gain selection.</p>
91
+ <p><b>Scale factor</b> is the number of ADC counts per unit weight. Calibrate by placing a
92
+ known weight on the scale: <code>factor = (readAverage() - tareOffset) / knownWeight</code>.</p>
93
+ <p><b>Tare offset</b> sets the initial zero point. You can also zero the scale at any time
94
+ by injecting a message with <code>payload = "tare"</code>.</p>
95
+ </script>