我们在利用Arduino开发网络服务器的时候,会用到以太网Shield。在其中加入以太网库后,你的设备将能够通过以太网Shield应答HTTP的请求。在打开浏览器并确认你的以太网Shield的IP地址后,你的Arduino将会用足够的HTML来应答浏览器,并让浏览器显示六个模拟针脚的输入值。
需要的硬件
Circuit电路
以太网shield能让我们的Arduino通过SPI总线连接到一个WIZnet以太网控制器。以太网shield利用第10、11、12、13针脚来连接WIZnet的SPI。以太网shield的新产品在开发板上含有一个SD卡。数字针脚4是用来控制SD卡上面的slave select针脚。
以太网shield应当连接到一个具有以太网线的网络。你需要在程序中更改网络设置来和网络进行通信。
Arduino会被放置在以太网shield下面
示意图
源代码
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 20 Mar 2012
by Tom Igoe
*/
#include
#include
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
Serial.begin(9600);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("");
client.println("");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("
***** http-equiv="<strong" />
\"refresh\" content=\"5\">");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("
");
}
client.println("");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
英文详细内容请参考:http://arduino.cc/en/Tutorial/WebServer
对于WIZnet的产品,如果您有什么疑问请直接留言也可以登录WIZnet的官网:http://www.wiznettechnology.cn
或者来信:Tel: 86-10-84539974(转166),QQ:2464237212,邮箱:wiznetbj@wiznettechnology.com,联系人:Lily Zhang,谢谢!
文章评论(0条评论)
登录后参与讨论