dongqi8114 2016-06-02 14:52
浏览 111

使用Raspberry GPIO的Python和PHP

With a little Python script I'm able to detect a button state that is connected in my Raspberry board. This is the script:

#!/usr/bin/env python

import os
import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

try:
    while True:
        if(GPIO.input(12) == 1):
            print 'Btn on'
            os.system('date')
            time.sleep(5)
        else:
            os.system('clear')
            print 'Waiting'
    time.sleep(2)

except KeyboardInterrupt:
    GPIO.cleanup()  

In this way, using PuTTY, or directly in the Raspberry terminal, I can easily detect the state of this button.

Now I would create the same thing but in PHP. I need to create a PHP web page that will be in the var/www/html directory of my Raspberry, and should just show me the same thing. When I press the button, I will get a simple echo "Btn on" with the timestamp, and if not pressed just another echo with "Waiting".. Is it possible? I tried directly exec the Python script, in PHP, in this way:

$command = escapeshellcmd('sudo -u www-data python btn.py');
$output = shell_exec($command);
echo $output;

but it's not working. Any idea?

  • 写回答

2条回答 默认 最新

  • dougutuo9879 2016-06-02 15:01
    关注

    It does not work because shell_exec waits for the python script to finish before it returns. However, the python script obviously does not reach the end because of the infinite loop. The simplest fix would be to poll the button status once with the python script and do any looping in php. Alternatively the python can write to a fifo file, from which the php script can read - but then be careful, because if you leave the python script running, it will fill the memory of the raspberry.

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料