学习笔记CGI & LED
在上一篇帖子中给大家分享了一下 GoAhead服务器的移植,简单的介绍了下CGI,和Form的两种方法GET和POST。
今天就再探一下CGI。通过网页来控制开发板上的4颗LED灯的亮灭状态。
新建一个cgi_led.html 文件放到开发板主目录的 web/ 文件夹下:
- <html>
- <head>
- <title> DEMO CGI LED--HelloWii </title>
- </head>
- <body bgcolor=#87CEEB>
- <div align="center">
- <form method=get action="cgi-bin/led.cgi">
- <p>please input a number</p>
- <input type="text" name="value" maxlength="1" size="1" >
- <input type="submit" name="button" value="input">
- </form>
- <p>0: is open LED1 </p>
- <p>1: is close LED1 </p>
- <p>2: is open LED2 </p>
- <p>3: is close LED2 </p>
- <p>4: is open LED3 </p>
- <p>5: is close LED3 </p>
- <p>6: is open LED4 </p>
- <p>7: is close LED4 </p>
- <p>8: is open ALL LED </p>
- <p>9: is close ALL LED</p>
- <br \>
- <p> HelloWii </p>
- </div>
- </body>
- </html>
通过浏览器打开(http://192.168.1.20/cgi_led.html)的效果图如下(IP地址请更换为自己的开发板IP):
图11-1:cgi_led.html
上面的信息已经很清楚的表述了各个数字的功能。
下面就是写CGI的程序了,具体编译上一篇CGI学习笔记中已经将了makefile和编写,下面直接贴led.c的代码,如下:
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <linux/input.h>
- #define LED1 0
- #define LED2 1
- #define LED3 2
- #define LED4 3
- #define LED_ON 0
- #define LED_OFF 1
- typedef struct
- {
- char name[32];
- char value[32];
- }entry;
- char *ledc;
- void getValue(char *value,char *line,char flag)
- {
- int i=0,j=0;
- for(i=0;( (line[i]) && (line[i] != flag));i++)
- {value[i] = line[i];}
- value[i] = '\0';
- if(line[i]) ++i;
- while(line[j++] = line[i++]);
- }
- int main(int argc, char **argv)
- {
- unsigned int count = 0;
- int fdled = -1;
- entry get;
- printf("Content-type : text/html\n\n");
- printf("<html>\n<head><title>LED CGI PROGRAM</title></head>\n");
- printf("<body>\n");
- usleep(500*1000);
- fdled = open("/dev/led",O_RDWR);
- if(fdled<0)
- {
- printf("Error:Can't open /dev/leds\n");
- return -1;
- }
- ioctl(fdled,1,LED3);ioctl(fdled,0,LED4);
- ledc = (char *)getenv("QUERY_STRING");
- getValue(get.name, ledc,'=');
- getValue(get.value,ledc,'&');
- printf("<br><center>Your input value is <b> %c </b> </center>",get.value[0]);
- switch(get.value[0])
- {
- case '0':ioctl(fdled,LED_ON,LED1); break;
- case '1':ioctl(fdled,LED_OFF,LED1); break;
- case '2':ioctl(fdled,LED_ON,LED2); break;
- case '3':ioctl(fdled,LED_OFF,LED2); break;
- case '4':ioctl(fdled,LED_ON,LED3); break;
- case '5':ioctl(fdled,LED_OFF,LED3); break;
- case '6':ioctl(fdled,LED_ON,LED4); break;
- case '7':ioctl(fdled,LED_OFF,LED4); break;
- case '8':ioctl(fdled,LED_ON,LED1);
- ioctl(fdled,LED_ON,LED2);
- ioctl(fdled,LED_ON,LED3);
- ioctl(fdled,LED_ON,LED4);
- break;
- case '9':ioctl(fdled,LED_OFF,LED1);
- ioctl(fdled,LED_OFF,LED2);
- ioctl(fdled,LED_OFF,LED3);
- ioctl(fdled,LED_OFF,LED4);
- break;
- }
- close(fdled);
- printf("</body>\n<html>");
- return 0;
- }
makefile如下:
- # General Makefile
- Exec := led.cgi
- Obj := led.c
- CC := arm-linux-gcc
- $(Exec) : $(Obj)
- $(CC) -o $@ $(Obj) $(LDLIBS$(LDLIBS-$(@)))
- clean:
- rm -vf $(Exec) *.elf *.o
将生成的leg.cgi 可执行程序,放到开发板的 web/cgi-bin/ 文件夹下,运行程序,即可通过网页对LED灯的控制。。。
今天我就抛砖引玉,简单的讲一下我的小例子。。。期待网友可以用SIN210开发出更炫的功能,比如通过网页显示开发板的温度什么的。。。