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

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 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

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

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

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

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.

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: https://www.tinkerforge.com/en/doc/Software/API_Bindings_MQTT.html#api-bindings-mqtt
Have you followed the installation steps for the MQTT bindings

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.

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