EBAZ4203(4205)+WEB实现远程控制记录

2 篇文章 0 订阅
1 篇文章 0 订阅
该文详细介绍了如何在Linux环境下,通过移植轻量级HTTP服务器thttpd和编写CGI脚本来实现对嵌入式设备的Web远程控制功能。主要步骤包括thttpd的编译、CGI脚本的创建、服务器配置以及权限设置,最终实现在Web界面上控制板子上的红绿灯状态。
摘要由CSDN通过智能技术生成

板子移植Linux后,尝试Web远程控制功能,实现所谓物联网概念。

适用于嵌入式的web服务器,有开源的BoaThttpdShttpdLighttpdAppWeb等,这里选择了thttpd,介绍说是一款简单、小巧、易移植、快速和安全的HTTP服务器。

一、编译thttpd

1、下载到源码thttpd-2.29.tar.gz,在有arm-linux-gnueabihf-gcc环境的ubuntu16虚拟机上解压准备编译。

lrg@zynq:~/thttpd$ tar -xzvf thttpd-2.29.tar.gz
lrg@zynq:~/thttpd$ cd thttpd-2.29/
lrg@zynq:~/thttpd/thttpd-2.29$ ./configure --prefix /usr/local/thttpd

2、找出Makefile文件,共有3个,修改每个Makefile文件里的CC内容 ,原来的CC=gcc改成CC=arm-linux-gnueabihf-gcc 。

lrg@zynq:~/thttpd/thttpd-2.29$ find -name Makefile
./Makefile
./extras/Makefile
./cgi-src/Makefile
lrg@zynq:~/thttpd/thttpd-2.29$ 

修改完后编译:

lrg@zynq:~/thttpd/thttpd-2.29$ make clean && make

有几个警告,不予理会。

 编译出来就一个可执行文件 thttpd,再压缩一下该文件,压缩后76K。

lrg@zynq:~/thttpd/thttpd-2.29$ arm-linux-gnueabihf-strip thttpd
lrg@zynq:~/thttpd/thttpd-2.29$
lrg@zynq:~/thttpd/thttpd-2.29$ du -sh thttpd
76K     thttpd
lrg@zynq:~/thttpd/thttpd-2.29$

查看一下文件属性,看其是否是基于ARM的可执行文件:

lrg@zynq:~/thttpd/thttpd-2.29$ file thttpd
thttpd: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=919a9de358bed4489a896806b41704e9bc8877f0, stripped
lrg@zynq:~/thttpd/thttpd-2.29$

3、编译cgi,用于调用系统资源。这里用了一个cgi库:cgic207.tar.gz

lrg@zynq:~/cgi$ ls
cgic207.tar.gz
lrg@zynq:~/cgi$ tar -xzvf cgic207.tar.gz
lrg@zynq:~/cgi/cgic207$ ls
capture.c  cgic.h     cgictest.c    Makefile    support.txt
cgic.c     cgic.html   led_cgi.c  license.txt     readme.txt
lrg@zynq:~/cgi/cgic207$

新建led_cgi.c文件 :

#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <stdio.h>
#include "cgic.h"
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int cgiMain()
{
    char r[4],g[4];

    cgiFormStringNoNewlines("R",r, 4); //红灯
    cgiFormStringNoNewlines("G",g, 4); //绿灯

    if(strncmp(r,"0",1) == 0)
    {
        //关红灯
      system("echo 0 > /sys/bus/platform/devices/ebaz-leds/leds/red/brightness");
    }
    if(strncmp(r,"1",1) == 0)
    {
        //开红灯
      system("echo 1 > /sys/bus/platform/devices/ebaz-leds/leds/red/brightness");
    }
    if(strncmp(g,"0",1) == 0)
    {
        //关绿灯
      system("echo 0 > /sys/bus/platform/devices/ebaz-leds/leds/green/brightness");
    }
    if(strncmp(g,"1",1) == 0)
    {
        //开绿灯
      system("echo 1 > /sys/bus/platform/devices/ebaz-leds/leds/green/brightness");
    }
    return 0;
}

 交叉编译led_cgi.c:

lrg@zynq:~/cgi/cgic207$ arm-linux-gnueabihf-gcc -static -o led.cgi cgic.c led_cgi.c

 生成led.cgi文件。

二、在EBAZ4023上配置web服务器

1、将thttpd 文件拷贝到板子Linux目录 /usr/local/thttpd;将led.cgi拷贝到/usr/local/thttpd/www/cgi-bin目录;将Ubuntu /etc下的mime.types文件拷贝到板子的/etc目录下。这里用的tftp工具。

