最新消息:

Arduino库教程-Ethernet-Web Server

Arduino 少儿编程 1646浏览 0评论
Arduino库教程

Web Server(网页服务器)

  • 在这个例子中,你将用Ethernet Shield和Arduino或genuino开发板来创建一个简单的Web服务器。采用以太网库,您的设备能够通过 Ethernet shield回答一个HTTP请求。在打开浏览器并导航到您的Ethernet shield的IP地址之后,你的Arduino会响应足够的HTML来使浏览器显示全部六个模拟引脚的输入值。

硬件要求

  • Arduino 或者 Genuino 开发板
  • Arduino Ethernet Shield

电路

  • 以太网shield可以让你通过SPI总线连接一个 Wiznet 以太网控制器到Arduino或者genuino开发板板。它使用SPI总线连接的引脚pin 10,11,12,和13,到Wiznet。以太网shield后来的模块也有一个SD卡在板上。数字引脚 pin 4 用来控制SD卡上的从选择引脚(slave select pin)。

  • shield应该连接到一个有以太网电缆的网络。您将需要更改程序中的网络设置来对应于您的网络。
    Arduino库教程-Ethernet-Web Server
    图由 Fritzing 软件绘制
    在上面的图片里,Arduino或genuino开发板会堆叠在以太网shield下面。

原理图
Arduino库教程-Ethernet-Web Server

警告

这个例子不需要一个SD卡。如果一个SD卡插入但不使用,可能是程序要挂上,因为引脚4作为SD卡的SS(低电平触发),当不使用时,它需要被配置为默认输入。有两种可能的解决方案:

  • 删除SD卡
  • 在setup()里增加这几行代码
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);

[Get Code]

样例代码

/*
  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 9 Apr 2012
 by Tom Igoe
 modified 02 Sept 2015
 by Arturo Guadalupi

 */

#include <SPI.h>
#include <Ethernet.h>

// 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() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  // 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("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // 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("<br />");
          }
          client.println("</html>");
          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 disconnected");
  }
}

[Get Code]
更多

  • Arduino Ethernet Shield – 产品描述。
  • Getting started with the Ethernet Shield – 在几分钟内启动所有东西。
  • Ethernet library – 以太网库的参考手册
  • AdvancedChatServer – 一个服务器,用来发送所有传入的信息到所有连接的客户端(除了那个发送信息的客户端)。
  • ChatServer – 一个简单的服务器,用来发送所有传入的信息到所有连接的客户端。
  • WebClient – 查询网络,并通过串口监视器得到答案
  • WebClientRepeating – 如何用以太网shield重复HTTP请求。
  • WebServer – 一个简单的Web服务器,用来显示模拟输入的值。
  • DhcpAddressPrinter – 获取DHCP地址,并打印出到串口监视器。
  • DhcpChatServer – 连接到一个telnet服务器,并打印所有收到的信息到串口监视器上。用DHCP。
  • TelnetClient – 连接到一个telnet服务器,并打印所有收到的信息到串口监视器上。
  • BarometricPressureWebServer – 用SPI从压力传感器读取的Post数据。
  • UDPSendReceiveString – 通过UDP协议(通用数据包)发送和接收文本字符串。
  • UdpNtpClient – 查询一个网络时间协议(NTP)服务器,并通过串口服务器监视器获取这个信息。

您必须 登录 才能发表评论!