0

/var/www/html/index.php

<?php

exec("./on");
shell_exec("/var/www/html/on");

on.cpp

#include <string>
#include <getopt.h>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <unistd.h>
#include <wiringPi.h>

using namespace std;

void light_1_on() {
  digitalWrite(0, HIGH);
}

int main( int argc, char ** argv){

  cout << "Setting up wiringPi" << endl;
  wiringPiSetup();
  pinMode(0, OUTPUT);

  light_1_on();

}

Built using: g++ on.cpp -o on -lwiringPi

Permissions changed with: chmod 777 on

Access changed with: chown www-data:www-data on

When run ./on via the terminal, the light that's connected to this GPIO output turns on. But when run via my exec or shell_exec via php, the light does not turn on.

Any ideas why I might not be able to turn the light on via execution in php? Any fixes or workarounds for this?

Thanks!

1

It turns out I had to add gpio to the group for www-data. I had to use this:

sudo usermod -a -G gpio www-data

Took me quite a while to figure this out. Hope it helps someone!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.