|
好久没有发原创文章了,实在对不起大家。
拿个翻译的东西凑数。
使用ATMega88实现HTTP/TCP(AVR Web服务器)
HTTP/TCP with an atmega88 microcontroller (AVR web server)
Abstract:
摘要
This is a continuation of the article An AVR microcontroller based Ethernet device. The hardware is still the same (ENC28J60 + atmega88). The software is now updated to provide also a a web-server.
本文是之前《基于AVR的以太网设备》的续篇。硬件还和之前一样(ENC28j60+ATMega88)。片内软件现在已经升级到能够支持做一个Web服务器了。
That is: instead of using a command line application and send UDP packets to the Ethernet device we can just point our web browser to it. .... and even better: we add only the web server and all the UDP based functionality is still there. Now you can use both!
升级的部分是:使用web浏览器取代之前的命令行发送UDR数据报的方式来访问我们的以太网设备……之前的UDP基本功能仍然保留。现在,这两种功能你都可以使用了!
The code is written in C and there is even a lot of space left on the atmega88 microcontroller.
即便代码是使用C编写的,(编译以后)ATMeg88存储器中仍然有相当的剩余。
All hardware components are available from shop.tuxgraphics.org.
The software and circuit diagrams are available for free (GPL V2 license).
代码和电路都是免费可用的。
Introduction
A UDP command interface is sufficient for most applications but an integrated web-server is much more universal and easier to use. How to build a web server into an atmega88 chip?
对于大部分应用场合,UDP命令行的方式已经足够了,但是,对于一个简单通用的综合性Web服务器来说,却远远不够。怎样在一个ATMega88中建立一个Web服务器呢?
Before starting this Ethernet project I did of course some prototyping and then I noticed already that UDP was not a problem with lots of space left on the atmega88. Therefore I was quite confident that TCP + HTTP will work. TCP/IP was invented more than 25 years ago. Todays microcontrollers provide almost the computing power a standard computer had at that time. No java or xml was used at that time. Things were done in smart and efficient ways.
在我开始做这个以太网项目的之前,我做了一些原型机,在这个过程中,我注意到,即便实现了UDP协议以后,ATMega88中仍然有大量的空间剩余。因此,我对于实现TCP+HTTP非常有信心。TCP/IP协议已经诞生了将近25年。当今的微控制器提供了一个几乎和从前标准计算机相同的计算能力。而从前,没有java和xml。今天,同样的事情通过一种更加小巧有效的方法完成。
So here is a real web-server on an atmega88 AVR microcontroller.
所以今天,我们将在这里通过ATMega88实现一个真正的Web网络服务器。
TCP is a state machine
TCP 是一个状态机
TCP is a protocol where one establishes a connection. For this a number of packets are first exchanged and both sides of the connection go through several states [see tcp state machine from rfc793]. Once the connection is established a number of data packets can be sent. More than one packet, large amounts of data can be sent. Counters and the state machine ensure that the actual user data arrives in correct order and without data loss.
TCP是一个建立网络连接的协议。通过该协议,在连接建立之前,连接双方通过交换一些IP数据报实现在一些状态间进行切换[请参照RFC793标准中TCP状态机的相关内容]。一旦连接建立成功,包含数据的IP数据报将被传送——实现海量数据的传输。通过计数器和状态机我们可以确认数据被按照正确的顺序传送,并且没有发生丢失。
Large web pages will need to send many data packets. Small pages less. How many do we need to send???
大的网页需要发送大量的数据包,当然,小的网页需要传送的数据包就要少些(数据包就是包含实际数据的IP数据报),那么,我们究竟需要发送多少呢?
Let’s take a look at the application introduced in the first article [June 2006, article060601]. In this first article we just switch on and off something. It can be done with a simple web page which might look like this:
让我们回顾一下本文的前篇中提到过的那个程序。在前文中,我们只是(用UDP协议实现)将一些开关打开(Switch on)或者关闭(Switch off),同样的功能可以通过下面的简单网页来实现:
Output is: ON
Switch off
Figure 1: The web page needed for the Ethernet Remote Device circuit.
Figure 1:使用网页来配置远处的以太网设备
Other applications might be measurement of temperature or air pressure. Those are all small web
pages with is very little data. In other words we will send less than 100 bytes including all the html
tags. How many IP packets with data will be sent for such a page?
Just one!
在其他的应用中,可能是(用UDP协议)来采集温度或者气压。这些都是小的网页,只需要很少少的信息量。换句话说,我们只需要发送不到100个字节的信息就可以包含所有的HTML网页信息了。那么,这种情况下究竟需要多少IP数据包来发送这样的页面呢?
The whole point of using TCP is that one can send more than one packet of data but we don’t need
that functionality. We need TCP only because HTTP is based on it and we want HTTP in order to
use our web browser.
使用TCP协议的要点是:虽然发送者可以发送多个数据包,但我们并不需要这样能够的功能。我们是用TCP协议,只是因为我们网页浏览器使用的HTTP协议是建立在TCP基础之上的。
Under the assumption that you will never need to send more than one packet with data the whole TCP protocol and state handling can be simplified a lot. We can e.g send the FIN immediately together with the data. This makes the state handling for the closing of the connection very simple.
假设我们永远也不需要发送多于一个的数据包,那么整个TCP协议以及其状态处理可以大大的简化。例如,我们可以在发送完数据以后立即发送FIN(申请断开连接的请求信号)。这样,关闭连接的状态处理就变得非常简单了(因为状态少了)。
Under this assumption it possible to implement a web-server in a atmega88 and have just under 50% of the chip’s memory still free for the actual application.
在这个假设下,就有可能在ATmega88中实现Web服务器,甚至我们还有将近50%的片内存储空间留下用来执行其他实际(功能)的应用程序。
The Ethernet remote device with build-in web server: switching something on and off
内建Web服务器的以太网远程设备:
开关的控制
The application that the eth_rem_dev_tcp-2.X software implements is a simple switch. You can
switch on or off something. A simple password mechanism provides very basic protection to avoid
that unauthorized users toggle the switch.
Here is a screen shot of the web-page that the eth_rem_dev_tcp-2.X displays:
代码:eth_rem_dev_tcp-2.X实现一个简单的开关控制。你可以通过这个实例来控制(远程以太网设备的某些)开关量。同时,通过一个简单的密码检测提供最基本的保护,防止未经授权的用户来攫取开关的控制权。
下面的图片是这个例程的一个截屏:
Figure 2: The atmega88 based web-server, screenshot with mozilla firefox
Figure 2:基于ATMega88的Web服务器的网页截图
A Single Data Packet TCP/HTTP web-server on a microcontroller
使用微控制器实现的单数据包的TCP/HTTP网页服务器
The code is available at the end of the article and I will explain it a bit. That way you will hopefully be able to modify it and adapt it also to other applications. The Single Data Packet web-server will go through the following TCP states:
文章结尾处的代码是可以直接使用的,当然我会适当作一些解释。这样你就可以通过修改代码来适应其他应用的要求。单数据包Web服务器将经过以下(简化过的)状态(实现一次数据传输):
1 receive SYN
2 send SYN,ACK
3 receive ACK (the connection is now established)
4 receive ACK with HTTP GET command
5 send ACK
6 send FIN,ACK with HTTP data (e.g 200 OK)
7 receive FIN,ACK
8 send ACK
1 接收“从对方到己方”的连接请求信号(SYN)
2 发送“从己方到对方”的连接请求(SYN),并应答前面的对方的连接请求(ACK)
3 接收对方回应的应答信号(现在,已经建立了一个连接)
4 接收HTTP GET 指令的应答信号(ACK)
5 发送应答信号
6 发送HTTP数据包(比方说,200个字节的数据),同时请求终止“从己方到对方”连接(FIN),并直接应答(ACK)
7 接收对方请求终止“从对方到己方”连接的信号(FIN)以及对刚才发送出去的终止请求的应答信号(ACK)
8 发送应答信号,告知对方它终止连接的请求已经被响应(ACK)
As you can see this is a quite simple command and action sequence. I have implemented the needed functions for this the file ip_arp_udp_tcp.c The file main.c is where the receive loop for the data is implemented. main.c has in this loop a number of if statements in order to decide what action to take. Here you will also see that the code branches between udp and tcp with port 80 (=web-server). If you want to implement your own application (e.g read temperatures, air pressure, whatever...) the you just need to modify the code above the call to the function print_webpage and modify the function print_webpage in order to print your own webpage. All this is in the file main.c The file enc28j60.c implements the driver to the Ethernet chip. You don’t have to worry about the enc28j60.c.
就像你上面看到的那样,这一过程非常简单。我已经在ip_arp_udp_tcp.c文件中完成了所需的基本函数。主文件main.c中有一个查询式的数据接收循环。上面描述的状态以及状态的处理程序都在这个循环中得到实现。例程中,你会发现用于分别处理UDP和TCP(Web服务器通常使用80端口)的程序分支。如果你想实现一个你自己的功能(比方说,读取温度、气压或者其他什么东东……)你只需要修改所生成网页的内容以及之前的一些代码就可以了。所有的这些都在main.c中,而文件enc28j60.c是以太网芯片的驱动函数库,你可以不必理它。
The URL format
URL格式
In order to build an interactive web page the HTML code provides "点击此处打开armok01137143.pdf
-----此内容被Gorgon Meducer于2006-12-04,22:45:44编辑过 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|