A problem of executing bin file generated by C

We recently attempt to use tuya cloud in Node-red, and we have found out that tuya's SDK is base on C--tuya SDK.
After following the instructions, we do compliled and got the executable bin file "subdevice_basic_demo". It dose work in command line like this, and links to the tuya cloud well.

croot@PN6400:~# cd /home/tuya/tuya-iot-core-sdk-main/build/bin
root@PN6400:/home/tuya/tuya-iot-core-sdk-main/build/bin# ./subdevice_basic_demo 

But when we call the bin file in js file like this:

var fs = require("fs");
var exec = require('child_process').exec;

module.exports = function(RED) {
    function PN6400_tuyaInit(config) {
					RED.nodes.createNode(this,config);
					var node = this;
					this.key = RED.nodes.getNode(config.key);

					node.on('input', function(msg) {
						//run ('export PATH="/home/tuya/tuya-iot-core-sdk-main/build/bin/:$PATH"') ahead of time 
						exec('subdevice_basic_demo'+' ',function(error,stdout,stderr){
							if(error) {
								console.info('stderr : '+stderr);
							}
						});
					});
    }
    RED.nodes.registerType("PN6400_tuyaInit",PN6400_tuyaInit);
}

Then nothing happened, aka the executable file "subdevice_basic_demo" never executed.
So anyone can help us?

Is the problem that it is not calling your code or that the code does not work when called like that? You can check by adding some logging to your C code so that it, for example, logs what is happening to file.

The code does not work when called.
And I have added some logging codes in the C file, but still nothing happended.

FYI
If I replace the bin file with a simple python file instead, the python file works.

exec('#a python file instead #'+' ',function(error,stdout,stderr){
							if(error) {
								console.info('stderr : '+stderr);
							}
						});

So the problem should not be the JS calling part itself.

If it does work outside of Node-RED from the command line, then you proabably either need the full path to the command when you call it - or the environment that you are running Node-RED is not the same as the user you are using to run from command line.

  1. Try using ./subdevice_basic_demo
  2. Is the executable available in the cwd? Node-red current working directory will NOT be /home/tuya/tuya-iot-core-sdk-main/build/bin (i.e. try using full path) and check the account running node-red can access and execute the binary)

Ps. Instead of calling exec, you might want to investigate adding c bindings to call the program/functions directly: How to call C/C++ code from Node.js | by Konstantin Tarkus | Medium

Why not use one of the existing Node.js wrappers for Tuya?

tuya - npm search (npmjs.com)

Thanks for your advice.
The problem is solved.
As I put the bin file under the path of /usr/bin, like this:


Then the JS code can call the bin file.
PS: In both cmdline and Node-RED mode, the logged user is root.

Thanks.Right, the path is the key point.

Surely this is a bright idea.
I have planned to create some different functions from the existing libraries.

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