본문 바로가기

WCH/CH32V307

CH32V307 SM - 웹서버 테스트

 

 

CH32V307 의 내장 Ethernet PHY를 이용하여 웹서버 테스트를 해 보자

사용 소스 코드는 하기 링크를 수정해서 작성하였다.

https://github.com/openwch/ch32v307/tree/main/EVT/EXAM/ETH/WebServer

 

 

웹서버 처리 함수

/*********************************************************************
 * @fn      Web_Server
 *
 * @brief   web process function.
 *
 * @return  none
 */
void Web_Server(void)
{
    char *paraptr;
    uint8_t reqnum = 0;
    u32 resplen = 0;
    u32 pagelen = 0;
    reqnum = strFind(HTTPDataBuffer,"GET") + strFind(HTTPDataBuffer,"get") + \
             strFind(HTTPDataBuffer,"POST") + strFind(HTTPDataBuffer,"post");
    while(reqnum){
        reqnum--;
        ParseHttpRequest(&http_request, HTTPDataBuffer);
        switch (http_request.METHOD)
        {
            case METHOD_ERR:
                break;
            case METHOD_POST:                                       //'post' request
                name = http_request.URL;
                ParseURLType(&http_request.TYPE, name);
                if (strstr(name, "main") != NULL) {                 //Request the "main" page
                    pagelen = strlen(Html_main);
                    copy_flash(Html_main, pagelen);
                }
                else if(strstr(name, "success") != NULL) {          //Request "success" page
                    pagelen = strlen(Html_success);
                    copy_flash(Html_success, pagelen);
                    paraptr = (char *) HTTPDataBuffer;
                    if (strstr(paraptr, "__PMAC") != NULL) {             //Configuration information with "Basic" pages
                        Refresh_Basic(paraptr);
                    }
                    if (strstr(paraptr, "__PMOD") != NULL) {             //Configuration information with "Port" page
                        Refresh_Port(paraptr);
                    }
                    if (strstr(paraptr, "__PUSE") != NULL) {             //Configuration information with "User" page
                        Refresh_Login(paraptr);
                    }
                }
                /*Analyze the requested resource type and return the response*/
                MakeHttpResponse(httpweb, http_request.TYPE, pagelen);
                resplen = strlen(httpweb);
                Data_Send(socket, httpweb, resplen);
                Data_Send(socket, HtmlBuffer, pagelen);
                /*After the request is processed, the current
                 * socket connection is closed, and a new connection
                 * will be established when the browser sends the next
                 * request.*/
                WCHNET_SocketClose(socket, TCP_CLOSE_NORMAL);
                break;
            case METHOD_GET:                                        //'get' request
                name = http_request.URL;
                ParseURLType(&http_request.TYPE, name);
                if(strstr(name, "HTTP") != NULL) {
                    pagelen = Refresh_Html(Html_login, Para_Login, 2);
                }
                else if(strstr(name, "main") != NULL) {             //Request to get the "main" web page
                    pagelen = strlen(Html_main);
                    copy_flash(Html_main, pagelen);
                }
                else if(strstr(name, "basic") != NULL) {            //Request to get the "basic" web page
                    pagelen = Refresh_Html(Html_basic, Para_Basic, 4);
                }
                else if(strstr(name, "port") != NULL) {             //Request to get the "port" page
                    pagelen = Refresh_Html(Html_port, Para_Port, 4);
                }
                else if(strstr(name, "user") != NULL) {             //Request to get the "user" web page
                    pagelen = Refresh_Html(Html_user, Para_Login, 2);
                }
                else if(strstr(name, "about") != NULL) {            //Request to get the "about" page
                    pagelen = strlen(Html_about);
                    copy_flash(Html_about, pagelen);
                }
                else if(strstr(name, "logo") != NULL) {             //Request for "logo" image
                    pagelen = sizeof(Html_logo);
                    copy_flash(Html_logo, pagelen);
                }
                else if(strstr(name, "png1") != NULL) {             //Request to get "png1" image
                    pagelen = sizeof(Html_png1);
                    copy_flash(Html_png1, pagelen);
                }
                else if(strstr(name, "png2") != NULL) {             //Request to get "png2" image
                    pagelen = sizeof(Html_png2);
                    copy_flash(Html_png2, pagelen);
                }
                else if(strstr(name, "png3") != NULL) {             //Request to get "png3" image
                    pagelen = sizeof(Html_png3);
                    copy_flash(Html_png3, pagelen);
                }
                else if(strstr(name, "png4") != NULL) {
                    pagelen = sizeof(Html_png4);
                    copy_flash(Html_png4, pagelen);
                }
                else if(strstr(name, "weixin") != NULL) {           //Request for "weixin" image
                    pagelen = sizeof(Html_weixin);
                    copy_flash(Html_weixin, pagelen);
                }
                else if(strstr(name, "style") != NULL) {            //Request to get the "style" css stylesheet file
                    pagelen = strlen(Html_style);
                    copy_flash(Html_style, pagelen);
                }
                else if(strstr(name, "led=on") != NULL) {            //Request to get the "style" css stylesheet file
                    pagelen = Refresh_Html(Html_login, Para_Login, 0);
                    printf("on:%x\n", Para_Login);
                    //pagelen = sizeof(Html_login);
                    //copy_flash(Html_login, pagelen);
                }
                else if(strstr(name, "led=off") != NULL) {            //Request to get the "style" css stylesheet file
                    pagelen = Refresh_Html(Html_login, Para_Login, 0);
                    printf("off:%x\n", Para_Login);
                    //pagelen = sizeof(Html_login);
                    //copy_flash(Html_login, pagelen);
                }
                /*Analyze the requested resource type and return the response*/
                MakeHttpResponse(httpweb, http_request.TYPE, pagelen);
                resplen = strlen(httpweb);
                Data_Send(socket, httpweb, resplen);
                Data_Send(socket, HtmlBuffer, pagelen);
                break;
            default:
                break;
        }
    }
    /*After the request is processed, the current
     * socket connection is closed, and a new connection
     * will be established when the browser sends the next
     * request.*/
    WCHNET_SocketClose(socket, TCP_CLOSE_NORMAL);
    memset(HTTPDataBuffer, 0,sizeof(HTTPDataBuffer));
}

 

 

