Page 1 of 1

Photocell senosr written in PHP

Posted: Sat Oct 05, 2013 7:37 pm
by talkeasy
Hi All

I have build the circuit board and have successfully run the python code for testing a Photocell LDR as documented on the Adafruit web site:
http://learn.adafruit.com/basic-resisto ... ll-reading

For various reasons, not least for the challenge, I have tried to transpose the code into PHP as per below, but it only returns a series of zeros.

Code: Select all

<?php
# Light Sensor - loops until cancelled by CTL-C

exec("modprobe w1-gpio");

function Getgpio22()
{ 
$reading = 0;

# Discharge capacitor
# set up GPIO 22 direction to OUT and value to low
exec("echo \"22\" > /sys/class/gpio/export");
exec("chmod 777 -R /sys/class/gpio/gpio22");
exec("echo \"out\" > /sys/class/gpio/gpio22/direction");
exec("echo \"0\" > /sys/class/gpio/gpio22/value");
sleep(0.1);

# set up GPIO 22 direction to IN
exec("echo \"in\" > /sys/class/gpio/gpio22/direction");

while (exec("cat /sys/class/gpio/gpio22/value") ==0)
{
# Count loops until voltage across
# capacitor reads high on GPIO
$reading++;
} 
return $reading;
}

while (true)
{
print Getgpio22();
}

?>
Could someone suggest where I am going wrong, please?

Thanks!

Re: Photocell senosr written in PHP

Posted: Sat Oct 05, 2013 8:12 pm
by 0xFF
If you want to use your PHP code to show sensor reading in your web site, I propose (for performance and security reason) to split your code for two parts.
One part - for init, reading sensors and storing values in file (database or something ...) - driven by cron daemon.
Second part - just for feeding values from file (or database) to the website.

Re: Photocell senosr written in PHP

Posted: Mon Oct 07, 2013 11:20 am
by talkeasy
The php code above outputs 0s as stated - but getting my brain in gear - if I cover the sensor to make it completely dark, the stream changes to 1s.

So the code works, but does not show the range / gradient that the Adafruit Python code generates, for example:

Code: Select all

492
497
497
494
523
571
632
840
1453
1465
1427
1388
1427
1418
1455
1436
Could this be due to the performance of PHP compared with Python? Could the output of PHP be improved by changing the resistor or capacity value?

Re: Photocell senosr written in PHP

Posted: Mon Oct 07, 2013 11:57 am
by 0xFF
Try to replace exec with native php file handling :

Code: Select all

while (file_get_contents('/sys/class/gpio/gpio22/value') =='0')

Re: Photocell senosr written in PHP

Posted: Mon Oct 07, 2013 12:21 pm
by talkeasy
Thank-you very much! Yes that did the trick.

For anyone reusing this code, just one small change is needed:

Code: Select all

while (file_get_contents('/sys/class/gpio/gpio22/value') ==0)
without the quotes around the zero, instead of
while (file_get_contents('/sys/class/gpio/gpio22/value') =='0')

Re: Photocell senosr written in PHP

Posted: Mon Oct 07, 2013 12:44 pm
by 0xFF
exec() is slow.
You can optimize your code by replacing all exec(cat...) , exec (echo...) by native php file handling functions.