RPi Blynk Temperatur/Humidity Sensor

When I use my script like that:

var Blynk = require('blynk-library');
var AUTH = 'MY AUTH TOKEN';
var blynk = new Blynk.Blynk(AUTH);
var v1 = new blynk.VirtualPin(1);
const childProcess = require('child_process');

//"LIGHT"
v1.on('write', function(param) {
  if (param == 1) { 
    childProcess.spawn('~/Desktop/Programme/Remote/steuerungA 1', { shell: true });
  }
  if (param == 0) {
    childProcess.spawn('~/Desktop/Programme/Remote/steuerungA 0', { shell: true });
  }
});

and start it like that:

NODE_PATH=/usr/local/lib/node_modules node ./index.js

everything works and I can control my light with my 433 MHz Transmitter.
But now I want to add the Temperatur/Humidity Sensor.
So I added that to my code:

var sensorLib = require('node-dht-sensor');
var sensorType = 11;
var sensorPin  = 4;
    if (!sensorLib.initialize(sensorType, sensorPin)) {
        console.warn('Failed to initialize sensor');
    process.exit(1);
}
setInterval(function() {
    var readout = sensorLib.read();
    blynk.virtualWrite(2, readout.temperature.toFixed(1));
    blynk.virtualWrite(3, readout.humidity.toFixed(1));
    
    console.log('Temperature:', readout.temperature.toFixed(1) + 'C');
    console.log('Humidity:   ', readout.humidity.toFixed(1)    + '%');
}, 2000);

When I start my script now like that:

NODE_PATH=/usr/local/lib/node_modules node ./index.js

it doesn’t work, so I have to start it like that:

sudo NODE_PATH=/usr/local/lib/node_modules node ./index.js

So now the Sensor works and I can see the Temperatur/Humidity in my App.
But when I use my button for the light now, nothing happen and no error or something else.

Can you help me??

Thanks!!!

FYI, I fixed your post to use the proper and documented method for formatting posted code… it is much easier for us to view.

Blynk - FTFC

Normal as some processes in the sketch probably require root

Try adding in some terminal log statements so you can track what your code is doing - similar process as Serial.print() in Arduino.

console.log("Something happens here");

Or

console.log("Received Parameter is: ", param);

Etc.

I added an console.log at the if in my v1.on and the text is written when I use my button, but the light don’t turn on…
There is a problem between the sudo and the SteuerungA 1/0.
But why? :S

I do not see how using an elevated root process can prevent your child process from running… but then that is a Linux issue and not something we can really do much about here in a Blynk forum. Try asking around on various Linux forums.

All I can suggest otherwise is to try breaking down your sketch, or using a smaller test sketch, just controlling the light, and compare with all your available variants… sudo, not sudo, Blynk, not Blynk, alternate ways of controlling the light (e.g. direct JS code instead of what appears to be a Python sub-process), double checking path and user access, various combos of each, etc and so on.