웹페이지 소스

const char Html_login[] = R"rawliteral(
<html>
    <head><meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body{font-family:Arial; text-align: center; margin: 0px auto; padding-top:30px;}
        .switch{position:relative;display:inline-block;width:120px;height:68px}.switch input{display:none}
        .slider{position:absolute;top:0;left:0;right:0;bottom:0;background-color:#ccc;border-radius:34px}
        .slider:before{position:absolute;content:"";height:52px;width:52px;left:8px;bottom:8px;background-color:#fff;-webkit-transition:.4s;transition:.4s;border-radius:68px}
        input:checked+.slider{background-color:#2196F3}
        input:checked+.slider:before{-webkit-transform:translateX(52px);-ms-transform:translateX(52px);transform:translateX(52px)}
    </style>
    
    <script>
    function toggleCheckbox(element)
    { 
        var xhr = new XMLHttpRequest();
        if(element.checked)
        { 
            xhr.open("GET", "/?led=on", true);
        }
        else
        {
            xhr.open("GET", "/?led=off", true);
        }
        
        xhr.send();
    }
    </script>
    </head>
    
    <body>
    <h1>CH32V307 Web Server Test</h1>
    <label class="switch"><input type="checkbox" onchange="toggleCheckbox(this)" %s><span class="slider"></span></label>
    </body>
</html>
)rawliteral";

 

 

 

CH32V307 웹서버 소스코드를 수정하여 다운로드 해서 페이지에 접속하면 정상적으로 웹서버가 동작 하는것을 확인 할 수 있다.

저렴한 가격에 이더넷 PHY까지 내장된 MCU 로 간단한 어플리케이션에 빠르게 적용하기 좋은 솔루션이 될것 같다.

반응형