# Tinkerforge Industrial Dual 0-20mA Bricklet read Data in NodeRed

**URL:** <https://discourse.nodered.org/t/tinkerforge-industrial-dual-0-20ma-bricklet-read-data-in-nodered/22922>\
**Category:** General\
**Created:** [10 March 2020 12:42 UTC](https://discourse.nodered.org/t/tinkerforge-industrial-dual-0-20ma-bricklet-read-data-in-nodered/22922 "2020-03-10T12:42:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bix85](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bix85/32/18198_2.png) [@Bix85](https://discourse.nodered.org/u/Bix85)\
**Post date:** [10 March 2020 12:42 UTC](https://discourse.nodered.org/t/tinkerforge-industrial-dual-0-20ma-bricklet-read-data-in-nodered/22922/1 "2020-03-10T12:42:11Z")

</div>

Hi,

I am a new user of Node Red and have not much experience with it.

For a project I have a Raspberry Pi with NodeRed installation.  
Connected to the Pi is a Tinkerforge Masterbricklet that have 2 x [Industrial Dual 0-20mA Bricklet](https://www.tinkerforge.com/de/doc/Hardware/Bricklets/Industrial_Dual_020mA.html) connected.  
I like to get the current values of the industrial bricklets.  
I tried in different ways, but all ways I tried will not work.

Do you have an Idea how to implement such a system?

Thanks a lot!

Best regards Bix

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [10 March 2020 15:19 UTC](https://discourse.nodered.org/t/tinkerforge-industrial-dual-0-20ma-bricklet-read-data-in-nodered/22922/2 "2020-03-10T15:19:22Z")

</div>

Hi Bix,  
I’ve read through the tinkerforge documentation before, but don’t have hands-on experience. Can you explain what you’ve tried so far (the things that didn’t work), and if you’ve you’re master bricklet as standalone or wired over usb/ethernet/something else to the pi?  
What does your code on the bricklet do, and how are you trying to communicate with NR?

~ Lena

---

<div class="post-metadata">

**Author:** ![Bix85](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bix85/32/18198_2.png) [@Bix85](https://discourse.nodered.org/u/Bix85)\
**Post date:** [11 March 2020 08:36 UTC](https://discourse.nodered.org/t/tinkerforge-industrial-dual-0-20ma-bricklet-read-data-in-nodered/22922/3 "2020-03-11T08:36:15Z")

</div>

Hello Lena,  
the masterbricklet is connected to the Pi with a micro usb cable.

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/31f5109be3f3e7c7b4617b4cba3a208c97edf4c8.png)

Above a screenshot of the tried.  
As function I've wrote this:

var Tinkerforge = require('/usr/lib/node\_modules/tinkerforge');  
var HOST = 'localhost';  
var PORT = 4223;  
var UID = 'seR'; // Change XYZ to the UID of your Industrial Dual 0-20mA Bricklet

var ipcon = new Tinkerforge.IPConnection(); // Create IP connection  
var id020 = new Tinkerforge.BrickletIndustrialDual020mA(UID, ipcon); // Create device object

ipcon.connect(HOST, PORT,  
function (error) {  
node.warn('Error: ' + error);  
}  
); // Connect to brickd  
// Don't use device before ipcon is connected

ipcon.on(Tinkerforge.IPConnection.CALLBACK\_CONNECTED,  
function (connectReason) {  
// Get current current from sensor 1  
id020.getCurrent(1,  
function (current) {  
msg.paylaod = "{"Current (Sensor 1)":" + current/1000000.0 + "}";  
node.warn('Current (Sensor 1): ' + current/1000000.0 + ' mA');  
},  
function (error) {  
msg.payload ="{"Current (Sensor 1)":" + error + "}";  
node.warn('Error: ' + error);  
}  
);  
}  
);

ipcon.disconnect();  
return msg;

But the main problem is, that I get no connection to the bricklet. So I think, something with my mqtt is wrong.

Bix

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [11 March 2020 16:59 UTC](https://discourse.nodered.org/t/tinkerforge-industrial-dual-0-20ma-bricklet-read-data-in-nodered/22922/4 "2020-03-11T16:59:24Z")

</div>

I added the indentation to your code. Can you verify that the following is indeed yours? Because if so I've spotted 2 syntax errors in it, and I've a couple questions regarding it.  
You use `node.warn` and set `msg.payload` in it. Do you have a specific reason why? As far as I know, Tinkerforge requires you to use `console.log` calls for outputting when using javascript. At the very end, you disconnect from the ip connection, then return the msg object. Why are you returning at the end? Unless of course, by function you mean a function node in Node-RED. Which I've to add immediately that what you're trying to do likely won't work the way you think it will. I'll continue below the code.

```javascript
var Tinkerforge = require('/usr/lib/node_modules/tinkerforge');
var HOST = 'localhost';
var PORT = 4223;
var UID = 'seR'; // Change XYZ to the UID of your Industrial Dual 0-20mA Bricklet

var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
var id020 = new Tinkerforge.BrickletIndustrialDual020mA(UID, ipcon); // Create device object

ipcon.connect(HOST, PORT,
    function (error) {
        node.warn('Error: ' + error);
    }
); // Connect to brickd
// Don't use device before ipcon is connected

ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
    function (connectReason) {
        // Get current current from sensor 1
        id020.getCurrent(1,
            function (current) {
                msg.paylaod = "{"Current (Sensor 1)":" + current/1000000.0 + "}";
                node.warn('Current (Sensor 1): ' + current/1000000.0 + ' mA');
            },
            function (error) {
                msg.payload ="{"Current (Sensor 1)":" + error + "}";
                node.warn('Error: ' + error);
            }
        );
    }
);

ipcon.disconnect();
return msg;

```

To use Tinkerforge, you've to execute the code on the device, using the brick daemon. Are you running the brick daemon on the raspberry pi? In your flow you use topics that match the default setup for the MQTT API bindings. These are a way to run code on the bricks, just like you would with Javascript, or Python (as all the default examples use Python in their docs) for that matter. The MQTT API bindings are documented here: [Doc | Tinkerforge](https://www.tinkerforge.com/en/doc/Software/API_Bindings_MQTT.html#api-bindings-mqtt)  
Have you followed the installation steps for the MQTT bindings

> [@](#):
>
> To install the bindings copy the tinkerforge\_mqtt file to a folder that is in the PATH. For example, this folder on Linux and macOS:  
> `/usr/local/bin/`

> [@](#):
>
> To test a MQTT example [Brick Daemon](https://www.tinkerforge.com/en/doc/Software/Brickd.html#brickd) and [Brick Viewer](https://www.tinkerforge.com/en/doc/Software/Brickv.html#brickv) have to be installed first. Brick Daemon acts as a proxy between the USB interface of the Bricks and the API bindings. Brick Viewer connects to Brick Daemon and helps to figure out basic information about the connected Bricks and Bricklets.
> 
> Then run the `tinkerforge_mqtt` script, configured to connect to your Brick Daemon and MQTT broker, using the command line switches. See the output of `tinkerforge_mqtt --help`.
> 
> All examples are written in pseudo code, you have to translate them to your favorite programming language before running them. Alternatively you can use the `mosquitto_pub` and `mosquitto_sub` commands, which are part of the Mosquitto MQTT broker.

Using Node-RED with the MQTT nodes is of course an alternative for using `mosquitto_pub` and `mosquitto_sub`, which you appear to be using here. The error you see in your flow says "connection broken". Have you double checked that the settings in Node-RED for connecting it to your MQTT broker are correct, and match the broker settings that Tinkerforge is using (`tinkerforge_mqtt --broker-host ... --broker-port ...` and so on)?

As for the syntax errors/typos in your code: `msg.paylaod = "{"Current (Sensor 1)":" + current/1000000.0 + "}";`. You're assigning `paylaod`, rather than `payload`. Next you set a string to it, but it contains `"` characters that aren't escaped, resulting in a syntax error. If your goal is to have it become a string looking like `{"Current (Sensor 1)": <number>}"`, use `msg.payload = "{\"Current (Sensor 1)\": current/1000000.0 + "}";`. However, since that output is the string representation of a JSON object, you might just be looking for `msg.payload = {"Current (Sensor 1)": current/1000000.0}` instead.

I'm not sure if you can actually run a brick binding in a javascript in a function node, but you can't use `require(...)` there. If you were to do so, you need to add it to the functionGlobalContext in the settings.js file, and get it from the global object. This is written in the Node-RED function node documentation (but as my battery is about to die I won't link it now).

Start by checking if you set up the mqtt broker correctly, then check if you have it set correctly on the `tinkerforge_mqtt` script, then check if that all matches the settings in Node-RED for the MQTT nodes. Use an MQTT client on your computer to verify that it all works well, and that you can see the output for the topics that you would do with Node-RED, I recommend MQTT Explorer for this task.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 May 2020 17:12 UTC](https://discourse.nodered.org/t/tinkerforge-industrial-dual-0-20ma-bricklet-read-data-in-nodered/22922/5 "2020-05-10T17:12:29Z")

</div>

Closed on 10 May 2020 17:12 UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
