如何通过一个Apache网页服务器控制树莓派的串行GPIO引脚 [英] How to control the serial GPIO pin of Raspberry Pi via an Apache web sever

查看:1131
本文介绍了如何通过一个Apache网页服务器控制树莓派的串行GPIO引脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从树莓派数据发送到的欧诺的Arduino通过GPIO串行端口通过使用树莓派运行Apache服务器的。我连RPI的TX引脚为3.3  V-TO-5 五电平转换器,其输出到RX Arduino的PIN

I would like to send data from Raspberry Pi to Arduino Uno via the GPIO serial port by using the Apache server running on Raspberry Pi. I connected the TX pin of RPI to a 3.3 V-to-5 V level shifter and its output to the RX Arduino PIN.

要从树莓派将数据发送到Arduino的我用我重新编译树莓派下面的C程序,它工作正常。我改名为可执行code SendUART

To send data from Raspberry Pi to Arduino I used the following C program that I recompiled for Raspberry Pi, and it works fine. I renamed the executable code SendUART.

#include <stdio.h>
#include <unistd.h>   //Used for UART
#include <fcntl.h>    //Used for UART
#include <termios.h>  //Used for UART
#include <string.h>

main(int argc,char **argv)
{
    //----- TX BYTES -----
    unsigned char tx_buffer[20];
    unsigned char *p_tx_buffer;
    int lx;

    //-------------------------
    //----- SETUP USART 0 -----
    //-------------------------
    //At bootup, pins 8 and 10 are already set to UART0_TXD, UART0_RXD (ie the alt0 function) respectively
    int uart0_filestream = -1;

    //OPEN THE UART
    //The flags (defined in fcntl.h):
    //    Access modes (use 1 of these):
    //        O_RDONLY - Open for reading only.
    //        O_RDWR - Open for reading and writing.
    //        O_WRONLY - Open for writing only.
    //
    //    O_NDELAY / O_NONBLOCK (same function) - Enables nonblocking mode. When set read requests on the file can return immediately with a failure status
    //                                            if there is no input immediately available (instead of blocking). Likewise, write requests can also return
    //                                            immediately with a failure status if the output can't be written immediately.
    //
    //    O_NOCTTY - When set and path identifies a terminal device, open() shall not cause the terminal device to become the controlling terminal for the process.
    uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);        //Open in non blocking read/write mode
    if (uart0_filestream == -1)
    {
        //ERROR - CAN'T OPEN SERIAL PORT
        printf("Error - Unable to open UART.  Ensure it is not in use by another application\n");
    }

    //CONFIGURE THE UART
    //The flags (defined in /usr/include/termios.h - see http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html):
    //    Baud rate:- B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000
    //    CSIZE:- CS5, CS6, CS7, CS8
    //    CLOCAL - Ignore modem status lines
    //    CREAD - Enable receiver
    //    IGNPAR = Ignore characters with parity errors
    //    ICRNL - Map CR to NL on input (Use for ASCII comms where you want to auto correct end of line characters - don't use for bianry comms!)
    //    PARENB - Parity enable
    //    PARODD - Odd parity (else even)
    struct termios options;
    tcgetattr(uart0_filestream, &options);
    options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;  //<Set baud rate
    options.c_iflag = IGNPAR;
    options.c_oflag = 0;
    options.c_lflag = 0;
    tcflush(uart0_filestream, TCIFLUSH);
    tcsetattr(uart0_filestream, TCSANOW, &options);
    p_tx_buffer = &tx_buffer[0];
    strcpy(tx_buffer, argv[1]);
    lx=strlen(tx_buffer);


    if (uart0_filestream != -1)
    {
        int count = write(uart0_filestream, &tx_buffer[0], 10);  //Filestream, bytes to write, number of bytes to write
        if (count < 0)
        {
            printf("UART TX error\n");
        }
    }
    //----- CLOSE THE UART -----
    close(uart0_filestream);
}

然后我用一个PHP脚本(test.php的)通过系统命令来处理软件:

Then I used a PHP script (test.php) to handle the software via the "system" command:

<?php
     $mes = "0123456789";
     $retval = 0;
     $last_line = 0;
     echo($mes);
     $SndMsg = "/var/www/SendUART " . $mes;
     $last_line = system($SndMsg, $retval);
     echo $last_line
?>

和我通过命令行执行的:

And I executed it via the command line:

php -f test.php

的字符串正确地接收到的Arduino(我开发了一个简单的草图获得的Arduino这开启,如果从接收引脚接收到的所有字符板载LED)。

The string is correctly received by the Arduino (I developed a simple sketch for Arduino that turns on the onboard LED if all characters are received from the Rx pin).

然后我打电话通过Apache Web服务器的PHP脚本,在地址栏中写着:
http://192.168.1.103/test.php

Then I called the PHP script via the Apache web server, writing in the address bar: http://192.168.1.103/test.php

其中192.168.1.103是树莓派的IP地址,而 test.php的是PHP脚本。当然,无论 test.php的和SenUART程序都存储在同一文件夹中的/ var / www /,脚本,但在浏览器中显示以下错误:

where 192.168.1.103 is the IP address of the Raspberry Pi and test.php is the PHP script. Of course, both test.php and the SenUART program are stored in the same folder /var/www/, but the following error is shown in the browser:

0123456789Error - 无法打开UART。确保它不会被其他应用程序

0123456789Error - Unable to open UART. Ensure it is not in use by another application

我该如何解决这一问题?

How do I fix the problem?

推荐答案

用户在运行您的Web服务器可能没有访问UART。您可以通过配置通过设置以下Apache以root身份运行快速测试这个的httpd.conf

The user running your webserver probably doesn't have access to the UART. You can quickly test this by configuring Apache to run as root by setting the following in httpd.conf:

User root

由于它是不跑一个网络服务器作为根是个好主意,你要找出什么用户的Apache通常运行为您的系统上(可能是 WWW )和给使用串行端口用户权限。这样的事情可能工作:

Since it's not a good idea to run a webserver as root, you'll want to find out what user Apache usually runs as on your system (probably www) and give that user permission to use the serial port. Something like this may work:

chown :www /dev/ttyAMA0
chmod g+rw /dev/ttyAMA0

另外,你可能只需要用户 WWW 添加到组像标注

useradd -G callout www

调整您的特定系统。

Adjust for your specific system.

这篇关于如何通过一个Apache网页服务器控制树莓派的串行GPIO引脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