micropython implementation TPYBoard v201 simple web server

Reproduced please specify the source of the article, more tutorials can self-refer docs.tpyboard.com, QQ technical exchange group: 157 816 561, the public number: MicroPython player sinks

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 development board

Ready to work

Hardware & Tools

- a development board TPYBoard v201
- tool arranged TPYBoard v201 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: https://github.com/TPYBoard/Documentation/blob/master/tpyboard_docs/tpyboard/tutorial/doc/USR-K2%E8%B5%84%E6%96%99.rar
Download address 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.

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.

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.

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.

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

 

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:

 1 import pyb
 2 from pyb import UART
 3 
 4 #串口6初始化
 5 uart = UART(6,115200,timeout = 100)
 6 #响应报文
 7 header = """
 8 HTTP/1.1 200 OK
 9 Content-Type:text/html
10 Content-Length:{0}
11 
12 {1}
13 """
14 #HTML页面
15 html = """<!DOCTYPE html>
16 <html>
17     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
18     <head> <title>TPYBoard</title> </head>
19     <body>
20       <h1>TPYBoard v201</h1><br />
21       <h2>Simple HTTP server</h2>
22     </body>
23 </html>
24 """
25 
26 while True:
27     if uart.any() > 0:
28         request = uart.read().decode()
29         print('request:',request)
30         #当接收到GET请求头时,进行响应
31         if request.find('GET') > -1:
32             data = header.format(len(html),html)
33             uart.write(data)

 

保存代码。打开浏览器,输入URL进行访问。URL=ip:81,例如我的URL就是192.168.0.99:81。

PuTTY中打印了接收到的请求头的数据,如下:

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

我用的是谷歌浏览器,不同浏览器可能会有些不同,但是大同小异,只要符合HTTP请求报文格式即可,有兴趣的朋友可以多去了解些。细心的朋友会注意到,每次访问时浏览器会发送两次请求,其中有一个 GET /favicon.ico HTTP/1.1,这是因为浏览器想找favicon.ico文件作为访问网页的图标,这个可以忽略掉,不影响功能。也可以在程序中进行过滤。

下载源码:https://github.com/TPYBoard/TPYBoard-v201 

Guess you like

Origin www.cnblogs.com/xiaowuyi/p/11224188.html