如何通过Apache网络服务器控制Raspberry Pi的串行GPIO引脚
我想通过使用运行在Raspberry Pi上的Apache服务器,通过GPIO串行端口将数据从Raspberry Pi发送到Arduino Uno。我将RPI的TX引脚连接到一个3.3 V-to-5 V电平移位器,并将其输出连接到RX Arduino PIN。如何通过Apache网络服务器控制Raspberry Pi的串行GPIO引脚
从Raspberry Pi发送数据到Arduino我使用了下面的C程序,我重新编译为Raspberry Pi,它工作正常。我重命名了可执行代码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的)通过 “系统” 命令来处理软件:
<?php
$mes = "";
$retval = 0;
$last_line = 0;
echo($mes);
$SndMsg = "/var/www/SendUART " . $mes;
$last_line = system($SndMsg, $retval);
echo $last_line
?>
而且我通过命令行来执行它:
php -f test.php
该字符串被Arduino正确接收(我为Arduino开发了一个简单的sketch,如果从Rx引脚接收到所有字符,它将打开板载LED。
然后我通过Apache Web服务器称为PHP脚本,写在地址栏中输入: http://192.168.1.103/test.php
其中192.168.1.103是树莓派的IP地址和test.php
是PHP脚本。当然,无论test.php
和SenUART程序都存储在同一文件夹中的/ var/www /,脚本,但在浏览器中显示以下错误:
Error - 无法打开UART。确保它不被其他应用程序使用
如何解决该问题?
运行您的网络服务器的用户可能无法访问UART。您可以通过配置Apache以通过设置在httpd.conf
下面以root身份运行快速测试:
User root
因为它不是运行Web服务器作为根是个好主意,你要找出什么用户的Apache通常像系统一样运行(可能是www
),并为该用户提供使用串行端口的权限。这样的事情可能工作:
chown :www /dev/ttyAMA0
chmod g+rw /dev/ttyAMA0
或者,您可能只需要用户www
添加到组像callout
:
useradd -G callout www
调整为特定的系统。
亲爱的菲利普,太棒了!有用!!非常感谢你! – Ferrari 2015-03-10 08:53:36
UART是否可以作为串口控制台使用?你使用raspbian吗? http://www.raspberry-projects.com/pi/pi-operating-systems/raspbian/io-pins-raspbian/uart-pins – chugadie 2015-03-08 17:47:55
UART已启用。我正在使用raspbian。 – Ferrari 2015-03-08 22:06:36
您必须禁用UART作为串行控制台使用。看到以前发布的链接。 – chugadie 2015-03-09 12:46:58