【极海APM32F407 Tiny Board】rt-thread系统下网页控制LED灯
[复制链接]
本帖最后由 TL-LED 于 2023-6-30 17:33 编辑
这篇来测试下极海APM32F407 Tiny Board开发板在rt-thread系统下使用网页来控制板子上的LED2指示灯,在https://bbs.eeworld.com.cn/thread-1247049-1-1.html这篇的基础上增加网页部分程序。
一、程序部分
1.1、在程序中增加
1.2、httpserver_conn.c
#include "lwip/opt.h"
#include "lwip/arch.h"
#include "lwip/api.h"
#include "board.h"
#include <stdbool.h>
#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\">Welcome to APM32F407 lwIP HTTP Server!</h2>\
<p align=\"center\">This is a small test page : http control led.</p>\
<p align=\"center\"><a href=\"https://bbs.eeworld.com.cn/\">\
<font size=\"6\"> 电子工程世界论坛 </font> </a></p>\
<a href=\"https://bbs.eeworld.com.cn/elecplay/content/82c9e772\">\
<img src=\"https://bbs.eeworld.com.cn/data/attachment/forum/202304/17/142507d3f4ddxd4shg3hen.jpg\"/></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()
{
sys_thread_new("http_server_netconn",httpserver_thread, NULL, 1024, 4);
}
#endif
1.3、main.c
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "httpserver-netconn.h"
int main(void)
{
uint32_t sysclock = 0;
/* set LED2 pin mode to output */
rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);
/* Print system clock */
sysclock = RCM_ReadSYSCLKFreq();
rt_kprintf("System Clock: %d\n", sysclock);
httpserver_init();
while (1)
{
//rt_pin_write(LED2_PIN, PIN_HIGH);
//rt_thread_mdelay(500);
//rt_pin_write(LED2_PIN, PIN_LOW);
rt_thread_mdelay(500);
}
}
1.4、程序源码
二、程序运行
2.1、串口输出
查看开发板的IP地址
2.2、网页运行
输入ip:192.168.1.102
2.3、运行视频
200
|