root@stretch-armhf:/usr/local/thttpd/www# tftp 192.168.0.12 -c get thttpd

2、在/usr/local/thttpd目录下,创建日志、进程ID空文件、配置文件、WEB目录

root@stretch-armhf:/usr/local/thttpd# touch /usr/local/thttpd/thttpd.log
root@stretch-armhf:/usr/local/thttpd# touch /usr/local/thttpd/thttpd.pid
root@stretch-armhf:/usr/local/thttpd# touch /usr/local/thttpd/thttpd.conf
root@stretch-armhf:/usr/local/thttpd# mkdir -p www/cgi-bin
root@stretch-armhf:/usr/local/thttpd# ls -l
total 204
-rwxr-xr-x 1 root staff  75040 Jul 28 07:50 thttpd
-rw-r--r-- 1 root staff    149 Jul 29 09:18 thttpd.conf
-rw-r--r-- 1 root root  114688 Jul 30 03:02 thttpd.log
-rw-r--r-- 1 root staff      5 Jul 30 01:47 thttpd.pid
drwxr-sr-x 3 root staff   4096 Jul 28 09:47 www
root@stretch-armhf:/usr/local/thttpd#

服务器配置文件thttpd.conf内容为:

dir=/usr/local/thttpd/www
user=root
port=80
host=0.0.0.0
cgipat=/cgi-bin/*
logfile=/usr/local/thttpd/thttpd.log
pidfile=/usr/local/thttpd/thttpd.pid
root@stretch-armhf:/usr/local/thttpd/www# ls -l
total 428
d-wx--s--x 2 root staff   4096 Jul 30 02:55 cgi-bin
-rw-r--r-- 1 root staff   1220 Jul 30 02:51 index.htm
-rw-r--r-- 1 root staff 378528 Jul 28 08:54 jquery-1.8.3.js
-rw-r--r-- 1 root staff  45167 Jul 28 08:54 jquery-form.js
root@stretch-armhf:/usr/local/thttpd/www# cd cgi-bin/
root@stretch-armhf:/usr/local/thttpd/www/cgi-bin# ls -l
total 600
-rwxr-xr-x 1 root staff 612952 Jul 30 02:55 led.cgi
root@stretch-armhf:/usr/local/thttpd/www/cgi-bin#

注意的是,thttpd 和www/cgi-bin目录的cgi文件必须修改为可执行权限,而其它文件为非可执行:

root@stretch-armhf:/usr/local/thttpd# chmod +x thttpd
root@stretch-armhf:/usr/local/thttpd# cd ./www/cgi-bin/
root@stretch-armhf:/usr/local/thttpd/www/cgi-bin#
root@stretch-armhf:/usr/local/thttpd/www/cgi-bin# chmod +x led.cgi

启动WEB服务器 :

root@stretch-armhf:/usr/local/thttpd# ./thttpd ?
usage:  ./thttpd [-C configfile] [-p port] [-d dir] [-r|-nor] [-dd data_dir] [-s|-nos] [-v|-nov] [-g|-nog] [-u user] [-c cgipat] [-t throttles] [-h host] [-l logfile] [-i pidfile] [-T charset] [-P P3P] [-M maxage] [-V] [-D]

root@stretch-armhf:/usr/local/thttpd#
root@stretch-armhf:/usr/local/thttpd#./thttpd -C thttpd.conf
root@stretch-armhf:/usr/local/thttpd#
root@stretch-armhf:/usr/local/thttpd#

网站首页index.htm内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>EBAZ4203远程管理</title>
</head>
<body>
    <form id="greenLedForm" action = "/cgi-bin/led.cgi" method="post" target="hideframe">
    <table width="337" border="0">
      <tr>
        <td width="54"><strong> 红灯:</strong>&nbsp;</td>
        <td width="71"><input name="R" type="RADIO" value="1" checked="checked"/>打开&nbsp;</td>
        <td width="69"><input name="R" type="RADIO" value="0" />关闭&nbsp;</td>
        <td width="115" rowspan="2" style="text-align: center"><input type="submit" name="submit" id="button1" value="提交"/></td>
      </tr>
      <tr>
        <td><strong> 绿灯:</strong>&nbsp;&nbsp;</td>
        <td><input name="G" type="RADIO" value="1" checked="checked"/>打开&nbsp;</td>
        <td><input name="G" type="RADIO" value="0" />关闭&nbsp;</td>
      </tr>
    </table>
      
    </form>
    <iframe style="display:none; height:0px; width: 0px;" src="" frameborder="0" name="hideframe"></iframe>

</body>
</html>

Web界面:

 通过Web远程实现了控制板子上的红、绿灯亮与灭。

相关文件下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值