使用HTML和cgi控制I.MX6ULL开发板上的LED

一.HTML文件

html 复制代码
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>LED_device</title>     
    </head>
    <body>
 
        <form action="/cgi-bin/led.cgi" method="post">
            <p>LED设备号</p>
            <input type="text" name="name">
            <p>操作</p>
            <input type="text" name="number">
            <input type="submit" value="提交">
        </form>

        <a href="/cgi-bin/test.cgi"> 跳转到test.cgi </a>
 
    </body>
</html>

二.led.c文件

cpp 复制代码
#include <stdio.h>
#include "cgic.h"
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>



#define LED_DEVICE "/sys/class/leds/sys-led/brightness"

#define LED_DEVICE0    1
#define LED_ON  1
#define LED_OFF 0


int cgiMain() 
{
    char name[241] = {0};
    char number[241] = {0};
    int led_device = 0;
    int led_operation = 0; 
    int fd = -1;

    fd = open(LED_DEVICE,O_RDWR);
    if(0 > fd)
    {
        printf("led device open failed\n");
    }

    cgiHeaderContentType("text/html");
    fprintf(cgiOut, "<HTML>\n");

        fprintf(cgiOut,"<HEAD>\n");

            fprintf(cgiOut, "<TITLE>LED CGI</TITLE>\n");
            fprintf(cgiOut,"<meta charset='utf-8'>\n");

        fprintf(cgiOut,"</HEAD>\n");

        fprintf(cgiOut,"<BODY>\n");

            fprintf(cgiOut, "<H1>LED CGI</H1>\n");
            cgiFormString("name",name,241);                         //获取名为name的数据
            cgiFormString("number",number,241);                     //获取名为number的数据
            fprintf(cgiOut,"<H2>name=%s</H2>\n",name);
            fprintf(cgiOut,"<H2>number=%s</H2>\n",number);
            fprintf(cgiOut, "<a href='/led.html'>回到LED控制界面</a>\n\n");

            led_device = atoi(name);
            led_operation = atoi(number);

            if((led_device == LED_DEVICE0) && (led_operation == LED_ON))
            {
                fprintf(cgiOut,"<p>开灯操作成功</p>");
            }
            else if((led_device == LED_DEVICE0) && (led_operation == LED_OFF))
            {
                fprintf(cgiOut,"<p>关灯操作成功</p>\n");
            }
            else
            {
                fprintf(cgiOut,"<p>操作指令有误</p>\n");
            }


        fprintf(cgiOut, "</BODY>\n");

        /* 对比设备和操作指令,使用write函数向LED文件写入数据以控制灯的亮灭 */
        if(led_device == LED_DEVICE0 && led_operation == LED_ON)
        {
            write(fd,"1",1);
        }
        else if(led_device == LED_DEVICE0 && led_operation == LED_OFF)
        {
            write(fd,"0",1);
        }


        close(fd);

    fprintf(cgiOut, "</HTML>\n");


    return 0;
}

在led.html网页中输入设备号为1,操作指令为1则可开灯,操作指令为0则关灯

相关推荐
子兮曰3 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰3 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万3 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝3 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋3 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁3 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
汉堡大王95273 天前
Jev:不是聊天机器人, 而是一个智能 if 语句
前端·人工智能·后端
梦想很大很大3 天前
从运行事实到回归证据:Workrun 的 Telemetry 与 Evaluation 实践
前端·人工智能·后端
计算机魔术师3 天前
Meta Muse agent 接入 Shopify 的 Shop Pay 实现代理式购物
前端
沙蒿同学3 天前
我用 Go 搭了一条 AI Agent 流水线:从 1 张商品图到一整套淘宝详情页
前端·javascript·后端