Help with triggering python child process

I’m trying to use blynk app to trigger a python script on Raspberry Pi 4 which toggles a fan or light. I can’t get it to work. When I run the app on the Pi and clink on button1 on my phone I get “Got a value : 4”. But when I click button2, the VirtualPin2 value is not shown. Also the actual python script isn’t run either. My main.cpp looks like this:

#define BLYNK_PRINT stdout

#ifdef RASPBERRY
 #include <BlynkApiWiringPi.h>
#else
 #include <BlynkApiLinux.h>
#endif
#include <BlynkSocket.h>
#include <BlynkOptionsParser.h>

static BlynkTransportSocket _blynkTransport;
BlynkSocket Blynk(_blynkTransport);
#include <BlynkWidgets.h>

// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V1
BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  
  if (pinValue == 4 || pinValue == 0)
  {
      system("python3 /home/pi/Desktop/Automation/light_toggle.py");
    }


  // process received value
}

BLYNK_WRITE(V2)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  
  if (pinValue == 5 || pinValue == 0)
  {
      system("python3 /home/pi/Desktop/Automation/fan_toggle.py");
    }

void setup()
{
}

void loop()
{
  Blynk.run();
}

int main(int argc, char* argv[])
{
  const char *auth, *serv;
  uint16_t port;
  parse_options(argc, argv, auth, serv, port);

  Blynk.begin(auth, serv, port);

  setup();
  while(true) {
    loop();
  }

  return 0;
}

Don’t know much about C++ and even less about how it works with Blynk :joy:

But don’t you need to include <stdlib.h> to use system()?

It looks like you’re missing a closing bracket } in BLYNK_WRITE(V2).

Does the script work if you run it like this: sudo python3 /home/pi/Desktop/Automation/fan_toggle.py? The root user might not have python3 in its path.

No error messages?!