使用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则关灯

相关推荐
顾北121 小时前
AI对话应用接口开发全解析:同步接口+SSE流式+智能体+前端对接
前端·人工智能
摸鱼的春哥1 小时前
春哥的Agent通关秘籍07:5分钟实现文件归类助手【实战】
前端·javascript·后端
Smart-Space1 小时前
htmlbuilder - rust灵活构建html
rust·html
念念不忘 必有回响1 小时前
viepress:vue组件展示和源码功能
前端·javascript·vue.js
C澒1 小时前
多场景多角色前端架构方案:基于页面协议化与模块标准化的通用能力沉淀
前端·架构·系统架构·前端框架
崔庆才丨静觅1 小时前
稳定好用的 ADSL 拨号代理,就这家了!
前端
江湖有缘1 小时前
Docker部署music-tag-web音乐标签编辑器
前端·docker·编辑器
恋猫de小郭2 小时前
Flutter Zero 是什么?它的出现有什么意义?为什么你需要了解下?
android·前端·flutter
崔庆才丨静觅9 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby606110 小时前
完成前端时间处理的另一块版图
前端·github·web components