【RTT&英飞凌PSoC6评估板】rt-thread下网页服务器控制LED灯
[复制链接]
本帖最后由 TL-LED 于 2023-7-24 22:00 编辑
测试下PSoC6开发板网页服务器控制板卡上的LED指示灯。
在http://bbs.eeworld.com.cn/thread-1250711-1-1.html例程的基础上,增加网页服务器功能。
一、测试代码
1.1、httpserver_net.c
#include "lwip/opt.h"
#include "lwip/arch.h"
#include "lwip/api.h"
#include "board.h"
#include <stdbool.h>
#define LED2_PIN GET_PIN(0, 1)
#define LED2_ON rt_pin_write(LED2_PIN, PIN_LOW);
#define LED2_OFF rt_pin_write(LED2_PIN, PIN_HIGH);
#if LWIP_NETCONN
#ifndef HTTPD_DEBUG
#define HTTPD_DEBUG LWIP_DBG_OFF
#endif
const static char http_html_hdr[] =
"HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
const unsigned char Led1On_Data[] =
"<HTML> \
<head><title>HTTP LED Control</title></head> \
<center> \
<p> \
<font size=\"6\">LED<font style = \"color:red\">已打开!</font> \
<form method=post action=\"off\" name=\"ledform\"> \
<input type=\"submit\" value=\"关闭\" style=\"width:80px;height:30px;\"></form> \
</center> \
</HTML> ";
const unsigned char Led1Off_Data[] =
"<HTML> \
<head><title>HTTP LED Control</title></head> \
<center> \
<p> \
<font size=\"6\">LED<font style = \"color:red\">已关闭!</font> \
<form method=post action=\"on\" name=\"ledform\"> \
<input type=\"submit\" value=\"打开\" style=\"width:80px;height:30px;\"></form> \
</center> \
</HTML> ";
static const char http_index_html[] =
"<html><head><title>Congrats!</title></head>\
<body><h2 align=\"center\">RTT&英飞凌PSoC6评估板 lwIP HTTP Server!</h2>\
<p align=\"center\">This is a small test page : http control led.</p>\
<p align=\"center\"><a href=\"http://bbs.eeworld.com.cn/elecplay/content/fd37a7d3/\">\
<font size=\"6\"> RTT&英飞凌PSoC6评估板 </font> </a></p>\
<a href=\"http://bbs.eeworld.com.cn/elecplay/content/82c9e772\">\
<img src=\"http://bbs.eeworld.com.cn/data/attachment/forum/202305/16/180636shdknb4w8bxdkv2d.png\"/></a>\
</body></html>";
static bool led_on = false;
void httpserver_send_html(struct netconn *conn, bool led_status)
{
netconn_write(conn, http_html_hdr,
sizeof(http_html_hdr)-1, NETCONN_NOCOPY);
if (led_status == true)
netconn_write(conn, Led1On_Data,
sizeof(Led1On_Data)-1, NETCONN_NOCOPY);
else
netconn_write(conn, Led1Off_Data,
sizeof(Led1Off_Data)-1, NETCONN_NOCOPY);
netconn_write(conn, http_index_html,
sizeof(http_index_html)-1, NETCONN_NOCOPY);
}
static void httpserver_serve(struct netconn *conn)
{
struct netbuf *inbuf;
char *buf;
u16_t buflen;
err_t err;
err = netconn_recv(conn, &inbuf);
if (err == ERR_OK)
{
netbuf_data(inbuf, (void**)&buf, &buflen);
if (buflen>=5 &&
buf[0]=='G' &&
buf[1]=='E' &&
buf[2]=='T' &&
buf[3]==' ' &&
buf[4]=='/' )
{
httpserver_send_html(conn, led_on);
}
else if (buflen>=8&&buf[0]=='P'&&buf[1]=='O'
&&buf[2]=='S'&&buf[3]=='T')
{
if (buf[6]=='o'&&buf[7]=='n')
{
led_on =true;
LED2_ON;
}
else if (buf[6]=='o'&&buf[7]=='f'&&buf[8]=='f')
{
led_on = false;
LED2_OFF;
}
httpserver_send_html(conn, led_on);
}
netbuf_delete(inbuf);
}
netconn_close(conn);
}
static void httpserver_thread(void *arg)
{
struct netconn *conn, *newconn;
err_t err;
LWIP_UNUSED_ARG(arg);
conn = netconn_new(NETCONN_TCP);
LWIP_ERROR("http_server: invalid conn", (conn != NULL), return;);
led_on = true;
LED2_ON;
netconn_bind(conn, NULL, 80);
netconn_listen(conn);
do
{
err = netconn_accept(conn, &newconn);
if (err == ERR_OK)
{
httpserver_serve(newconn);
netconn_delete(newconn);
}
}
while (err == ERR_OK);
netconn_close(conn);
netconn_delete(conn);
}
void httpserver_init()
{
rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);
sys_thread_new("http_server_netconn",httpserver_thread, NULL, 1024, 4);
}
#endif
1.2、main.c
#include <rtthread.h>
#include <rtdevice.h>
#include "drv_gpio.h"
#include "httpserver_net.h"
int main(void)
{
httpserver_init();
for (;;)
{
rt_thread_mdelay(500);
}
}
二、程序运行
开发运行后,配置wifi用户名和密码后,输入开发板的IP地址,我这里是192.168.1.109
LED开关控制操作视频
psoc
|