talkeasy
Posts: 38
Joined: Sun Nov 04, 2012 10:57 pm

Photocell senosr written in PHP

Sat Oct 05, 2013 7:37 pm

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!
Last edited by talkeasy on Mon Oct 07, 2013 1:30 pm, edited 3 times in total.

User avatar
0xFF
Posts: 202
Joined: Tue Nov 20, 2012 7:52 pm
Location: Poland

Re: Photocell senosr written in PHP

Sat Oct 05, 2013 8:12 pm

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.

talkeasy
Posts: 38
Joined: Sun Nov 04, 2012 10:57 pm

Re: Photocell senosr written in PHP

Mon Oct 07, 2013 11:20 am

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?
Last edited by talkeasy on Mon Oct 07, 2013 1:31 pm, edited 1 time in total.

User avatar
0xFF
Posts: 202
Joined: Tue Nov 20, 2012 7:52 pm
Location: Poland

Re: Photocell senosr written in PHP

Mon Oct 07, 2013 11:57 am

Try to replace exec with native php file handling :

Code: Select all

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

talkeasy
Posts: 38
Joined: Sun Nov 04, 2012 10:57 pm

Re: Photocell senosr written in PHP

Mon Oct 07, 2013 12:21 pm

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')

User avatar
0xFF
Posts: 202
Joined: Tue Nov 20, 2012 7:52 pm
Location: Poland

Re: Photocell senosr written in PHP

Mon Oct 07, 2013 12:44 pm

exec() is slow.
You can optimize your code by replacing all exec(cat...) , exec (echo...) by native php file handling functions.

Return to “Automation, sensing and robotics”