目前共有3篇帖子。 内容转换:不转换▼
 
点击 回复
426 2
【程序】AVR单片机+uip协议栈动态生成HTML网页
一派护法 十九级
1楼 发表于:2017-5-29 17:12
以下程序生成的HTML网页上面有一个form表单,但暂时不支持对表单提交的POST数据进行解析。
【类型定义】
typedef struct
{
    uint8_t step;
    char buf[20];
    char len_s[4];
    uint8_t bufptr;
} uip_tcp_appstate_t;

【程序代码】
#include <avr/pgmspace.h>
#include <stdio.h>
#include <string.h>
#include "uip/uip.h"

const char html_0[] PROGMEM = "HTTP/1.1 200 OK\r\nContent-Length: ";
const char html_1[] PROGMEM = "\r\nKeep-Alive: timeout=5, max=100\r\nConnection: Keep-Alive\r\nContent-Type: text/html\r\n\r\n";

const char html_2[] PROGMEM = "<!doctype html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<title>AVR Page</title>\n</head>\n\n<body>\n<form id=\"form1\" name=\"form1\" method=\"post\">\nThis is a page from a server just made of AVR MCU.<br>\n<input name=\"textfield\" type=\"text\" id=\"textfield\" value=\"";
const char html_3[] PROGMEM = "\">\n<input type=\"submit\" value=\"Submit\">\n</form>\n</body>\n</html>\n";

void send_current_str(void)
{
    uint8_t len = strlen(uip_conn->appstate.buf + uip_conn->appstate.bufptr);
    uip_send(uip_conn->appstate.buf + uip_conn->appstate.bufptr, len);
    uip_conn->appstate.bufptr += len + 1;
}

void uip_appcall(void)
{
    if (uip_connected())
    {
        uip_conn->appstate.step = 0;
        uip_conn->appstate.bufptr = 1 + sprintf_P(uip_conn->appstate.buf, PSTR("PINA=%d"), PINA);

        sprintf_P(uip_conn->appstate.len_s, PSTR("%d"), strlen_P(html_2) + strlen_P(html_3) + uip_conn->appstate.bufptr - 1);
        uip_conn->appstate.bufptr = 0;
    }

    if (uip_acked() || uip_newdata() || uip_rexmit())
    {
        switch (uip_conn->appstate.step)
        {
        case 0:
            uip_send_P(html_0);
            break;
        case 1:
            uip_send(uip_conn->appstate.len_s, strlen(uip_conn->appstate.len_s));
            break;
        case 2:
            uip_send_P(html_1);
            break;
        case 3:
            uip_send_P(html_2);
            break;
        case 4:
            send_current_str();
            break;
        case 5:
            uip_send_P(html_3);
            break;
        default:
            uip_close();
        }

        if ((uip_newdata() || uip_acked()) && !uip_closed())
            uip_conn->appstate.step++;
    }
}
一派护法 十九级
2楼 发表于:2017-5-29 17:14
最后一个if语句可以替换为:
if (!uip_rexmit() && !uip_closed())
    uip_conn->appstate.step++;
一派护法 十九级
3楼 发表于:2017-5-29 17:27
【4个文本框】
#include <avr/pgmspace.h>
#include <stdio.h>
#include <string.h>
#include "uip/uip.h"

const char html_0[] PROGMEM = "HTTP/1.1 200 OK\r\nContent-Length: ";
const char html_1[] PROGMEM = "\r\nKeep-Alive: timeout=5, max=100\r\nConnection: Keep-Alive\r\nContent-Type: text/html\r\n\r\n";

const char html_2[] PROGMEM = "<!doctype html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<title>AVR Page</title>\n</head>\n\n<body>\n<form id=\"form1\" name=\"form1\" method=\"post\">\nThis is a page from a server just made of AVR MCU.<br>\n<label for=\"textfield\">PINA: </label><input name=\"textfield\" type=\"text\" id=\"textfield\" value=\"0x";
const char html_3[] PROGMEM = "\"><br>\n<label for=\"textfield2\">PINB: </label><input name=\"textfield2\" type=\"text\" id=\"textfield2\" value=\"0x";
const char html_4[] PROGMEM = "\"><br>\n<label for=\"textfield3\">PINC: </label><input name=\"textfield3\" type=\"text\" id=\"textfield3\" value=\"0x";
const char html_5[] PROGMEM = "\"><br>\n<label for=\"textfield4\">PIND: </label><input name=\"textfield4\" type=\"text\" id=\"textfield4\" value=\"0x";
const char html_6[] PROGMEM = "\"><br>\n<input type=\"submit\" value=\"Submit\">\n</form>\n</body>\n</html>\n";

void send_current_str(void)
{
    uint8_t len = strlen(uip_conn->appstate.buf + uip_conn->appstate.bufptr);
    uip_send(uip_conn->appstate.buf + uip_conn->appstate.bufptr, len);
    uip_conn->appstate.bufptr += len + 1;
}

void uip_appcall(void)
{
    if (uip_connected())
    {
        uip_conn->appstate.step = 0;
        uip_conn->appstate.bufptr = 1 + sprintf_P(uip_conn->appstate.buf, PSTR("%02x"), PINA);
        uip_conn->appstate.bufptr += 1 + sprintf_P(uip_conn->appstate.buf + uip_conn->appstate.bufptr, PSTR("%02x"), PINB);
        uip_conn->appstate.bufptr += 1 + sprintf_P(uip_conn->appstate.buf + uip_conn->appstate.bufptr, PSTR("%02x"), PINC);
        uip_conn->appstate.bufptr += 1 + sprintf_P(uip_conn->appstate.buf + uip_conn->appstate.bufptr, PSTR("%02x"), PIND);

        sprintf_P(uip_conn->appstate.len_s, PSTR("%d"), strlen_P(html_2) + strlen_P(html_3) + strlen_P(html_4) + strlen_P(html_5) + strlen_P(html_6) + uip_conn->appstate.bufptr - 4);
        uip_conn->appstate.bufptr = 0;
    }

    if (uip_acked() || uip_newdata() || uip_rexmit())
    {
        switch (uip_conn->appstate.step)
        {
        case 0:
            uip_send_P(html_0);
            break;
        case 1:
            uip_send(uip_conn->appstate.len_s, strlen(uip_conn->appstate.len_s));
            break;
        case 2:
            uip_send_P(html_1);
            break;
        case 3:
            uip_send_P(html_2);
            break;
        case 4:
        case 6:
        case 8:
        case 10:
            send_current_str();
            break;
        case 5:
            uip_send_P(html_3);
            break;
        case 7:
            uip_send_P(html_4);
            break;
        case 9:
            uip_send_P(html_5);
            break;
        case 11:
            uip_send_P(html_6);
            break;
        default:
            uip_close();
        }

        if (!uip_rexmit() && !uip_closed())
            uip_conn->appstate.step++;
    }
}

回复帖子

内容:
用户名: 您目前是匿名发表
验证码:
(快捷键:Ctrl+Enter)
 

本帖信息

点击数:426 回复数:2
评论数: ?
作者:巨大八爪鱼
最后回复:巨大八爪鱼
最后回复时间:2017-5-29 17:27
 
©2010-2024 Arslanbar Ver2.0
除非另有声明,本站采用知识共享署名-相同方式共享 3.0 Unported许可协议进行许可。