MicroPython TPYBoard v201 simple web server implementation

Foreword

TPYBoard v201 Ethernet development board is mounted to TTL serial module (USR-K2), thereby realizing the function of TCP communication. Since TCP communication can naturally also be implemented HTTP protocol. Thus, on the germination of the tutorial. This tutorial is very simple, just implement a simple static page, hoping to bring a different inspiration to everyone.

TPYBoard v201 physical map:

MicroPython TPYBoard v201 simple web server implementation

Ready to work

Hardware & Tools

  • A development board TPYBoard v201
  • TPYBoard v201 tool configuration of network parameters
  • Router, computer, cable, etc.

Knowledge base

  • Understand the relevant knowledge of basic TCP, HTTP communication protocol. I do not know may have to go to Baidu, a lot of tutorials.

TPYBoard v201 network configuration parameters

USR-K2 information and Download: Download
Download is on GitHub, if the download speed is very slow, you can also add TPYBoard technical exchange / skirt /; 157,816,561 files to download.

First, the computer TPYBoard v201 development board by using a network cable and you access the same LAN. Double click <USR-M0_V2.2.1.272.exe>, click on the search equipment, in the case of a successful search will display to the device in the above list.

MicroPython TPYBoard v201 simple web server implementation

Click the search elements to the device, you can read the relevant parameters of the device. But it can also be modified and saved. Next, we want to set the IP address and port. I am here to set the IP address static IP <192.168.0.99>, prevent IP will change each time you restart.

MicroPython TPYBoard v201 simple web server implementation
Next port settings, select the operating mode module TCP Server. We can note, there is a basic set set HTTP service port, because the USR-K2 itself has a built-in Web page used to set parameters, the default is port 80 is enabled, then we opened below the TCP Server service on the use of port 81 port it.

MicroPython TPYBoard v201 simple web server implementation

Once set up, click on the bottom of the software to save the settings. Operation log to the left of the display box shows the progress and status of preservation.

MicroPython TPYBoard v201 simple web server implementation

Then re-search module is the same whether the IP address of the next device, and we set verify.

MicroPython TPYBoard v201 simple web server implementation

Program realization

TPYBoard v201 through the serial port on the board 6 between the communication module and the USR-K2. We do not have to manage TCP data links and other issues, simply want to send is sent to the K2 modules can be through the serial port 6. In fact, our program is mainly read and write serial work, when we read from the serial port to the HTTP request to form a simple response message written to the serial port to send it back.

main.py Source:

import pyb
from pyb import UART

#串口6初始化
uart = UART(6,115200,timeout = 100)
#响应报文
header = """
HTTP/1.1 200 OK
Content-Type:text/html
Content-Length:{0}

{1}
"""
#HTML页面
html = """<!DOCTYPE html>
<html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <head> <title>TPYBoard</title> </head>
    <body>
      <h1>TPYBoard v201</h1><br />
      <h2>Simple HTTP server</h2>
    </body>
</html>
"""

while True:
    if uart.any() > 0:
        request = uart.read().decode()
        print('request:',request)
        #当接收到GET请求头时,进行响应
        if request.find('GET') > -1:
            data = header.format(len(html),html)
            uart.write(data)

Save the code. Open your browser and enter the URL to access it. URL = ip: 81, for example, my URL is 192.168.0.99:81.

MicroPython TPYBoard v201 simple web server implementation

PuTTY the print data received request header, as follows:

    request: GET / HTTP/1.1
    Host: 192.168.0.99:81
    Connection: keep-alive
    Cache-Control: max-age=0
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.7 Safari/537.36
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
    Accept-Encoding: gzip, deflate
    Accept-Language: zh-CN,zh;q=0.9

I use the Google browser, a different browser may be somewhat different, but similar, provided that they meet the HTTP request packet format can be, interested friends can get to know more. Careful friends will notice that each time you access the browser will send two requests, one of which GET /favicon.ico HTTP / 1.1, this is because the browser looking for favicon.ico file as a Web page to access the icon, this can ignored, without sacrificing functionality. Filtering may be performed in the program.

Download Source: https://github.com/TPYBoard/TPYBoard-v201

Guess you like

Origin blog.51cto.com/13798209/2422409